Remove post-persist ambiguity and have events match emitted state - #1350
Open
brandur wants to merge 1 commit into
Open
Remove post-persist ambiguity and have events match emitted state#1350brandur wants to merge 1 commit into
brandur wants to merge 1 commit into
Conversation
brandur
force-pushed
the
brandur-events-persist-state
branch
from
August 13, 2026 01:26
e4d6dcc to
4c45612
Compare
brandur
marked this pull request as draft
August 13, 2026 01:28
brandur
force-pushed
the
brandur-events-persist-state
branch
from
August 13, 2026 05:10
4c45612 to
4750557
Compare
brandur
commented
Aug 13, 2026
| attempt = CASE | ||
| WHEN river_job.state = 'running' | ||
| AND NOT (job_input.state IN ('retryable','scheduled') AND river_job.metadata ? 'cancel_attempted_at') | ||
| AND NOT (job_input.state IN ('available','retryable','scheduled') AND river_job.metadata ? 'cancel_attempted_at') |
Contributor
Author
There was a problem hiding this comment.
These are kind of a related fix that's also adjacent. A job completer may set a job back to available if the scheduled time was very short in the future. Adding available here makes it so that a pending cancel wins if this was about to occur which is the more correct behavior (it was a bug before that this was wasn't considered).
This one's aimed at fixing [1] in which for some sequences in workers, we'd emit a surprising event based on the state that was actually persisted to the database. This contract was also not stable, and changed subtly with the introduction of #1219. From [1], this is best illustrated by a short example where we error after invoking `JobCompleteTx`: func (w *Worker) Work(ctx context.Context, job *river.Job[Args]) error { tx, _ := w.dbPool.Begin(ctx) defer tx.Rollback(ctx) river.JobCompleteTx[*riverpgxv5.Driver](ctx, tx, job) // row -> completed tx.Commit(ctx) return errors.New("boom") // executor reports an error, but its UPDATE is an IfRunning no-op } This used to emit a `job_completed`, but has changed in `master` to emit a `job_failed`. Here, we try to correct the emitted event and officially standardize it: | Scenario | Persisted state | Previous event | New event | |---|---:|---:|---:| | `JobCompleteTx` commits, then worker returns an error | `completed` | `job_failed` | `job_completed` | | Remote cancellation, then worker requests a retry | `cancelled` | `job_failed` | `job_cancelled` | | Remote cancellation, then worker snoozes | `cancelled` | `job_snoozed` | `job_cancelled` | Unambiguous persisted states always take precedence, though we retain reason (added in #1219) to distinguish possible states of `available`, which may be (1) an immediate retry after failure, (2) a short snooze, or (3) an interruption caused by client shutdown. [1] #1290 (comment)
brandur
force-pushed
the
brandur-events-persist-state
branch
from
August 13, 2026 05:14
4750557 to
11ee66d
Compare
brandur
marked this pull request as ready for review
August 13, 2026 05:15
Contributor
Author
|
@bgentry This does seem like a useful fix for previously somewhat ambiguous behavior. Seem okay? |
Contributor
Contributor
|
I tested this branch in combination and #1219 and it fixes all the issues I encountered while rebasing it. Thanks! BTW, from code it looks to me like |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This one's aimed at fixing [1] in which for some sequences in workers,
we'd emit a surprising event based on the state that was actually
persisted to the database. This contract was also not stable, and
changed subtly with the introduction of #1219.
From [1], this is best illustrated by a short example where we error
after invoking
JobCompleteTx:This used to emit a
job_completed, but has changed inmasterto emita
job_failed.Here, we try to correct the emitted event and officially standardize it:
JobCompleteTxcommits, then worker returns an errorcompletedjob_failedjob_completedcancelledjob_failedjob_cancelledcancelledjob_snoozedjob_cancelledUnambiguous persisted states always take precedence, though we retain
reason (added in #1219) to distinguish possible states of
available,which may be (1) an immediate retry after failure, (2) a short snooze,
or (3) an interruption caused by client shutdown.
[1] #1290 (comment)