I use Sidekiq with ActiveJob and its callbacks. At the beginning I'd like to say that my problem sounds very similar to this one: https://github.com/mperham/sidekiq/wiki/Problems-and-Troubleshooting#cannot-find-modelname-with-id12345 however I don't use ActiveRecord callbacks.
I have a service having the method like this:
def call
array_of_batches.each do |batch|
MyJob.perform_later(batch)
end
end
Which means it creates many jobs that are performed concurrently. In the job I use before_perform
and after_perform
callbacks and I have 2 problems with each of them.
1:
before_perform do |job|
# do something
return if my_object.status == 'performed'
my_object.update(status: 'performed')
send_notification
end
In the case above it often happens that when a few jobs are running concurrently I send a notification a few times instead of only one. In a few jobs it goes to the end instead of only once in a first job that is run.
2:
after_perform do |job|
my_object.decrement!(:count)
return unless my_object.count.zero?
my_object.update(status: 'finished')
send_notification
end
In the case above it happens that decrementing is not on time and because of that the object status is not updated correctly and a notification is never sent. For example when the count
value before performing 10 jobs equals 10
then after performing all the jobs count
attribute equals 2
or 3
instead of 0
since we were supposed to decrement the value every single time.
Is it possible to fix those cases somehow? Cheers