Make it possible to kill running sidekiq jobs
When we ran into gitlab-com/production#451, we couldn't find a way to kill the running sidekiq job.
Proposal
According to the Sidekiq FAQ you should write something like this:
class MyWorker
include Sidekiq::Worker
def perform(args)
return if cancelled?
# do stuff
end
def cancelled?
Sidekiq.redis {|c| c.exists("cancelled-#{jid}") }
end
def self.cancel!(jid)
Sidekiq.redis {|c| c.setex("cancelled-#{jid}", 86400, 1) }
end
end
So in the implementation of workers, we should regularly check #cancelled?
if we are allowed to continue the current worker.
For this we could create a helper something like:
def run_in_batches(relation, of: batch_size)
relation.each_batch(of: batch_size) do |batch|
break if cancelled?
yield batch
end
end