Module: Yabeda::DelayedJob

Defined in:
lib/yabeda/delayed_job.rb,
lib/yabeda/delayed_job/plugin.rb,
lib/yabeda/delayed_job/version.rb

Defined Under Namespace

Classes: Error, Plugin

Constant Summary collapse

LONG_RUNNING_JOB_RUNTIME_BUCKETS =
[
  0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, # standard (from Prometheus)
  30, 60, 120, 300, 1800, 3600, 21_600 # DelayedJob tasks may be very long-running
].freeze
VERSION =
'0.1.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.jobs_started_atObject

Hash of hashes containing all currently running jobs’ start timestamps to calculate maximum durations of currently running not yet completed jobs { { queue: “default”, worker: “SomeJob” } => { “jid1” => 100500, “jid2” => 424242 } }



82
83
84
# File 'lib/yabeda/delayed_job.rb', line 82

def jobs_started_at
  @jobs_started_at
end

Class Method Details

.active_record_adapter?Boolean

Returns:

  • (Boolean)


74
75
76
77
# File 'lib/yabeda/delayed_job.rb', line 74

def active_record_adapter?
  defined?(Delayed::Backend::ActiveRecord::Job) &&
    Delayed::Worker.backend.name == Delayed::Backend::ActiveRecord::Job.name
end

.elapsed(start) ⇒ Object



93
94
95
# File 'lib/yabeda/delayed_job.rb', line 93

def elapsed(start)
  (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start).round(3)
end

.job_scopeObject



65
66
67
68
69
70
71
72
# File 'lib/yabeda/delayed_job.rb', line 65

def job_scope
  db_time_now = ::Delayed::Worker.backend.db_time_now
  ::Delayed::Worker.backend.where(
    '(run_at <= ? AND (locked_at IS NULL OR locked_at < ?)) AND failed_at IS NULL',
    db_time_now,
    db_time_now - ::Delayed::Worker.max_run_time
  ).group(:queue)
end

.labelize(job) ⇒ Object



50
51
52
53
54
# File 'lib/yabeda/delayed_job.rb', line 50

def labelize(job)
  result = { queue: job.queue, worker: job.name }
  result.merge!(error: job.error.class.name) if job.error
  result
end

.server?Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/yabeda/delayed_job.rb', line 45

def server?
  require 'delayed/command'
  @server ||= ObjectSpace.each_object(Delayed::Command).any?
end

.track_database_metricsObject



56
57
58
59
60
61
62
63
# File 'lib/yabeda/delayed_job.rb', line 56

def track_database_metrics
  job_scope.select(:queue).count.each do |queue, count|
    Yabeda.delayed_job.jobs_waiting_count.set({ queue: queue }, count)
  end
  job_scope.select('queue, max(NOW() - run_at) latency').each do |job|
    Yabeda.delayed_job.queue_latency.set({ queue: job.queue }, job.latency)
  end
end

.track_max_job_runtimeObject



84
85
86
87
88
89
90
91
# File 'lib/yabeda/delayed_job.rb', line 84

def track_max_job_runtime
  now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  ::Yabeda::DelayedJob.jobs_started_at.each do |labels, jobs|
    oldest_job_started_at = jobs.values.min
    oldest_job_duration = oldest_job_started_at ? (now - oldest_job_started_at).round(3) : 0
    Yabeda.delayed_job.running_job_runtime.set(labels, oldest_job_duration)
  end
end