19

I encountered an issue with sidekiq:
I want to set timeout for jobs, meaning when a job has process time greater than timeout then that job will stop.

I have searched how to set global timeout config in file sidekiq.yml.
But I want to set separate timeout for difference separate jobs meaning one of classes to define worker will have particular timeout config.
Can you help me.
Thanks so much.

sKhan
  • 9,694
  • 16
  • 55
  • 53
huyhoang-vn
  • 335
  • 1
  • 3
  • 13

3 Answers3

30

There's no approved way to do this. You cannot stop a thread safely while it is executing. You need to change your job to check periodically if it should stop.

You can set network timeouts on any 3rd party calls you are making so that they time out.

Mike Perham
  • 21,300
  • 6
  • 59
  • 61
  • 1
    I got it. Thank Mr. @Mike Perham so much. I will research using network timeouts way following your suggest. – huyhoang-vn Feb 21 '16 at 09:54
  • Is there an example of how to do this? I keep having external services which are unreachable or freeze for whatever reason, causing Sidekiq threads to hang indefinitely. – CDub Apr 16 '18 at 17:26
1

You can wrap your job code inside a timeout block like the below:

Timeout::timeout(2.hours) do

  ***.  do possibly long-running task *****

end

The job will fail automatically after 2 hours.

1

This is the same method as yassen suggested, but more concrete.

class MyCustomWorker 
    include Sidekiq::Worker

    def perform
        begin
            Timeout::timeout(30.minutes) do # set timeout to 30 minutes
                perform_job()
            end
        rescue Timeout::Error
            Rails.logger.error "timeout reached for worker"
        end
    end

    def perform_job
        # worker logic here
    end
end
Diego Velez
  • 1,584
  • 1
  • 16
  • 20