This gem allows Sidekiq workers to cancel themselves at runtime. Using long-running Sidekiq jobs that perform some kind of long computation (which is also better to avoid), you might want to be able to cancel these jobs in a clean and controlled way.
class MyWorker
include Sidekiq::Worker
prepend Sidekiq::CancellableWorker
def perform(args = {})
# perform some long running work
100.times do |i|
sleep i
return if @context.cancelled?
end
end
end
Then, while it is running, the job can be cancelled this way (for example in the Rails console):
MyWorker.cancel!(jid)
Prepending the module Sidekiq::Cancellable
worker injects the @context
variable into the worker at runtime. The worker can then call @context.cancelled?
(or pass it through the chain of execution) to be able to stop the execution depending on the result.
Add this line to your application's Gemfile:
gem 'sidekiq-cancellable-worker'
And then execute:
$ bundle
Or install it yourself as:
$ gem install sidekiq-cancellable-worker
Bug reports and pull requests are welcome on GitHub at https://github.com/OnceApp/sidekiq-cancellable-worker.
The gem is available as open source under the terms of the MIT License.