Monitor Sidekiq periodic and cron jobs

On this page

Sidekiq retries a failing job, which hides trouble, and a stopped scheduler enqueues nothing, which hides more. Whether you use Sidekiq Enterprise periodic jobs or the sidekiq-cron gem, a ping at the end of perform tells you the schedule really ran.

How can a scheduled job fail silently on Sidekiq periodic jobs?

  • With sidekiq-cron, a poller thread checks the scheduled jobs every 30 seconds by default. After downtime such as a deploy, only jobs that should have started less than 1 minute ago are caught up, so a longer outage skips runs.
  • Sidekiq Enterprise periodic jobs are global to the whole cluster, and Sidekiq makes sure a single process creates the jobs. If every process is down, nothing is created.
  • A job that raises an error is retried like any other job, so a failing periodic job can look busy rather than broken for days.
  • Cron expressions use the Ruby process's time zone in Enterprise, and the Rails time zone (or UTC) in sidekiq-cron, so the same line can mean different hours in two apps.

What are the limits of Sidekiq periodic jobs?

LimitWhat the documentation says
sidekiq-cron poll intervalEvery 30 seconds by default; cron_poll_interval changes it.
sidekiq-cron catch-upBy default only jobs that should have started less than 1 minute ago are considered after downtime.
sidekiq-cron safetySafe with several Sidekiq processes: a Redis sorted set lets only the first process enqueue each run.
Enterprise periodic jobsRegistered at startup; only one process creates each job; a per-job tz option is available from Enterprise 2.2.1.
Secondssidekiq-cron accepts second-level cron lines such as */30 * * * * *, and then the poll interval should be lower than the job interval.

How do you add a CronMissed ping to Sidekiq periodic jobs?

Wrap perform so that a run reports its start, its success and its failure, and re-raise the error so that Sidekiq's own retry still works.

nightly_report_job.rb
require "net/http"

class NightlyReportJob
  include Sidekiq::Job
  PING = "https://ping.cronmissed.com/5f0c1b2e-7a3d-4c8e-9b61-2d4f8a7e3c10"

  def perform
    ping("/start")
    run_report
    ping
  rescue StandardError => e
    ping("/fail", e.full_message.byteslice(-100_000, 100_000))
    raise
  end

  private

  def ping(suffix = "", body = nil)
    uri = URI("#{PING}#{suffix}")
    Net::HTTP.start(uri.host, uri.port, use_ssl: true, open_timeout: 10, read_timeout: 10) do |http|
      body ? http.post(uri.request_uri, body) : http.get(uri.request_uri)
    end
  rescue StandardError
    nil # a lost ping must never fail the job
  end

  def run_report
    # the work
  end
end

Which check settings fit Sidekiq periodic jobs?

Create a cron check with the same expression and time zone as the periodic registration. Grace should cover the queue delay (jobs wait behind others) and the run time; ten minutes is typical on a busy queue. Because Sidekiq retries failed jobs, the first /fail ping alerts immediately and the later success ping brings the check back up.

How do you confirm the check works?

  1. Enqueue the job once by hand with NightlyReportJob.perform_async and confirm the three pings in the log.
  2. Stop all Sidekiq processes and confirm the check goes down after the grace.
  3. Raise inside run_report and confirm the alert includes the end of the error.

Sources

Questions and answers

Does the /fail ping on each retry cause an alert each time?
No. A failure on a check that is already down sends no new alert; the check alerts once when it goes down and once when a success brings it back.
Where should the ping URL live?
In an environment variable or your credentials store, not in the repository. The ping URL is the check's only credential.
Know when this job stops

Create a check, add the ping to the job, and CronMissed alerts you the first time a run is late or reports a failure.

Start monitoring