2
votes

I want to cancel the execution of a worker. I have this job, for example: #<Sidekiq::Cron::Job:0x007f1f66a869c0 @name="Crawler-job_data-source=10", @cron="20 */5 * * *", @klass="HardWorker", @status="enabled", @last_enqueue_time=2014-12-01 10:57:19 -0300, @args=[10], @message="{\"retry\":1,\"queue\":\"default\",\"unique\":true,\"class\":\"HardWorker\",\"args\":[10]}"> I setup the worker's execution with @cron="20 */5 * * *".

There are two posibilities:

1) The worker starts every 5 hours from 0 hours, 20 minutes

2) The worker starts immediately when i try this : HardWorker.perform_async(anId)

When i try HardWorker.perform_async(anId), it returns an id, for example "f80b29495c44a34ded9a333f" .

Is there anyway to cancel the sidekiq worker after the execution of the perform_async method?

4

4 Answers

2
votes

No, you cannot cancel a job via Sidekiq. You can build such logic into your application.

0
votes

Take a look a the sinatra app that comes with sidekiq: https://github.com/mperham/sidekiq/wiki/Monitoring

You can cancel scheduled jobs from that interface.

0
votes

With https://github.com/utgarda/sidekiq-status (it lets you keep track of jobs) you could unschedule a job.

For example you could keep track of next time of execution and youd be able to cancel it if needed.

Or you might want to include some kind of logic that ensures wether the job should be executed at all. (this way you actually would not have to unschedule at all)

def perform
  return unless should_perform?(object)

end

private
def should_perform? object
  Time.now.to_i > object.timestamp
end

Or, you might want to mark object with a flag (like dont_execute_until), and use it as argument passed to should_perform.

just some ideas :)

0
votes

When faced with this problem I took a very simple approach. In my Ruby (Sinatra) app I set an Environment Variable. It is local to my RACK process. In the perform_async method I have a [dangerous] infinite loop that reads that ENV value. In my Sinatra app I defined a route that changes that environment variable to something else. That is a simple flag to the endless loop in my Sidekiq worker to stop.

I really hope this is helpful for someone.