Skip to content

feat: skip a plan or apply that's already running for the commit - #2758

Open
dannysauer wants to merge 1 commit into
diggerhq:developfrom
neverendingsupport:feat/dannys/skip-duplicate-plan
Open

dannysauer wants to merge 1 commit into
diggerhq:developfrom
neverendingsupport:feat/dannys/skip-duplicate-plan

Conversation

@dannysauer

Copy link
Copy Markdown
Contributor

Summary

Closes #2755. A repeated digger plan or digger apply comment no longer starts a second run of a commit that's still being planned or applied. Digger replies to the comment instead, naming the projects it found running and pointing to --force.

This is a little wider than the issue, for the reasons in this comment: it covers applies too, and it closes the race between comments that arrive together.

Behavior

  • A plan is skipped when every project it asks for is already being planned. An apply is skipped when any of them is already being applied, since two applies of one project compete for its state.
  • It counts runs on the PR's current commit queued in the last 2 hours, including the automatic plan after a push and an apply started from a check run. DIGGER_DUPLICATE_COMMAND_WINDOW changes the window, and 0 turns the check off.
  • A job downstream of a failed job doesn't count, because it never starts.
  • --force skips the check. It's a new flag in ParseDiggerCommentFlags, which rejected unknown flags until now, so it can't collide with existing comments.
  • The Apply button on a check run gets the same check, and Digger posts the reply as a PR comment.
  • New indexes on digger_batches (repo_full_name, pr_number, commit_sha) and digger_job_parent_links (parent_digger_job_id) keep the lookup cheap. The second was already named on the model, but a , in its gorm tag where ; belongs meant it was never created.
  • Only the GitHub handler checks. GitLab, Bitbucket and backendless mode accept --force and ignore it.

The race

Two comments a few seconds apart could both pass the check before either had created its batch. On our deployment, a pair of digger apply comments did exactly that while GitHub was slow. So each plan or apply comment takes a transaction-scoped Postgres advisory lock for its PR, from the check until its batch exists, for at most 5 minutes.

  • Waiting: a comment polls for the lock for up to a minute, without holding a connection while it waits.
  • Failure: if it still can't get the lock, or the check fails, the comment is skipped with a note to retry, rather than run unchecked.
  • Pooling: the lock is transaction-scoped, so it works behind a transaction pooler, and Postgres frees it if a backend dies.

Known gap

A run that was cancelled in CI, or that failed to dispatch, still counts as running until the window ends, because the backend doesn't hear that it stopped. The reply and the docs point to --force for that.

Testing

  • Unit tests (SQLite) cover the skip rules, the window setting, --force parsing and the query, including jobs downstream of a failure.
  • TestLockPullRequestCommandsPostgres needs a real Postgres and skips unless DIGGER_TEST_POSTGRES_URL is set. I ran it, and the query test, against Postgres 17. Happy to wire a Postgres service into backend_test.yml if you'd like it in CI.
  • Docs: comment-args, commentops, plan-preview (which also gets a broken CommentOps link fixed), and the self-hosting configuration table.

We run this on our self-hosted backend. We're happy to adjust it based on your feedback.

A second `digger plan` or `digger apply` on a commit that's still being
planned or applied started a second batch. Two applies of the same project
then raced for its state.

The backend now skips such a comment, or a check-run Apply, and replies
with the projects it found running and how to force it. A plan is skipped
when every requested project is being planned; an apply when any is.
`--force` runs it anyway. A job downstream of a failed job doesn't count,
since it never starts. DIGGER_DUPLICATE_COMMAND_WINDOW bounds how far back
the check looks (default 2h, the job token's lifetime); 0 disables it.
A new index on digger_batches (repo_full_name, pr_number, commit_sha)
keeps the lookup cheap.

Requests that arrive together would both pass the check before either
created its batch. A transaction-scoped Postgres advisory lock per PR
serializes them from the check until the batch exists, for at most 5
minutes. A request that can't get the lock within a minute, or can't run
the check, is skipped with a note to retry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

Skip a duplicate digger plan while the same commit is still being planned

1 participant