huyhoang-vn
huyhoang-vn

Reputation: 335

How set timeout for jobs in 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.

Upvotes: 19

Views: 14947

Answers (3)

Yaseen I.
Yaseen I.

Reputation: 61

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 stop after 2 hours.

Upvotes: 2

Diego Velez
Diego Velez

Reputation: 1893

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

Upvotes: 3

Mike Perham
Mike Perham

Reputation: 22208

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.

Upvotes: 33

Related Questions