Class: PrometheusExporter::Instrumentation::DelayedJob

Inherits:
Object
  • Object
show all
Defined in:
lib/prometheus_exporter/instrumentation/delayed_job.rb

Constant Summary collapse

JOB_CLASS_REGEXP =
%r{job_class: (\w+:{0,2})+}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client: nil) ⇒ DelayedJob

Returns a new instance of DelayedJob.



27
28
29
# File 'lib/prometheus_exporter/instrumentation/delayed_job.rb', line 27

def initialize(client: nil)
  @client = client || PrometheusExporter::Client.default
end

Class Method Details

.register_plugin(client: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/prometheus_exporter/instrumentation/delayed_job.rb', line 8

def register_plugin(client: nil)
  instrumenter = self.new(client: client)
  return unless defined?(Delayed::Plugin)

  plugin = Class.new(Delayed::Plugin) do
    callbacks do |lifecycle|
      lifecycle.around(:invoke_job) do |job, *args, &block|
        max_attempts = Delayed::Worker.max_attempts
        enqueued_count = Delayed::Job.count
        pending_count = Delayed::Job.where(attempts: 0, locked_at: nil).count
        instrumenter.call(job, max_attempts, enqueued_count, pending_count, *args, &block)
      end
    end
  end

  Delayed::Worker.plugins << plugin
end

Instance Method Details

#call(job, max_attempts, enqueued_count, pending_count, *args, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/prometheus_exporter/instrumentation/delayed_job.rb', line 31

def call(job, max_attempts, enqueued_count, pending_count, *args, &block)
  success = false
  start = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
  attempts = job.attempts + 1 # Increment because we're adding the current attempt
  result = block.call(job, *args)
  success = true
  result
ensure
  duration = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) - start

  @client.send_json(
    type: "delayed_job",
    name: job.handler.to_s.match(JOB_CLASS_REGEXP).to_a[1].to_s,
    success: success,
    duration: duration,
    attempts: attempts,
    max_attempts: max_attempts,
    enqueued: enqueued_count,
    pending: pending_count
  )
end