feat(quota): add the CI minutes ledger, subjects and Actions accrual #100
No reviewers
Labels
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
codefloe/forgejo-codefloe!100
Loading…
Reference in a new issue
No description provided.
Delete branch "t3code/implement-issue-93"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Implements #93, phase 1 of the CI build minutes quota work.
Builds the accounting layer and makes CI minutes visible.
Nothing is enforced in this phase: rules over the new subjects can be created and evaluate correctly, but no code path calls the evaluator to deny anything.
The point is to get numbers that can be trusted before anything blocks on them.
What changed
Schema
New
models/codefloe/quota_cipackage holding two tables, created by Codefloe migration v10.quota_ci_usageis the per-owner monthly aggregate, unique on(owner_id, period, source, class), and is what quota rules are evaluated against.quota_ci_recordis the individual billed intervals it was built from, unique on(source, external_ref).That second index is what makes accrual idempotent, so a producer that retries a report cannot double charge.
Limit subjects
models/quota/limit_subject_codefloe.godeclarestime:ci:all,time:ci:actionsandtime:ci:externalat offset 1000, well clear of upstream's enum, and registers their reprs and parent relations frominit().It also gives every subject a unit.
A rule sums its subjects, so a rule mixing
size:andtime:would add bytes to seconds and compare the result against one limit; such a rule is now rejected at rule creation, at rule edit, and when a group is created with inline rules.Accrual
services/codefloe/quota_ci.AccrueTaskwrites the record and the aggregate increment in one transaction, keyedtask:<id>, with the class taken from the runner's labels.It is called from both places an Actions task reaches a terminal state,
UpdateTaskByStateandStopTask, so a zombie task stopped byStopZombieTasksis still billed for the time it ran.CancelAbandonedJobsneeds no hook: a job that was never picked up has no task and consumed no minutes.Reading usage
GetUsedForUsernow also fills a consumed-time half ofUsed, from the ledger plus an in-flight estimate ofSUM(now - started)over the owner's running tasks.The estimate matters.
Without it an owner running fifty eight-hour jobs in parallel reads as near-zero usage until they all finish, which is exactly the window phase 2 would need to be right about.
With it, no heartbeat writer is needed.
API
GET /api/v1/user/quota/ciand/api/v1/orgs/{org}/quota/cireturn used seconds per source and class, the applicable limits, the period start and the reset time.GET /api/v1/user/quota/ci/recordsand the org equivalent are paginated and ordered by seconds descending.These are separate endpoints rather than fields on
QuotaInfo, which keepsmodules/structs/quota.goandservices/convert/quota.gountouched.UI
A CI minutes card on the storage overview, showing used against limit, the reset date, the per-source split, and the top repositories for the period.
The existing storage card is now filtered to byte-counted rules, so a
time:rule is never rendered as a size.Config
Retention is applied by a nightly cron task that drops records past the window and keeps the aggregates, which are small and are what history is read from once the detail is gone.
Notes for the reviewer
Two deviations from the issue worth a look.
models/quota/used.gocould not be left untouched.Usedis a struct, so making time rules evaluate correctly needed a field on it.The file carries three small edits: the
Timefield, a tail call inCalculateFor, and one call inGetUsedForUser.Everything else lives in new
*_codefloe.gofiles.The issue says limits are expressed as durations in config but names no key, so I added
[quota.ci.default] TOTALas a duration, mirroring[quota.default] TOTAL.It is the fallback reported when no quota group gives the owner a
time:rule.If limits are meant to come only from quota rules, that key can be dropped.
Checklist
Tests for Go changes
*_test.gofor unit tests.tests/integrationdirectory if it involves interactions with a live Forgejo server.make fmt,make generate-swagger,make swagger-validate,make lint-locale,make lint-editorconfig, the deadcode check, and the unit and integration suites for every touched package.make lint-gocould not run in this environment: the pinned golangci-lint cannot read Go 1.27 export data.Documentation
[quota.ci]section is documented by its comments inmodules/setting/codefloe_quota_ci.go; user-facing docs belong with phase 2, when the feature starts enforcing.Release notes
d961f92d289433477bef9433477bef65e6e5a002pat-s referenced this pull request from codefloe/docs2026-08-28 12:02:21 +00:00