Expiring Jobs

Mike Perham edited this page Jul 28, 2016 · 15 revisions

Sidekiq Pro supports jobs which expire after a certain length of time.

Jobs that are set to expire can run as long as they want, but an expiring job must start executing before the expiration time.

Use Cases

  1. Perhaps you want to expire a cache which has a TTL of 30 minutes with a Sidekiq job. If the job doesn't process successfully within 30 minutes, there's no point in continuing to execute the job.
  2. You use a Sidekiq job to send a daily digest email. If the job doesn't execute within 24 hours, perhaps you want to skip that day as the user might only care about the latest digest.

Usage

Require the feature in your initializer:

require 'sidekiq/pro/expiry'

Defining Expiration

Statically:

class SomeWorker
  include Sidekiq::Worker
  sidekiq_options expires_in: 1.hour
  ...
end

Dynamically, per job:

SomeWorker.set(expires_in: 1.day).perform_async(...)

expires_in must be a relative time, not an absolute timestamp.

Expiration knows about scheduled jobs: schedule a job to run two hours from now with a one hour expiration and it will expire three hours from now.

Caveats

If a job expires and was part of a batch, the job result counts as a success and the batch will count as succeeded.