Skip to content

feat(build): queue builds by the account's free memory - #268

Open
martinsifra wants to merge 1 commit into
apify:masterfrom
martinsifra:feat/queue-builds-by-memory
Open

martinsifra wants to merge 1 commit into
apify:masterfrom
martinsifra:feat/queue-builds-by-memory

Conversation

@martinsifra

Copy link
Copy Markdown

Problem

runAndSummarizeBuilds starts the build of every affected Actor at once. Each platform build takes 4096 MB (builds docs), and the build API has no parameter to change that. When the account's free memory can't hold all the builds together, the platform refuses the extra ones and the whole build step fails.

Change

Builds now run in a queue per account, so they finish as fast as the account's memory allows without failing for lack of it.

  • One queue per account. Actors are grouped by their token's value, because the builds of one account share its memory. Accounts are built in parallel.
  • Builds at once. Each account's free memory is read once from GET /users/me/limits (maxActorMemoryGbytes - actorMemoryGbytes). The queue runs max(1, floor(free / 4096)) builds at once and starts the next Actor whenever one finishes. When the token can't read the limits, every build starts at once and refusals are handled as below.
  • Refused builds go back to the queue. The limits can lag behind the platform, so a start can still be refused with actor-memory-limit-exceeded or concurrent-runs-limit-exceeded, or with a 402 whose message mentions memory or concurrency. The refused Actor goes to the end of the queue, and the number of builds at once shrinks to the ones running. It's tried again when one of them finishes, or every 30 s when none of this account's builds is running. It gives up 30 minutes after its first refusal.
  • Failed builds are not re-run. A build that starts and then FAILS or TIMES OUT stops every account from starting new builds and fails the step, as before.
  • Local builds: build-from-local uploads each Actor's source zip once, even when the Actor is queued again.
  • Logging: each account logs one line, e.g. [APIFY_TOKEN_X]: 8192 MB of free memory, building 2 of 5 Actors at once (4096 MB each).

Not verified yet

  • Tests not run locally. Type-check, lint and unit tests haven't run because dependencies couldn't be installed, so CI is the first run.
  • Error type name. actor-memory-limit-exceeded comes from the API's error-type list. I haven't seen it returned on a refused build start.
  • Free memory figure. The docs don't say whether current.actorMemoryGbytes includes builds that are running.
  • No test for the single zip upload. The existing local-build tests cover dry runs only.

Tests

test/unit/bin/build.test.ts adds tests for:

  • spotting memory errors (isCapacityLimitError)
  • limited free memory, separate accounts, too little memory and unknown limits
  • re-queuing a refused build with fewer builds at once
  • the 30 s wait and giving up after 30 minutes
  • no re-queue for other errors, and no new builds after a failure

🤖 Generated with Claude Code

Every Actor's build used to start at once. Each build takes 4096 MB, so on
an account whose free memory can't hold them all, the platform refused the
extra builds and the whole build step failed.

Builds now run in a queue per account (per token): as many at once as the
free memory from the account limits holds, starting the next one whenever a
build finishes. The limits can lag behind the platform, so a build the
platform still refuses for lack of memory goes back to the end of the queue
and the parallel builds shrink to the ones running. It gives up 30 minutes
after its first refusal. A build that starts and then fails is not re-run.

Local builds upload their source zip once, even when queued again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant