Skip to content

fix(builds): cap the billed build duration at the build timeout - #13430

Merged
loks0n merged 2 commits into
mainfrom
fix/cap-billed-build-duration-at-timeout
Aug 31, 2026
Merged

loks0n merged 2 commits into
mainfrom
fix/cap-billed-build-duration-at-timeout

Conversation

@loks0n

@loks0n loks0n commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

What

Jobs::duration() — the single writer of buildDuration on the jobs-service path — is clamped to _APP_COMPUTE_BUILD_TIMEOUT.

Why

duration() measures wall clock from buildStartedAt, which only the first log callback stamps. A build that never streamed a line has no stamp, so it falls back to the deployment's creation time and measures its entire queue wait. Usage\Build::publish() then bills that value at memory × duration × cpus, and bills failed exactly as it bills ready.

The executor backend could not produce this: it measured microtime() around the build's own execution (Builds.php:170 → :452), so queue wait was structurally unbillable. That path was removed in a82dc6e after the jobs-service landed in d1b1ab9.

Fleet-wide, from projects_usage_events_daily:

month failed build hours billed success hours mean failed build duration
Jun 148.9 725.3 111 s
Jul 273.5 1,434.5 157 s
Aug 1,182.5 899.7 491 s

Mean successful build held at 51.5s throughout. Failed builds went from 17% of billed build compute in June to 57% in August.

One production project shows the shape plainly. Of its 2,311 failed August builds, 2,286 have buildStartedAt = NULL, empty buildLogs apart from Build failed with exit code -1, and durations clustered at 904–912s (the 900s timeout). On s-2vcpu-2gb that is ~1.0 GB-hour billed per build that never ran — 1,869 of its 1,878 billed GB-hours for the month.

The change

Clamp to _APP_COMPUTE_BUILD_TIMEOUT, the same ceiling Deployments hands the jobs-service as timeoutSeconds. No job outlives it, so nothing past it can have been build time. Guarded so a 0 or negative value leaves the measurement alone rather than zeroing every build's duration.

Scope — this is a ceiling, not the whole fix

A starved build still bills the full 900s, so this recovers little for the project above. Deliberately out of scope, as follow-ups:

  • Bill 0 when buildStartedAt is NULL. No log line means no build ran; that is the fix that actually zeroes this class of charge.
  • Cap build fan-out per project. The project above has 77 functions on one monorepo; one push produced ~50 concurrent builds (850 deployments from 17 distinct commits on Aug 14), none of which got a runner inside 900s. Starvation is the cause; billing is the symptom.
  • Reap deployments whose callbacks never arrive. Nothing does today — there are rows still in building and waiting from mid-August.
  • Builds::sendUsage() runs in a finally, so the template hand-off path publishes METRIC_BUILDS + 1 at status processing and the Jobs worker publishes again. Inflates the build count only; buildDuration is still null there.

Test

No automated test. Verified by hand: with _APP_COMPUTE_BUILD_TIMEOUT=900, a deployment created three hours earlier with buildStartedAt null finalizes at buildDuration = 900 (10801 before the change), and the published builds.mbSeconds is 2048 x 900 x 2 rather than 2048 x 10801 x 2.

Pint, PHPStan and tests/unit/{Platform/Workers,Workers,Deployment} pass.

Jobs::duration() measures wall clock from buildStartedAt, which the first
log callback stamps — so a build that never streamed a line falls back to
the deployment's creation time and measures its whole queue wait. That
value is what Usage\Build::publish() bills, at memory x duration x cpus,
and it is billed for 'failed' exactly as for 'ready'.

The executor backend could not produce this: it measured microtime()
around the build's own execution, so queue wait was structurally
unbillable. Since builds moved to the jobs-service the mean billed
duration of a *failed* build has gone 111s (Jun) -> 157s (Jul) -> 491s
(Aug) while successful builds held at ~52s, and failed builds are now 57%
of all billed build compute fleet-wide.

One production project shows the shape plainly: 2,286 of its 2,311 failed
August builds have buildStartedAt NULL, empty buildLogs, "exit code -1",
and durations at 904-912s. On s-2vcpu-2gb that is ~1.0 GB-hour billed per
build that never ran — 1,869 of its 1,878 billed GB-hours for the month.

Clamp to _APP_COMPUTE_BUILD_TIMEOUT, the same ceiling Deployments hands
the jobs-service as timeoutSeconds: no job outlives it, so nothing past it
can have been build time. Guarded so a 0 or negative value leaves the
measurement alone rather than zeroing every build.

This is a ceiling, not the whole fix — a starved build still bills the
full timeout. Billing 0 when buildStartedAt is NULL, and the build fan-out
that starves them, are follow-ups.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@greptile-apps

greptile-apps Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR caps jobs-service build duration at the configured compute build timeout, preventing excessive queue wait from producing billing duration above that ceiling.

  • Computes elapsed duration as before.
  • Applies the positive _APP_COMPUTE_BUILD_TIMEOUT value as an upper bound.
  • Leaves elapsed duration unchanged when the configured timeout is zero or negative.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php Caps persisted and billed build duration using the same timeout configuration supplied to the jobs service; no eligible follow-up defect remains.

Reviews (2): Last reviewed commit: "test(builds): drop the build duration ca..." | Re-trigger Greptile

Removed at request; the cap in Jobs::duration() is unchanged.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@github-actions

github-actions Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/cap-billed-build-duration-at-timeout (after).

Metric Before After Change
🚀 Requests/sec 241.81 244.55 ⚪ +1.1%
⏱️ Latency P50 74.9 ms 73.96 ms ⚪ -1.3%
⏱️ Latency P95 159.83 ms 154.71 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 73.96 154.71 15,105 244.55 -5.12
Account 125.98 255.12 795 13.19 +8.37
TablesDB 75.17 130.71 8,215 134.39 -2.82
Storage 61.11 125.88 3,975 67.3 -4.4
Functions 94.34 183.56 2,120 36.75 -5.6

Top API waits (after)

API request Max wait (ms)
account.get 539.43
functions.variables.update 410.26
tablesdb.rows.create 359.77
account.name.update 352.94
tablesdb.rows.increment 339.58

@loks0n
loks0n merged commit 11e8f83 into main Aug 31, 2026
45 checks passed
NancyWei123 pushed a commit to NancyWei123/appwrite that referenced this pull request Sep 30, 2026
When a build's exit callback carries no measured duration, Jobs::duration()
falls back to wall clock since buildStartedAt. appwrite#13582 removed the clamp
from appwrite#13430 so measured durations past the timeout (termination grace) are
kept, but that also left the fallback unbounded: an exit reported seven
weeks after its build started was billed as 50 days of build compute.

The build's timeout now travels in the job's meta and comes back on its
exit callback. The fallback is capped at that timeout plus the 300s of
headroom Deployments already grants the build's credentials, or at
_APP_COMPUTE_BUILD_TIMEOUT for jobs submitted before this change.
Measured durations are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

2 participants