Skip to content

fix(builds): release deployment lock during pre-finalization work - #13685

Open
loks0n wants to merge 1 commit into
mainfrom
codex/build-finalization-lock
Open

loks0n wants to merge 1 commit into
mainfrom
codex/build-finalization-lock

Conversation

@loks0n

@loks0n loks0n commented Sep 14, 2026

Copy link
Copy Markdown
Member

What does this PR do?

Split successful build finalization into three phases in the same queue job: apply callbacks and validate under the deployment lock, run an idempotent beforeFinalize() hook outside the lock, then reacquire the lock and finalize from a fresh deployment record. The queue job returns only after finalization. Cancellation or another callback's terminal outcome wins when the lock is reacquired.

Cloud uses the hook for edge distribution. Production edge calls took 74–82 seconds while the deployment lock lease is 30 seconds. Moving that wait outside the lock avoids holding an expired lease or blocking log callbacks. The hook returns progress logs, which are appended to the latest record under the finalization lock.

Replace the unused deferred-callback hooks with this single hook. Replaying an already-applied terminal callback still attempts readiness, so a failed hook or second lock acquisition can be retried without another queue protocol. Usage, realtime, and terminal notifications stay under the lock. Production code decreases by 18 lines.

Cloud companion: https://github.com/appwrite-labs/cloud/pull/5836 (replaces its initial queued-event approach). Production investigation.

Test Plan

Validated through Cloud's registered worker callback using this CE source:

  • A log callback runs during distribution; its stored log survives finalization.
  • Cancellation and a competing successful callback are preserved; usage/screenshots publish once.
  • The original callback can retry after a distribution failure or failure to acquire the finalization lock.
  • Cloud distribution/activation/test-hygiene suites: 10 tests, 103 assertions.
  • Replacing this CE source with the prior version makes the regression test fail; restoring it passes.
  • Changed-file PHPStan level 4, Pint and Rector checks pass.

No Kubernetes/edge E2E or staging drill has been run. Tests for the Cloud extension are in the companion PR; this PR adds no worker unit tests to CE. Stage both PRs together, exercise a slow site distribution plus cancellation and callback redelivery, and check stored logs, terminal status and usage. Existing Redis failed-job recovery still controls when failed messages are retried.

Checklist

  • QA plan was made for both happy and failure flows
  • Local QA was performed (focused automated checks)
  • Branch has been successfully deployed to stage
  • Staging QA was performed (in frankfurt, and non-frankfurt region)

@loks0n loks0n self-assigned this Sep 14, 2026
@greptile-apps

greptile-apps Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

The implementation appears safe to merge, but the concurrency-sensitive finalization protocol should receive observable worker-level regression coverage.

Fix All in Claude CodeFindings

  1. P2 Missing Concurrency Coverage ▶
Fix with agent prompt
### Issue 1
src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php:166-177
This new two-lock finalization flow has no CE worker-level coverage. Existing Jobs tests only check queue configuration, so regressions involving cancellation during `beforeFinalize()`, callback redelivery after a hook or lock failure, competing terminal callbacks, or logs written between locks could go unnoticed. Please add observable-behavior tests that coordinate these cases and verify the durable deployment state and one-time side effects rather than mirroring the implementation phases.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

This PR splits successful deployment finalization into a locked preparation phase, an unlocked idempotent pre-finalization hook, and a second locked finalization phase using a freshly read deployment.

  • Preserves cancellation and competing terminal outcomes when the lock is reacquired.
  • Replaces deferred/custom callback hooks with beforeFinalize().
  • Centralizes realtime, usage, webhook, and function-event publication.
  • Lacks CE behavioral coverage for the newly introduced concurrency and retry protocol.

Reviews (1) · Last reviewed commit: "fix(builds): release deployment lock dur..."

Comment on lines +166 to +177
$locks('jobs-deployment:' . $deploymentId, self::LOCK_TTL, function () use ($dbForProject, $dbForPlatform, $project, $deploymentId, $size, $logs, $publisherForScreenshots, $vcsFactory, $platform, $bus, $usage, $publisherForUsage, $queueForRealtime, $queueForEvents, $queueForWebhooks, $publisherForFunctions): void {
$deployment = $dbForProject->getDocument('deployments', $deploymentId);
if ($deployment->isEmpty() || $deployment->getAttribute('status') === 'canceled') {
if ($deployment->isEmpty() || \in_array($deployment->getAttribute('status'), ['ready', 'failed', 'canceled'], true)) {
return;
}

$statusBefore = $deployment->getAttribute('status');
$durationBefore = $deployment->getAttribute('buildDuration');
$deployment->setAttribute('buildLogs', $deployment->getAttribute('buildLogs', '') . $logs);
$deployment = $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, true, '', $publisherForScreenshots, $vcsFactory, $platform, $bus, $size);
$this->publishUpdate($dbForProject, $project, $deployment, $statusBefore, $durationBefore, $usage, $publisherForUsage, $queueForRealtime, $queueForEvents, $queueForWebhooks, $publisherForFunctions);
}, self::LOCK_TIMEOUT);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Missing Concurrency Coverage

This new two-lock finalization flow has no CE worker-level coverage. Existing Jobs tests only check queue configuration, so regressions involving cancellation during beforeFinalize(), callback redelivery after a hook or lock failure, competing terminal callbacks, or logs written between locks could go unnoticed. Please add observable-behavior tests that coordinate these cases and verify the durable deployment state and one-time side effects rather than mirroring the implementation phases.

Knowledge Base Used:

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php
Line: 166-177

Comment:
**Missing Concurrency Coverage**

This new two-lock finalization flow has no CE worker-level coverage. Existing Jobs tests only check queue configuration, so regressions involving cancellation during `beforeFinalize()`, callback redelivery after a hook or lock failure, competing terminal callbacks, or logs written between locks could go unnoticed. Please add observable-behavior tests that coordinate these cases and verify the durable deployment state and one-time side effects rather than mirroring the implementation phases.

**Knowledge Base Used:**
- [Functions and execution processing](https://app.greptile.com/appwrite/-/custom-context/knowledge-base/appwrite/appwrite/-/docs/functions-execution-api.md)
- [Worker job families](https://app.greptile.com/appwrite/-/custom-context/knowledge-base/appwrite/appwrite/-/docs/worker-job-families.md)
- [Background processing and scheduling](https://app.greptile.com/appwrite/-/custom-context/knowledge-base/appwrite/appwrite/-/docs/background-processing.md)

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Fix in Claude Code Fix in Codex

@github-actions

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → codex/build-finalization-lock (after).

Metric Before After Change
🚀 Requests/sec 220.49 221.09 ⚪ +0.3%
⏱️ Latency P50 79.59 ms 79.68 ms ⚪ +0.1%
⏱️ Latency P95 184.22 ms 182 ms ⚪ -1.2%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 79.68 182 13,680 221.09 -2.22
Account 148.25 269.06 720 12.37 -2.62
TablesDB 76.56 139.77 7,440 123 -1.43
Storage 74.75 170.68 3,600 61.36 +7.89
Functions 111.31 213.26 1,920 33.39 -14.41

Top API waits (after)

API request Max wait (ms)
account.name.update 440.02
account.prefs.update 401.64
storage.files.create 363.79
storage.buckets.create 359
functions.variables.update 345.53

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