# Monitor Sidekiq Scheduled Jobs for Empty and Missed Runs

> Sidekiq counts a job as processed when perform returns. Post one HTTP receipt with a record count, or a failure, and register the schedule to catch missed runs.

Source: https://crontinel.com/use-cases/monitor-sidekiq-scheduled-jobs/

Sidekiq counts a job as processed when `perform` returns. Retries, the dead set, and the busy list tell you about exceptions. They say nothing about a job that ran, found nothing to do, and returned.

Scheduled jobs add a second gap. Whether you use sidekiq-cron, sidekiq-scheduler, or a plain cron entry that enqueues, a stopped scheduler produces no errors. The job is simply never enqueued.

## Post a receipt from the job

Net::HTTP is enough. No gem is needed. Wrap the job: a run that finishes sends the count, and a job that raises sends `failed` and re-raises, so Sidekiq still retries it as usual.

```ruby
require "net/http"
require "json"
require "time"

class ReportsGenerateJob
  include Sidekiq::Job

  def perform
    started = Time.now.utc.iso8601
    records = Reports.generate! # return how many records it processed
    crontinel_receipt(started, "completed", 0, records)
  rescue StandardError
    crontinel_receipt(started, "failed", 1, nil)
    raise # Sidekiq still sees the failure and retries as usual
  end

  private

  def crontinel_receipt(started, status, exit_code, records)
    uri = URI("https://app.crontinel.com/api/v1/ingest/cron")
    req = Net::HTTP::Post.new(uri)
    req["Authorization"] = "Bearer #{ENV.fetch("CRONTINEL_INGEST_KEY")}"
    req["Content-Type"] = "application/json"
    body = { request_key: "sidekiq-#{jid}-#{started}", command: "reports:generate", status: status,
             exit_code: exit_code, started_at: started, finished_at: Time.now.utc.iso8601 }
    body[:outcomes] = { metrics: { processed_records: records } } unless records.nil?
    req.body = body.to_json
    Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(req) }
  rescue StandardError
    # Monitoring must never fail the job.
  end
end
```

The key joins `jid` and the start time. A retry keeps the `jid` but starts later, so it reports as its own run. A key reused after a run has finished is rejected, and a failed first attempt would otherwise block the successful retry from being recorded.

A failed run carries no count, so it opens the failed alert and not also an outcome alert.

The rescue in `crontinel_receipt` matters. If Crontinel is slow or down, the job must not start failing and retrying because of it, and a monitoring error must never turn a good run into a reported failure.

## Alert on a job that was never enqueued

Register the job with its schedule:

```bash
curl -sS -X POST "https://app.crontinel.com/api/v1/ingest/schedule" \
  -H "Authorization: Bearer $CRONTINEL_INGEST_KEY" \
  -H "Content-Type: application/json" \
  -d '{"source":"named_by_user","tasks":[{"command":"reports:generate","expression":"0 3 * * *","timezone":"UTC","grace_seconds":300,"counted_minimum":1}]}'
```

When no receipt arrives inside the grace window, Crontinel opens a "cron never started" alert. When a receipt arrives with fewer than one record, the outcome rule fails the run even though Sidekiq called it a success.

## What was tested

The class above ran in a Sidekiq 8.1.7 process on Rails 8.1 with Ruby 3.3 and Redis 7, in Docker, against a local Crontinel app. A normal run landed as completed with its count. A run that finished with zero records failed the outcome rule and opened an alert. A job that raised landed as failed, Sidekiq retried it, and the alert resolved when a retry succeeded. Without the wrapper, the same crash sent nothing at all. It was not run with Sidekiq Pro or Enterprise, or with sidekiq-cron or sidekiq-scheduler.

See the [check-in recipes](https://docs.crontinel.com/check-in/recipes/) for the other runtimes.
