Skip to content

Running jobs that are not thread safe #1562

Answered by bensheldon
steveh asked this question in Q&A
Discussion options

You must be logged in to vote

Ooh, good question! Because this would be per process I think you could use a simple mutex. e.g.

class YourJob < ApplicationJob
  MUTEX = Mutex.new
  ResourceLockedError = Class.new(StandardError)

  retry_on ResourceLockedError, wait: :polynomially_longer, attempts: :unlimited

  around_perform do |_job, block|
    MUTEX.try_lock { block.call } || raise ResourceLockedError
  end
end

Raising an exception and retrying later is how GoodJob's Concurrency Controls work, though you could tune the wait: value to your particular usage.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@steveh
Comment options

Answer selected by steveh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants