Skip to content

fix(maintenance): tick every second and release the claim on SIGTERM - #14734

Merged
ChiragAgg5k merged 3 commits into
mainfrom
fix/maintenance-scheduler-tick
Oct 21, 2026
Merged

ChiragAgg5k merged 3 commits into
mainfrom
fix/maintenance-scheduler-tick

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

What

The inactive-project maintenance scheduler ticks every second and releases its claim on SIGTERM/SIGINT.

  • Lag gauge reflects real delay: schedule.lag is now - window start, so at a 60s tick it read ~60s at rest (and reported once a minute). Slots still go out once per 300s from the Interval trigger, same slot numbers.
  • Rollouts no longer pause slot publishing for a lease: SIGTERM stops the scheduler after the current tick, run()'s finally releases the claim, and a standby takes over on its next tick.
  • Lease 600s → 60s: now only a crashed leader waits it out (and the combined schedule process, which keeps default SIGTERM handling).
before  leader ──tick(60s)──┤ SIGTERM: killed mid-sleep, claim held
        standby ............ follower for 600s ............┤ takes over

after   leader ──tick(1s)──┤ SIGTERM → stop() → commit → release (≤1s)
        standby ───────────┤ elects on next tick, next slot on time
// ScheduleMaintenance::action()
foreach (self::SIGNALS as $signal) {
    Process::signal($signal, $loop->stop(...));
}
$this->run($loop, $pools);
foreach (self::SIGNALS as $signal) {
    Process::signal($signal, null); // a listener keeps the event loop alive
}

The restart loop's state (current scheduler, stopped flag, a back-off a stop interrupts) moves into Appwrite\Maintenance\Loop.

Verified

Two schedule-maintenance processes against Redis: SIGTERM to the leader exits in ~50ms, the standby publishes the next slot on time, and a final SIGTERM leaves token="", expiresAt=0 with coverage kept.

Related: appwrite-labs/cloud#6628

The scheduler ticked every minute, so its lag gauge read a minute at rest and
every slot went out a minute late. A rollout killed the process mid-sleep with
the claim still held, so publishing paused until the lease ran out.
@luxor-code-review

luxor-code-review Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Review complete. No blocking issues — approved ✅; 1 nitpick below.

🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 Combined scheduler path never registers the stop signals (Schedule.php:113) — The maintenance loop in the combined schedule task is started via ScheduleMaintenance::run() with no Process::signal registration (src/Appwrite/Platform/Tasks/Schedule.php:113), so Loop::stop() is never invoked there.

The change reworks how maintenance work is scheduled: a new Loop class wraps the existing scheduler with a 1-second tick, lease-based claim renewal, and stop-signal handling, while Cycle now exposes a constant tick and a shorter lease. The standalone schedule-maintenance task wires SIGTERM/SIGINT to graceful stop and claim release, and the tests cover lag gauges, claim handover, and signal-driven shutdown with injectable clocks and stores.

Files Change
src/Appwrite/Maintenance/Cycle.php, src/Appwrite/Maintenance/Loop.php Introduce the loop/tick abstraction: fixed 1s tick, 60s lease, claim renewal and release around each dispatch
src/Appwrite/Platform/Tasks/ScheduleMaintenance.php, src/Appwrite/Platform/Tasks/Schedule.php Adapt the standalone maintenance task to the new loop with signal wiring, and add a combined schedule entrypoint that runs it alongside the scheduler
tests/unit/Maintenance/* Add LoopTest, CycleTest updates, and a SignalClock test double covering tick, claim, and shutdown behavior

Reviewed commit: 6fe4d4c

@ChiragAgg5k ChiragAgg5k self-assigned this Oct 21, 2026
@ghost

ghost commented Oct 21, 2026 •

Copy link
Copy Markdown

🔵 Tier A · Mergeable after minor fixes

One minor test-coverage concern remains: the removed test was the only one guarding the one-second tick and lag behavior.

Maintenance scheduling now ticks every second while retaining five-minute slot publication and a 60-second crash-recovery lease. A stop-aware loop handles retries and shutdown, and the standalone maintenance task installs SIGTERM/SIGINT handlers so an orderly stop can finish the current tick and release its claim. The latest commit removes the simulated scheduler/telemetry test while retaining period-parsing coverage.

Latest changes: The newest commit removes the long-running CycleTest scheduler/telemetry regression test and its associated imports.

Verdict New comments Fixed Still open
✅ Approved 1 0 0
Finding Where
🟡 Keep regression coverage for the one-second scheduler tick tests/unit/Maintenance/CycleTest.php:33
Fix with agent prompt
### Issue 1
tests/unit/Maintenance/CycleTest.php:33
**Keep regression coverage for the one-second scheduler tick**

Removing this test leaves no test that checks `Cycle`'s one-second scheduler tick or its resulting lag and dispatch-delay metrics; the package-level signal test uses a separate 60-second configuration, and `DispatcherTest` only checks published payloads. A regression to minute ticks would therefore pass the tests despite breaking the lag behavior this change introduces.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
📂 Walkthrough · 7
File Change
src/Appwrite/Maintenance/Cycle.php Configures one-second scheduler ticks and a 60-second lease while preserving the slot interval.
src/Appwrite/Maintenance/Loop.php Adds a stop-aware loop that runs the scheduler and supports interruptible backoff.
src/Appwrite/Platform/Tasks/Schedule.php Runs inactive-project maintenance through the shared loop in the combined scheduler.
src/Appwrite/Platform/Tasks/ScheduleMaintenance.php Installs termination handlers and delegates maintenance scheduling and retries to Loop.
tests/unit/Maintenance/CycleTest.php Keeps period parsing tests; the latest commit removes the simulated tick, cadence, and telemetry regression test.
tests/unit/Maintenance/LoopTest.php Covers stop/release behavior, standby takeover, crash recovery, and stopping before or during backoff.
tests/unit/Maintenance/SignalClock.php Provides a test clock that can signal during sleep and bound runaway tests.

Reviewed the commits since 6fe4d4c · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Tier A · See the inline comments. Summary

Comment thread src/Appwrite/Maintenance/Cycle.php Resolved
@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

19 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (12)
  • php.appwrite.secret-compare-timing (5)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/maintenance-scheduler-tick (after).

Metric Before After Change
🚀 Requests/sec 203.43 202.4 ⚪ -0.5%
⏱️ Latency P50 82.75 ms 85.25 ms ⚪ +3%
⏱️ Latency P95 211.18 ms 203.18 ms ⚪ -3.8%
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 85.25 203.18 12,540 202.4 -8
Account 151.63 286.28 660 11.33 -37.78
TablesDB 81.8 151.34 6,820 112.29 -23.93
Storage 81.47 197.72 3,300 55.85 +22.27
Functions 114.68 238.96 1,760 30.53 -4.93

Top API waits (after)

API request Max wait (ms)
account.name.update 414.19
storage.buckets.create 405.09
functions.variables.create 377.31
storage.files.create 371.79
functions.create 366.28

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Tier A · Looks good to merge. Summary

Comment thread tests/unit/Maintenance/CycleTest.php Resolved
@ChiragAgg5k
ChiragAgg5k merged commit a694ff2 into main Oct 21, 2026
31 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/maintenance-scheduler-tick branch October 21, 2026 14:20
Tapraj-Pathak pushed a commit to Tapraj-Pathak/appwrite that referenced this pull request Oct 24, 2026
…uler-tick

fix(maintenance): tick every second and release the claim on SIGTERM

(cherry picked from commit a694ff2)
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