Class: Sidekiq::AppsignalCronCheckinMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/appsignal_cron_checkin_middleware.rb

Overview

Reports an AppSignal cron check-in for scheduled jobs flagged monitor: true
in config/sidekiq_production_schedule.yml, so AppSignal alerts when a job stops
running, runs late, or crashes.

Opt in per job with one flag — no identifier to spell out, no worker or code
change:

edi_order_flow:
every: '15m'
class: EdiOrderFlowWorker
monitor: true # check-in identifier = the schedule entry name

.stamp_identifiers! runs at scheduler startup and copies each monitored
entry's NAME into a checkin key (the entry name is not otherwise in the
pushed job hash). sidekiq-scheduler then pushes the whole entry into the job
payload and Sidekiq preserves unknown keys (the same way description: rides
along), so the identifier arrives here as job['checkin']. Deriving it from the
name means one source of truth — it can't drift from the entry it monitors. An
explicit checkin: on the entry overrides, for the rare custom identifier.

Because the flag lives on the SCHEDULE ENTRY and not the worker class, only
scheduler-driven runs are reported — an ad-hoc Worker.perform_async of the same
class is not, which is exactly what cron monitoring wants. A worker class
backing several entries (e.g. Google feed US/CA) gets a distinct identifier per
entry for free.

Registered PRODUCTION-ONLY (config/initializers/sidekiq.rb): the production
schedule is the only file with monitor: flags, and staging is skipped anyway.

The block form reports a START before the job and a FINISH after it succeeds; a
crash sends START without FINISH and the exception still propagates, so Sidekiq
retry/failure behavior is unchanged. Create the matching monitor in the
AppSignal dashboard (Check-ins → Cron) named for the entry, with the job's
crontab, timezone America/Chicago, and a grace window — the code makes
occurrences appear; the dashboard config is what alerts.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.stamp_identifiers!(schedule) ⇒ Hash

Stamp checkin = entry name onto every monitor: true entry, so the YAML opts
in with a flag and the identifier stays single-sourced. Called from the
scheduler startup hook before the schedule is applied. Mutates and returns it.

Parameters:

  • schedule (Hash)

    the loaded sidekiq-scheduler schedule (name => config)

Returns:

  • (Hash)

    the same schedule, checkin ids stamped onto monitored entries



48
49
50
51
52
53
# File 'lib/sidekiq/appsignal_cron_checkin_middleware.rb', line 48

def self.stamp_identifiers!(schedule)
  schedule.each do |name, cfg|
    cfg['checkin'] ||= name if cfg.is_a?(Hash) && cfg['monitor']
  end
  schedule
end

Instance Method Details

#call(_worker, job, _queue) ⇒ Object

Report an AppSignal cron check-in around a scheduled job carrying a String
checkin identifier (stamped from its schedule entry name); a plain
pass-through otherwise or when AppSignal is inactive.

Parameters:

  • _worker (Object)

    the worker instance (unused)

  • job (Hash)

    the Sidekiq job payload; job['checkin'] is the identifier

  • _queue (String)

    the queue name (unused)

Returns:

  • (Object)

    the wrapped job's return value



63
64
65
66
67
68
# File 'lib/sidekiq/appsignal_cron_checkin_middleware.rb', line 63

def call(_worker, job, _queue, &)
  identifier = job['checkin']
  return yield unless identifier.is_a?(String) && defined?(Appsignal) && Appsignal.active?

  Appsignal::CheckIn.cron(identifier, &)
end