Class: Delayed::Job::Monitor
- Inherits:
-
Object
- Object
- Delayed::Job::Monitor
- 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
-
.default_table_name ⇒ Object
Returns the value of attribute default_table_name.
Instance Attribute Summary collapse
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
Class Method Summary collapse
Instance Method Summary collapse
- #alert ⇒ Object
- #alert_body ⇒ Object
- #count(filter = nil) ⇒ Object
- #database ⇒ Object
- #delayed_jobs ⇒ Object
- #failed_jobs ⇒ Object
- #healthy? ⇒ Boolean
-
#initialize(options) ⇒ Monitor
constructor
A new instance of Monitor.
- #reset ⇒ Object
- #run ⇒ Object
- #running_jobs ⇒ Object
- #scheduled_jobs ⇒ Object
- #sick? ⇒ Boolean
- #total_jobs ⇒ Object
- #waiting_jobs ⇒ Object
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 = @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}=", .delete(attribute) { threshold }) end end |
Class Attribute Details
.default_table_name ⇒ Object
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_name ⇒ Object (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() Delayed::Job::Monitor.new().run end |
Instance Method Details
#alert ⇒ Object
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_body ⇒ Object
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 |
#database ⇒ Object
121 122 123 |
# File 'lib/delayed_job/monitor.rb', line 121 def database @database ||= Sequel.connect(@db_options) end |
#delayed_jobs ⇒ Object
117 118 119 |
# File 'lib/delayed_job/monitor.rb', line 117 def delayed_jobs @delayed_jobs ||= database[table_name.to_sym] end |
#failed_jobs ⇒ Object
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
77 78 79 |
# File 'lib/delayed_job/monitor.rb', line 77 def healthy? !sick? end |
#reset ⇒ Object
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 |
#run ⇒ Object
45 46 47 |
# File 'lib/delayed_job/monitor.rb', line 45 def run alert if sick? end |
#running_jobs ⇒ Object
101 102 103 |
# File 'lib/delayed_job/monitor.rb', line 101 def running_jobs @running_jobs ||= count(["locked_at IS NOT NULL"]) end |
#scheduled_jobs ⇒ Object
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
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_jobs ⇒ Object
89 90 91 |
# File 'lib/delayed_job/monitor.rb', line 89 def total_jobs @total_jobs ||= count end |
#waiting_jobs ⇒ Object
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 |