Skip to content

zebra: trim the request of old finished jobs to their git env vars - #1400

Open
loadez wants to merge 2 commits into
mf/zebra-finished-job-cleanerfrom
mf/zebra-trim-finished-job-request
Open

loadez wants to merge 2 commits into
mf/zebra-finished-job-cleanerfrom
mf/zebra-trim-finished-job-request

Conversation

@loadez

@loadez loadez commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Trim the request of old finished jobs to their git env vars.

Builds on the finished job cleaner (base branch of this PR). Past the same cutoff, the worker replaces a finished job's stored request with a payload that keeps only the SEMAPHORE_GIT_* env vars, in both the current and the legacy shape, which is all the public job API reads after a job finishes. The write happens only when the git vars extracted before and after are identical; otherwise the row is left alone and counted in a finished_job_cleaner.trim_skipped metric. Running jobs are never touched. A new cursor name starts a fresh pass.

Tests: the public job API returns the same git vars before and after the trim in both shapes, create_copy still works on a trimmed job, and a second pass changes nothing.

jobs.private_ssh_key and jobs.agent_auth_token were kept forever.

The debug SSH key is only served while the job is running, so it is
cleared on every transition to finished (finish, stop, force_finish,
bulk_force_finish). force_change is used because bulk_force_finish
loads the job without that column.

The agent auth token is still needed by post-job processing for a while
after the job finishes, so it is cleared later by a new periodic worker,
FinishedJobCleaner (START_FINISHED_JOB_CLEANER, off by default). It
walks finished jobs in (finished_at, id) order, which the existing jobs
index serves, and clears the token and the SSH key of jobs finished
more than FINISHED_JOB_CLEANER_CUTOFF_DAYS (default 3) ago. Each tick
handles at most FINISHED_JOB_CLEANER_BATCH_SIZE rows (default 250) and
sleeps FINISHED_JOB_CLEANER_NAPTIME_MS (default 1000) while there is
work, or FINISHED_JOB_CLEANER_LONGNAPTIME_MS (default 600000) once
caught up. Progress is stored in the new job_cleanup_cursors table, so
a restart resumes where it stopped, and the cursor row is locked with
SKIP LOCKED so only one replica works at a time. Updates are guarded by
the same predicates, so a second pass changes nothing.
Once a job is finished, the only part of jobs.request that is still read
is the SEMAPHORE_GIT_* env vars, which the public job API appends to the
job spec. The commands and the rest of the payload duplicate jobs.spec.

FinishedJobCleaner now also reduces the request of jobs finished before
its cutoff to those env vars (JobRequest.finished_payload/1), kept in
their original shape and under their original key. Jobs that are not
finished, or finished within the cutoff, are not touched. The cursor
name is bumped so the worker makes a new pass from the oldest finished
job.

The trim cannot be undone, so a row is only rewritten when the public
job API would serve exactly the same git env vars from the trimmed
request; other rows are left as they are and counted. A row deleted by
retention while a tick runs is skipped without failing the batch.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-10T13:07:24.549550Z 83562c6 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 83562c6d8a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 110 to 112
case List.last(batch) do
nil -> :ok
{finished_at, id} -> move_cursor(worker.cursor_name, finished_at, id)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Ensure backdated copies cannot fall behind the cursor

When Job.create_copy/2 runs during the initial v2 sweep, it can read an old job before this transaction trims it and insert a new finished row containing the full request while preserving the source's historical finished_at (zebra/lib/zebra/models/job.ex:119,130). Advancing the cursor to the batch's last (finished_at, id) means that copy is permanently skipped whenever its timestamp is earlier than the batch endpoint (or its UUID sorts behind the cursor), so some requests remain untrimmed until another cursor-version reset. Trim the request when creating a copy or use a scan strategy that eventually revisits backdated inserts.

Useful? React with 👍 / 👎.

@loadez
loadez force-pushed the mf/zebra-finished-job-cleaner branch from 3888c4a to fcfb1d1 Compare December 4, 2026 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

2 participants