Skip to content

fix(maintenance): spend the renewal budget on scheduled jobs, not scanned rows - #13572

Closed
HarshMN2345 wants to merge 6 commits into
claude/certificates-reconcile-provider-statusfrom
claude/maintenance-renewal-budget
Closed

HarshMN2345 wants to merge 6 commits into
claude/certificates-reconcile-provider-statusfrom
claude/maintenance-renewal-budget

Conversation

@HarshMN2345

@HarshMN2345 HarshMN2345 commented Sep 9, 2026 •

Copy link
Copy Markdown
Member

Stacked on #13571 (which is on #13570) — they share the test fixtures. Merge in order; GitHub retargets automatically. The diff shown here is only this change.

What does this PR do?

Two bugs in Maintenance::renewCertificates(), both in how it picks what to renew.

1. Skipped rows eat the renewal budget. On main:

$documents = $dbForPlatform->find('certificates', [
    Query::lessThan('attempts', 5),
    Query::isNotNull('renewDate'),
    Query::lessThanEqual('renewDate', $time),
    Query::limit(200), // Limit 200 comes from LetsEncrypt
]);
foreach ($documents as $certificate) {
    ...
    if ($rule->isEmpty() || $rule->getAttribute('region') !== $appRegion) {
        continue;
    }

find(limit 200) takes the first 200 due certificates, then the loop drops the ones this region doesn't own. The certificates table is shared across regions and has no region column, so on a multi-region deployment the first 200 due rows can all belong to another region — this region schedules nothing, and its own due renewals wait for the next daily run, where the same thing happens again. Nothing in the logs says so: it prints "Found 200 certificates for renewal, scheduling jobs" and schedules zero.

The fix pages through due certificates with iterate() (limit becomes the page size, ordered by $sequence so the cursor is stable) and counts the 200 cap on jobs actually enqueued, breaking once it's reached. The Let's Encrypt headroom is preserved; it's just no longer consumed by rows that never produce an order.

2. The wrong certificate gets renewed. The scan matched a rule by domain alone. When a domain is removed and re-added, or the worker creates a fresh certificate document for a rule, the old certificate row stays behind with its renewDate still due. main finds it, finds the rule by domain, and queues a renewal — for a certificate the rule no longer references. The extra guard rule.certificateId === certificate.$id makes the rule the authority on which certificate it renews.

The attempts < 5 cap, the region check and the Appwrite-owned auto-issue opt-out are unchanged.

Test Plan

tests/e2e/General/Certificates/MaintenanceTest.php runs the real Maintenance task with type: 'trigger' against a throwaway PostgreSQL schema and a capturing publisher:

  • Budget: 201 due certificates whose rules live in region other, created first, then 201 due renewals in default. main schedules 0 — the other region's rows fill the page. This PR schedules exactly 200, all of them default-region renewals. (200 is the Let's Encrypt budget — 300 orders per three hours with 100 held back for new domains — not an internal constant.)
  • Stale certificate: a rule referencing a certificate with a future renewDate, plus a leftover certificate for the same domain that is due. main queues a renewal for the leftover; this PR queues nothing.

trigger also runs the other maintenance steps (project sweep, cache/schedule/CSV cleanup), which is why the fixture's projects stub gains accessedAt; those steps only enqueue onto the capturing publisher.

$ composer lint src/Appwrite/Platform/Tasks/Maintenance.php tests/e2e/General/Certificates/
{"tool":"pint","result":"passed"}

Runs in Tests / E2E / General; skips locally without PostgreSQL.

Related PRs and Issues

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs? — no API metadata changed.

🤖 Generated with Claude Code

https://claude.ai/code/session_01MeytoiisEusFMCU44PDYav

…nned rows

The renewal scan fetched the first 200 due certificates and then skipped
the ones whose rule is missing, lives in another region, or does not
apply. Every skipped row still counted against the 200, so on a
multi-region deployment the rows another region owns could fill the
page and starve the renewals this region should have queued. The same
scan also matched a certificate by domain alone, so a rule that had
moved on to a new certificate kept renewing the one it left behind.

Page through due certificates in sequence order and stop once 200 jobs
are actually queued, and only renew the certificate the rule currently
references.
@greptile-apps

greptile-apps Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; no outstanding correctness, security, or repository-rule violation remains.

Summary

The PR changes certificate maintenance to page through all due certificates while charging the renewal budget only for jobs actually scheduled. It also verifies that each rule still references the certificate being renewed.

  • Uses stable $sequence pagination and stops after scheduling 200 eligible renewals.
  • Skips certificates belonging to other regions, stale certificate records, and existing auto-issue exclusions without consuming the scheduling budget.
  • Adds end-to-end coverage for regional filtering and stale certificate references.
  • Extends the PostgreSQL fixture with the project attribute required to exercise the public maintenance entrypoint.

Reviews (8) · Last reviewed commit: "Merge branch 'claude/certificates-reconc..."

@github-actions

github-actions Bot commented Sep 9, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing claude/certificates-reconcile-provider-status (before) → claude/maintenance-renewal-budget (after).

Metric Before After Change
🚀 Requests/sec 484.42 483.54 ⚪ -0.2%
⏱️ Latency P50 35.8 ms 35.7 ms ⚪ -0.3%
⏱️ Latency P95 84.46 ms 85.09 ms ⚪ +0.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 35.7 85.09 29,640 483.54 +0.64
Account 62.58 121.13 1,560 25.97 +1.52
TablesDB 34.08 66.68 16,120 265.31 +0.24
Storage 33.15 82.4 7,800 130.23 -0.26
Functions 50.82 99.87 4,160 70.16 +0.81

Top API waits (after)

API request Max wait (ms)
account.name.update 265.97
storage.buckets.create 224.59
account.prefs.update 223.93
functions.variables.update 215.36
storage.files.create 196.26

…ude/maintenance-renewal-budget

# Conflicts:
#	tests/e2e/General/Certificates/Database.php
@HarshMN2345
HarshMN2345 force-pushed the claude/certificates-reconcile-provider-status branch from 55392c8 to ff91d29 Compare September 9, 2026 12:17
@HarshMN2345
HarshMN2345 force-pushed the claude/maintenance-renewal-budget branch from 28faa65 to 60e0899 Compare September 9, 2026 12:17

Copy link
Copy Markdown
Member Author

Tests / E2E / Antivirus (dedicated) is red on 60e08999ed, but not because of this PR.

  • Failure: StorageCustomServerTest::testCreateBucketFileChunkedUploadWithAntivirus — Failed asserting that 403 matches expected 201.
  • clamd log in the same second: INSTREAM: Size limit reached, (requested: 1930551912, max: 98304000) — the scan stream carried a garbage chunk length (~1.9 GB for a small test file), clamd refused the stream, and the upload was rejected as if infected.
  • This tree is byte-identical to 28faa65bf8, which passed the same check 90 minutes ago (run 34343133816). The diff here is Maintenance.php and the certificate tests; nothing in the storage or antivirus path.
  • The same signature hit fix(certificates): retry failed issuance under a lease and bound the attempts #13573 earlier today at dc67ed76 and passed on the next run.

There is no fix in the repo to port. I'll re-run the failed job once the run completes; if it fails the same way twice, the INSTREAM framing gets its own PR rather than widening this one.


Generated by Claude Code

HarshMN2345 pushed a commit that referenced this pull request Sep 17, 2026
Picks up main via #13572 -> #13571 -> #13570 so CI runs
against current main.

Conflict in Certificates.php: main added an inline notifyError() call in
catch and gave notifyError() a sixth $console parameter. This branch
defers that email to the finally block, which sends only once the lease
is confirmed still ours, so a stale worker cannot email an admin about a
failure that a newer retry has already superseded. Kept the deferred
form, dropped the inline call, and carried main's new parameter into the
deferred call site -- git would have merged the signature change without
touching the call.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MeytoiisEusFMCU44PDYav
@HarshMN2345
HarshMN2345 force-pushed the claude/certificates-reconcile-provider-status branch from bdb52ee to 51abda3 Compare September 18, 2026 09:06
@HarshMN2345
HarshMN2345 force-pushed the claude/maintenance-renewal-budget branch from 6647e8b to 01fdcac Compare September 18, 2026 09:06
HarshMN2345 added a commit that referenced this pull request Sep 18, 2026
Picks up main via #13572 -> #13571 -> #13570 so CI runs
against current main.

Conflict in Certificates.php: main added an inline notifyError() call in
catch and gave notifyError() a sixth $console parameter. This branch
defers that email to the finally block, which sends only once the lease
is confirmed still ours, so a stale worker cannot email an admin about a
failure that a newer retry has already superseded. Kept the deferred
form, dropped the inline call, and carried main's new parameter into the
deferred call site -- git would have merged the signature change without
touching the call.
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