Class: Chronos::Integrations::Sidekiq::ServerMiddleware

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

Overview

Captures Sidekiq execution timing and failures around the worker call.

Examples:

ServerMiddleware.new.call(worker, job, "default") { worker.perform }

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ServerMiddleware

Returns a new instance of ServerMiddleware.



133
134
135
136
137
138
# File 'lib/chronos/integrations/sidekiq.rb', line 133

def initialize(options = {})
  @notifier = options[:notifier] || Chronos
  @clock = options[:clock] || proc { monotonic_time }
  @wall_clock = options[:wall_clock] || proc { Time.now.to_f }
  @limiter = options[:limiter] || JobPayload.new
end

Instance Method Details

#call(worker, job, queue) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/chronos/integrations/sidekiq.rb', line 140

def call(worker, job, queue)
  started_at = @clock.call
  payload = base_payload(worker, job, queue)
  context = execution_context(job, payload)
  @notifier.with_context(context) do
    begin
      result = yield
      finish(payload, started_at, "completed")
      result
    rescue Exception => error # rubocop:disable Lint/RescueException
      finish(payload, started_at, "failed", error)
      notify_failure(error, payload)
      raise
    end
  end
end