Repository navigation
fix(functions): requeue jobs callbacks that lose the deployment lock - #15852
Arnav-1456 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
Code review
The jobs worker now republishes state and log callbacks when they contend for the deployment lock, with a bounded requeue count carried in the jobs-message envelope. The new end-to-end tests cover eventual callback application after lock release and dead-lettering after the retry cap, using a helper to read the project sequence and hold the Redis lock. Note @Arnav-1456 does not have write access to this repository, so Hansi does not approve automatically. A maintainer can review and approve. Walkthrough (4)
Fixed since the last review (2)
Reviewed Tip Comment |
cbb74be to
2d4f416
Compare
A callback (log batch leader, artifact, exit, complete) that cannot acquire the per-deployment lock within LOCK_TIMEOUT (10s) threw Contention, and on the Redis broker a rejection is final: the message was dead-lettered to the failed list and never applied, while the jobs-service does not resend (the ingest endpoint already answered 204). A lost exit or complete callback could leave a deployment in building, and a lost log batch lost its lines - the failed list grew by hundreds of entries per deploy day (issue appwrite#15745). Contention is raised before the callback body runs, so a contended message has applied nothing and is safe to requeue as-is: catch it (for the state callbacks in the action body and for the gated log batch leader in log()), publish the same envelope back onto v1-jobs through publisherForJobs, and let the queue provide the spacing. A requeued log callback re-adds its lines on the next delivery, and the batch dedupe marker keeps them from appending twice. Requeues are capped at MAX_REQUEUES (3, i.e. four deliveries); beyond that the message is dead-lettered on purpose via PermanentFailure, so a persistently stuck lock cannot loop a message forever. A failed requeue enqueue surfaces as a RuntimeException like the existing marker-save failures, instead of losing the callback silently. The envelope gains an attempts field (default 0) that only the worker raises; the ingest endpoint and payloads already in queues are unchanged. Within one worker process log callbacks keep batching at appwrite#15881's gate and no longer contend with each other at all. Two e2e regression tests cover the contention path: one holds the deployment lock while a real build streams its callbacks and asserts the build completes after the release, the other holds the lock past the cap and asserts a callback dead-letters only after carrying the cap's requeue count.
2d4f416 to
072450a
Compare

What does this PR do?
Fixes #15745.
The jobs worker applies every state callback (artifact/exit/complete) under a per-deployment distributed lock, waiting at most
LOCK_TIMEOUT(10 s) for it. With more than oneappwrite-worker-jobscontainer — the normal production layout — state callbacks race for that lock, and the loser throwsUtopia\Lock\Exception\Contention. The queue treats any non-PermanentFailureexception as retryable, but on the Redis broker (the self-hosted default) a rejection is final: the message is dead-lettered toutopia-queue.failed.v1-jobsand never retried. The jobs-service does not resend either, because the ingest endpoint already answered204.Consequences reported in #15745: deployments stuck in
buildingwhen a lost exit/complete callback would have finalized them, missing build log lines when a contended log batch dead-lettered, and a failed list that grows by hundreds of entries per deploy day. #15881 already batches log callbacks behind a per-process gate so they no longer contend within one worker process — but the gate leader still races for the shared lock against state callbacks and other workers' leaders, so cross-process contention could still dead-letter log batches.This PR makes any contended callback requeue itself instead of dead-lettering:
src/Appwrite/Platform/Modules/Functions/Workers/Jobs.phpContentionat both contended sites — the state-callback$locks(...)call in the action body, and the gated log batch leader's$locks(...)call insidelog()(via a shared privaterequeueCallback()helper) — exceptions from the callback body keep their current semantics;publisherForJobs(existing resource) back ontov1-jobs, landing at the tail of the queue — the queue itself provides the spacing between attempts, and each attempt waits up toLOCK_TIMEOUTagain; a requeued log callback re-adds its lines on the next delivery, and the batch's dedupe marker keeps them from appending twice;MAX_REQUEUES = 3, i.e. 4 deliveries); beyond that the message is dead-lettered on purpose viaPermanentFailure, so a persistently stuck lock cannot loop a message forever;RuntimeExceptionsurfaces it (mirrors the existing marker-save failure handling) instead of losing the callback silently.src/Appwrite/Event/Message/Jobs.phpattemptsfield (default 0) to the envelope, round-tripped intoArray()/fromArray(); the ingest endpoint is unchanged, payloads already in queues stay compatible.testContendedCallbacksOfALockedBuildAreRequeuedAndApplied— holds the deployment lock while a real build streams its callbacks, releases, and asserts the build still completes: on a worker without the requeue the exit/complete callbacks dead-letter on their first contention and the deployment never completes;testALockHeldBeyondTheRequeueCapDeadLettersCallbacks— holds the lock past the requeue cap and asserts a state callback dead-letters only after carrying the cap's requeue count (attempts >= 3), and that the deployment never becomes ready.No API surface changes; no new dependencies.
Test Plan
vendor/bin/pint --config pint.json --teston all changed files — PASS (PSR-12, repo config).vendor/bin/phpstan analyse -c phpstan.neonon all changed files — 0 errors.test tests/e2e/Services/Functionsagainst the local dev stack (docker compose up -d), which deploys real functions through the executor and exercises the jobs worker callback path end-to-end — 101 tests, all green (was 99 before this PR; +2 regression tests).Related PRs and Issues
building#15745jobs-event-*) in Redis without TTL #15746, 🐛 Bug Report: Failed queue jobs never expire and there is no way to purge them #15747Checklist