Running jobs that are not thread safe #1562
-
Hi all, one of my jobs uses a library that I know is not thread safe. To mitigate the problem I use |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
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 |
Beta Was this translation helpful? Give feedback.
Ooh, good question! Because this would be per process I think you could use a simple mutex. e.g.
Raising an exception and retrying later is how GoodJob's Concurrency Controls work, though you could tune the
wait:
value to your particular usage.