Skip to content

fix(maintenance): start the loop on the next interval step - #14885

Merged
ChiragAgg5k merged 4 commits into
mainfrom
fix/maintenance-start-grid
Oct 23, 2026
Merged

ChiragAgg5k merged 4 commits into
mainfrom
fix/maintenance-start-grid

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Maintenance's first run waited for the next _APP_MAINTENANCE_START_TIME, so with _APP_MAINTENANCE_INTERVAL under a day every restart could delay the loop by up to ~24h. The first run now lands on the next step of the grid start + k × interval.

$anchor = new \DateTime($now->format("Y-m-d $start"), $now->getTimezone());
$elapsed = $now->getTimestamp() - $anchor->getTimestamp(); // negative before the start time
$steps = (int) \floor($elapsed / $interval) + 1;

return $anchor->setTimestamp($anchor->getTimestamp() + $steps * $interval); // 0 < delay <= interval

Start time 12:00:

Now Interval Before After
14:20 3600 tomorrow 12:00 15:00
14:20 28800 tomorrow 12:00 20:00
14:20 86400 tomorrow 12:00 tomorrow 12:00
11:30 3600 12:00 12:00
15:00 3600 tomorrow 12:00 16:00

Daily runs are unchanged. The computation lives in Appwrite\Maintenance\Grid, covered by tests/unit/Maintenance/GridTest.php.

Related: appwrite-labs/cloud#6628

Moves the first-run computation into Maintenance::next() unchanged and adds
cases for sub-daily intervals. The hourly and 8-hourly cases fail: a restart
waits for the next daily start time instead of the next interval step.
@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

6 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (3)
  • php.appwrite.secret-compare-timing (1)
  • 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 23, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/maintenance-start-grid (after).

Metric Before After Change
🚀 Requests/sec 219.73 220.88 ⚪ +0.5%
⏱️ Latency P50 77.43 ms 78.96 ms ⚪ +2%
⏱️ Latency P95 187.66 ms 181.69 ms ⚪ -3.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 78.96 181.69 13,737 220.88 -5.97
Account 149.24 288.58 723 12.06 +17.75
TablesDB 77.78 148.83 7,471 121.66 +8.95
Storage 69.91 165.37 3,615 60.65 -28.8
Functions 107.95 201.63 1,928 33.14 -21.1

Top API waits (after)

API request Max wait (ms)
account.name.update 459.9
account.prefs.update 391.13
storage.buckets.create 368.67
tablesdb.rows.list 352.55
functions.create 344.11

The first run waited for the next start time, so with an interval under a
day every restart could hold the loop back by up to a day. It now runs at
the next point of start time + k x interval, at most one interval away.
Daily intervals keep the same schedule.
@ChiragAgg5k ChiragAgg5k self-assigned this Oct 23, 2026
@ChiragAgg5k
ChiragAgg5k marked this pull request as ready for review October 23, 2026 09:44
@luxor-code-review

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

Copy link
Copy Markdown

Review complete. 🟡 2 medium

💬 Inline comments (1)

  • 🟡 next() returns a past time when now precedes the anchor by more than one interval — Maintenance.php:117
📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to

🟡 Medium — Non-positive _APP_MAINTENANCE_INTERVAL crashes or busy-loops maintenance · Maintenance.php:45–52 · unchanged line

// src/Appwrite/Platform/Tasks/Maintenance.php
45	        $interval = (int) System::getEnv('_APP_MAINTENANCE_INTERVAL', '86400'); // 1 day
46	        $cacheRetention = (int) System::getEnv('_APP_MAINTENANCE_RETENTION_CACHE', '2592000'); // 30 days
47	        $schedulesDeletionRetention = (int) System::getEnv('_APP_MAINTENANCE_RETENTION_SCHEDULES', '86400'); // 1 Day
48	        $jobInitTime = System::getEnv('_APP_MAINTENANCE_START_TIME', '00:00'); // (hour:minutes)
49	
50	        $now = new \DateTime();
51	        $now->setTimezone(new \DateTimeZone(date_default_timezone_get()));
52	        $next = self::next($now, $jobInitTime, $interval);

_APP_MAINTENANCE_INTERVAL is cast to int and passed unvalidated to Maintenance::next(), which computes \floor($elapsed / $interval) (src/Appwrite/Platform/Tasks/Maintenance.php:45,52,117). With interval = 0 this throws DivisionByZeroError and the maintenance worker crash-loops at startup; with a negative interval, floor($elapsed / $interval) + 1 is ≤ 0, so the returned $next is at or before $now, giving a zero/negative $delay that makes Console::loop re-run immediately every iteration — a busy loop re-enqueueing all maintenance work (project scans, certificate renewal) as fast as the loop can run.


The change replaces the maintenance task's fixed daily cadence with an interval-driven schedule anchored to a configurable start time, and adds unit tests for the new next() computation. Two edge cases in the arithmetic remain: a now-time more than one interval before the anchor yields a past next-run, and a non-positive interval env value crash-loops or busy-loops the worker.

Files Change
src/Appwrite/Platform/Tasks/Maintenance.php Adds next() computing the next maintenance run from _APP_MAINTENANCE_START_TIME anchor and _APP_MAINTENANCE_INTERVAL, wired into Console::loop delay.
tests/unit/Platform/Tasks/MaintenanceTest.php New unit tests covering the next-run calculation for the anchor/interval grid.

Reviewed commit: a69535d

@ghost

ghost commented Oct 23, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

Maintenance now calculates the first loop run as the next strictly future point on the configured start-time and interval grid, avoiding long restart delays for subdaily intervals. The calculation lives in Grid, and unit tests cover times before, between, and exactly on scheduled steps.

Latest changes: Adds regression cases for hourly and daily schedules when the current time is several hours before the configured start time.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 3
File Change
src/Appwrite/Maintenance/Grid.php Calculates the next maintenance run on the configured start-time and interval grid.
src/Appwrite/Platform/Tasks/Maintenance.php Uses the grid calculation to set the maintenance loop's initial delay.
tests/unit/Maintenance/GridTest.php Covers grid calculations across hourly, eight-hour, and daily schedules, including the newly added before-start cases.

Reviewed the commits since a62c276 · 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 · Looks good to merge. Summary

Unit tests cover src libraries, not CLI tasks, so the first-run computation
lives in Appwrite\Maintenance\Grid and the task only calls it.

@luxor-code-review luxor-code-review Bot 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.

This PR reworks the maintenance scheduler's next-run calculation to anchor runs to _APP_MAINTENANCE_START_TIME and lets _APP_MAINTENANCE_INTERVAL configure the cadence, plus a unit test.

Key findings

  • 🟡 next() returns a past time when now precedes the anchor by more than one interval — Maintenance.php:117
  • 🟡 Non-positive _APP_MAINTENANCE_INTERVAL crashes or busy-loops maintenance — Maintenance.php:45

@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 S · Looks good to merge. Summary

Grid steps before the start time land on earlier points of the same grid,
so 10:00 with an hourly 12:00 grid runs at 11:00, not in the past.
@ChiragAgg5k
ChiragAgg5k merged commit de0677d into main Oct 23, 2026
121 of 123 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/maintenance-start-grid branch October 23, 2026 10:22
Tapraj-Pathak pushed a commit to Tapraj-Pathak/appwrite that referenced this pull request Oct 24, 2026
…-grid

fix(maintenance): start the loop on the next interval step

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