Skip to content

Commit

Permalink
retry whole transaction, not only the exec stmt
Browse files Browse the repository at this point in the history
  • Loading branch information
cenkalti committed Oct 2, 2020
1 parent b062f0d commit 7c89840
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions internal/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ func (t *Table) Front(ctx context.Context, instanceID uint32) (*Job, error) {
// If UpdateNextRun is called on a disabled job, as many happen when a job has
// been disabled during execution, next_sched will advance but next_run will remain NULL.
func (t *Table) UpdateNextRun(ctx context.Context, key Key, randFactor float64, retryParams *retry.Retry) error {
return withRetries(maxRetries, func() error {
return t.updateNextRun(ctx, key, randFactor, retryParams)
})
}

func (t *Table) updateNextRun(ctx context.Context, key Key, randFactor float64, retryParams *retry.Retry) error {
tx, j, now, err := t.getForUpdate(ctx, key.Path, key.Body)
if err != nil {
return err
Expand Down Expand Up @@ -335,13 +341,10 @@ func (t *Table) UpdateNextRun(ctx context.Context, key Key, randFactor float64,
s := "UPDATE " + t.name + " " +
"SET next_run=?, next_sched=?, instance_id=NULL " +
"WHERE path = ? AND body = ?"
err = withRetries(maxRetries, func() error {
// Note that we are passing next_run as sql.NullTime value.
// If next_run is already NULL (j.NextRun.Valid == false), it is not going to be updated.
// This may happen when the job gets disabled while it is running.
_, err = tx.ExecContext(ctx, s, j.NextRun, j.NextSched.UTC(), key.Path, key.Body)
return err
})
// Note that we are passing next_run as sql.NullTime value.
// If next_run is already NULL (j.NextRun.Valid == false), it is not going to be updated.
// This may happen when the job gets disabled while it is running.
_, err = tx.ExecContext(ctx, s, j.NextRun, j.NextSched.UTC(), key.Path, key.Body)
if err != nil {
return fmt.Errorf("failed to set next run: %w", err)
}
Expand Down

0 comments on commit 7c89840

Please sign in to comment.