Class: Delayed::Job::Monitor

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

Constant Summary collapse

DEFAULT_TABLE_NAME =
"delayed_jobs".freeze
DEFAULT_THRESHOLDS =
{
  :total_job_threshold     => 50,
  :failed_job_threshold    => 5,
  :scheduled_job_threshold => 50,
  :waiting_job_threshold   => 5,
  :running_job_threshold   => 10
}.freeze
THRESHOLD_ATTRIBUTES =
DEFAULT_THRESHOLDS.keys

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Monitor

Returns a new instance of Monitor.



34
35
36
37
38
39
40
41
42
43
# File 'lib/delayed_job/monitor.rb', line 34

def initialize(options)
  @options    = options
  @db_options = @options[:database]
  @table_name = @db_options.delete(:table_name) { Delayed::Job::Monitor.default_table_name }

  # set thresholds, use default if not provided
  DEFAULT_THRESHOLDS.each do |attribute, threshold|
    send("#{attribute}=", options.delete(attribute) { threshold })
  end
end

Class Attribute Details

.default_table_nameObject

Returns the value of attribute default_table_name.



12
13
14
# File 'lib/delayed_job/monitor.rb', line 12

def default_table_name
  @default_table_name
end

Instance Attribute Details

#table_nameObject (readonly)

Returns the value of attribute table_name.



20
21
22
# File 'lib/delayed_job/monitor.rb', line 20

def table_name
  @table_name
end

Class Method Details

.run(options) ⇒ Object



8
9
10
# File 'lib/delayed_job/monitor.rb', line 8

def run(options)
  Delayed::Job::Monitor.new(options).run
end

Instance Method Details

#alertObject



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

def alert
  Pony.mail(:to      => "[email protected]",
            :from    => "[email protected]",
            :subject => "[Alert] DJ Queue",
            :body    => alert_body)
end

#alert_bodyObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/delayed_job/monitor.rb', line 56

def alert_body
  %Q{
Delayed::Job Summary
--------------------

Total jobs:\t\t#{total_jobs}
Failed jobs:\t\t#{failed_jobs}
Scheduled jobs:\t\t#{scheduled_jobs}
Waiting jobs:\t\t#{waiting_jobs}
Running jobs:\t\t#{running_jobs}
      }
end

#count(filter = nil) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/delayed_job/monitor.rb', line 109

def count(filter = nil)
  if filter
    delayed_jobs.filter(filter).count
  else
    delayed_jobs.count
  end
end

#databaseObject



121
122
123
# File 'lib/delayed_job/monitor.rb', line 121

def database
  @database ||= Sequel.connect(@db_options)
end

#delayed_jobsObject



117
118
119
# File 'lib/delayed_job/monitor.rb', line 117

def delayed_jobs
  @delayed_jobs ||= database[table_name.to_sym]
end

#failed_jobsObject



105
106
107
# File 'lib/delayed_job/monitor.rb', line 105

def failed_jobs
  @failed_jobs ||= count(["(run_at > ? AND last_error IS NOT NULL) OR failed_at IS NOT NULL", Time.now])
end

#healthy?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/delayed_job/monitor.rb', line 77

def healthy?
  !sick?
end

#resetObject



81
82
83
84
85
86
87
# File 'lib/delayed_job/monitor.rb', line 81

def reset
  @total_jobs     = nil
  @scheduled_jobs = nil
  @waiting_jobs   = nil
  @running_jobs   = nil
  @failed_jobs    = nil
end

#runObject



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

def run
  alert if sick?
end

#running_jobsObject



101
102
103
# File 'lib/delayed_job/monitor.rb', line 101

def running_jobs
  @running_jobs ||= count(["locked_at IS NOT NULL"])
end

#scheduled_jobsObject



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

def scheduled_jobs
  @scheduled_jobs ||= count(["run_at > ? AND locked_at IS NULL AND attempts = 0", Time.now])
end

#sick?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
# File 'lib/delayed_job/monitor.rb', line 69

def sick?
  total_jobs >= total_job_threshold ||
    failed_jobs >= failed_job_threshold ||
    scheduled_jobs >= scheduled_job_threshold ||
    waiting_jobs >= waiting_job_threshold ||
    running_jobs >= running_job_threshold
end

#total_jobsObject



89
90
91
# File 'lib/delayed_job/monitor.rb', line 89

def total_jobs
  @total_jobs ||= count
end

#waiting_jobsObject



97
98
99
# File 'lib/delayed_job/monitor.rb', line 97

def waiting_jobs
  @waiting_jobs ||= count(["run_at <= ? AND locked_at IS NULL AND attempts = 0", Time.now])
end