Skip to content

Commit

Permalink
Fix race condition between set() and push() (#14)
Browse files Browse the repository at this point in the history
A worker can end up picking up a job between `set()` and `push()`,
causing a race condition that results in the job running twice. Fix
the race by adding an `initial` state for jobs that haven't yet
been pushed.

Fixes #13.
  • Loading branch information
gwynne authored Oct 24, 2024
1 parent 4e31dfa commit 20ebbf1
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ jobs:
fail-fast: false
matrix:
include:
- macos-version: macos-13
xcode-version: '~15.2'
- macos-version: macos-14
xcode-version: latest-stable
- macos-version: macos-15
xcode-version: latest-stable
runs-on: ${{ matrix.macos-version }}
timeout-minutes: 60
steps:
Expand Down
2 changes: 1 addition & 1 deletion Sources/QueuesFluentDriver/FluentQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public struct FluentQueue: AsyncQueue, Sendable {
.bind(jobStorage.jobName),
.bind(jobStorage.queuedAt),
.bind(jobStorage.delayUntil),
.literal(StoredJobState.pending),
.literal(StoredJobState.initial),
.bind(jobStorage.maxRetryCount),
.bind(jobStorage.attempts),
.bind(Data(jobStorage.payload)),
Expand Down
3 changes: 3 additions & 0 deletions Sources/QueuesFluentDriver/JobModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import struct Queues.QueueName

/// The state of a job currently stored in the database.
enum StoredJobState: String, Codable, CaseIterable {
/// Job has been set up but is not yet ready to execute.
case initial

/// Job is ready to be picked up for execution.
case pending

Expand Down

0 comments on commit 20ebbf1

Please sign in to comment.