class Sidetiq::Middleware::History

Public Instance Methods

call(worker, msg, queue) { || ... } click to toggle source
# File lib/sidetiq/middleware/history.rb, line 4
def call(worker, msg, queue, &block)
  if worker.kind_of?(Sidetiq::Schedulable)
    call_with_sidetiq_history(worker, msg, queue, &block)
  else
    yield
  end
end

Private Instance Methods

call_with_sidetiq_history(worker, msg, queue) { || ... } click to toggle source
# File lib/sidetiq/middleware/history.rb, line 14
def call_with_sidetiq_history(worker, msg, queue)
  entry = new_history_entry
  start_time = Time.now

  yield
rescue StandardError => e
  entry[:status] = :failure
  entry[:exception] = e.class.to_s
  entry[:error] = e.message
  entry[:backtrace] = e.backtrace

  raise e
ensure
  entry[:runtime] = (Time.now - start_time)
  save_entry_for_worker(entry, worker)
end
new_history_entry() click to toggle source
# File lib/sidetiq/middleware/history.rb, line 31
def new_history_entry
  {
    status: :success,
    error: "",
    exception: "",
    backtrace: "",
    node: "#{Socket.gethostname}:#{Process.pid}-#{Thread.current.object_id}",
    timestamp: Time.now.iso8601,
    runtime: ""
  }
end
save_entry_for_worker(entry, worker) click to toggle source
# File lib/sidetiq/middleware/history.rb, line 43
def save_entry_for_worker(entry, worker)
  Sidekiq.redis do |redis|
    list_name = "sidetiq:#{worker.class.name}:history"

    redis.lpush(list_name, Sidekiq.dump_json(entry))
    redis.ltrim(list_name, 0, Sidetiq.config.worker_history - 1)
  end
end