Sidekiq
Ruby- Live processes:
Sidekiq::ProcessSetlists them, with data refreshed every 5 seconds (API). - Killed mid-job: open-source Sidekiq fetches with
BRPOP, so a job that was running when the process died is lost. Sidekiq Pro'ssuper_fetchrecovers it (Reliability). - Shutdown: a job still running when the
-ttimeout ends is terminated and pushed back to Redis to run again (Signals). The middleware below closes that run so it is not reported as hung. - Failures: a job is retried 25 times over about 21 days before it moves to the Dead set (Error Handling). The middleware reports the first failed attempt, not the last.
- Recurring jobs are a Sidekiq Enterprise feature. On open-source Sidekiq, enqueue the heartbeat from the scheduler you already run, such as the sidekiq-cron gem or system cron.
Heartbeat job
# app/sidekiq/queue_heartbeat_job.rb
require "net/http"
class QueueHeartbeatJob
include Sidekiq::Job
sidekiq_options queue: "default", retry: false
def perform
uri = URI(ENV.fetch("DRUMBEATS_HEARTBEAT_URL"))
Net::HTTP.start(uri.host, uri.port,
use_ssl: uri.scheme == "https",
open_timeout: 5, read_timeout: 5) do |http|
http.get(uri.request_uri)
end
end
end
# Enqueue it every minute. With Sidekiq Enterprise:
Sidekiq.configure_server do |config|
config.periodic do |mgr|
mgr.register("* * * * *", "QueueHeartbeatJob")
end
end# app/sidekiq/queue_heartbeat_job.rb
require "net/http"
class QueueHeartbeatJob
include Sidekiq::Job
sidekiq_options queue: "default", retry: false
def perform
uri = URI(ENV.fetch("DRUMBEATS_HEARTBEAT_URL"))
Net::HTTP.start(uri.host, uri.port,
use_ssl: uri.scheme == "https",
open_timeout: 5, read_timeout: 5) do |http|
http.get(uri.request_uri)
end
end
end
# Enqueue it every minute. With Sidekiq Enterprise:
Sidekiq.configure_server do |config|
config.periodic do |mgr|
mgr.register("* * * * *", "QueueHeartbeatJob")
end
endStart and finish pings: server middleware
# config/initializers/drumbeats.rb
require "net/http"
class DrumbeatsMiddleware
include Sidekiq::ServerMiddleware
def call(_instance, job, _queue)
return yield if job["class"] == "QueueHeartbeatJob"
# One monitor per job class: Billing::InvoiceJob pings the
# monitor whose slug is "billing-invoicejob".
slug = job["class"].downcase.gsub(/[^a-z0-9]+/, "-")[0, 50]
# The jid is reused by retries and by reruns after a shutdown.
run_id = "#{job["jid"]}-#{Time.now.to_i}"
ping(slug, "start", run_id)
begin
yield
rescue Sidekiq::Shutdown
# Deploy or restart: Sidekiq pushes the job back and runs it
# again. Close this run so it is not failed as hung.
ping(slug, "success", run_id)
raise
rescue StandardError
ping(slug, "failure", run_id)
raise
end
ping(slug, "success", run_id)
end
private
def ping(slug, event, run_id)
base = ENV.fetch("DRUMBEATS_PING_URL")
uri = URI("#{base}/#{slug}/#{event}?run_id=#{run_id}")
Net::HTTP.start(uri.host, uri.port,
use_ssl: uri.scheme == "https",
open_timeout: 3, read_timeout: 3) do |http|
http.get(uri.request_uri)
end
rescue StandardError
nil # a failed ping must not fail the job
end
end
Sidekiq.configure_server do |config|
config.server_middleware { |chain| chain.add DrumbeatsMiddleware }
end# config/initializers/drumbeats.rb
require "net/http"
class DrumbeatsMiddleware
include Sidekiq::ServerMiddleware
def call(_instance, job, _queue)
return yield if job["class"] == "QueueHeartbeatJob"
# One monitor per job class: Billing::InvoiceJob pings the
# monitor whose slug is "billing-invoicejob".
slug = job["class"].downcase.gsub(/[^a-z0-9]+/, "-")[0, 50]
# The jid is reused by retries and by reruns after a shutdown.
run_id = "#{job["jid"]}-#{Time.now.to_i}"
ping(slug, "start", run_id)
begin
yield
rescue Sidekiq::Shutdown
# Deploy or restart: Sidekiq pushes the job back and runs it
# again. Close this run so it is not failed as hung.
ping(slug, "success", run_id)
raise
rescue StandardError
ping(slug, "failure", run_id)
raise
end
ping(slug, "success", run_id)
end
private
def ping(slug, event, run_id)
base = ENV.fetch("DRUMBEATS_PING_URL")
uri = URI("#{base}/#{slug}/#{event}?run_id=#{run_id}")
Net::HTTP.start(uri.host, uri.port,
use_ssl: uri.scheme == "https",
open_timeout: 3, read_timeout: 3) do |http|
http.get(uri.request_uri)
end
rescue StandardError
nil # a failed ping must not fail the job
end
end
Sidekiq.configure_server do |config|
config.server_middleware { |chain| chain.add DrumbeatsMiddleware }
end