Class: Katalyst::Healthcheck::Task
- Inherits:
-
Object
- Object
- Katalyst::Healthcheck::Task
- Includes:
- Store::Attributes
- Defined in:
- lib/katalyst/healthcheck/task.rb
Overview
Represents a background task that runs periodically in an application
Constant Summary collapse
- TASK_GRACE_PERIOD =
10 minutes
10 * 60
Class Method Summary collapse
- .all ⇒ Object
- .destroy!(name) ⇒ Object
- .find(name) ⇒ Object
- .find!(name) ⇒ Object
- .store ⇒ Object
-
.summary ⇒ String
Summary of task status.
Instance Method Summary collapse
-
#healthy! ⇒ Task
Mark this task as healthy and save state.
- #next_time ⇒ Object
- #ok? ⇒ Boolean
-
#on_schedule? ⇒ Boolean
True if this background task is running on schedule.
- #reload ⇒ Object
-
#save ⇒ Task
Save task state.
-
#unhealthy!(error = nil) ⇒ Task
Mark this task as unhealthy and save state.
Methods included from Store::Attributes
#attributes, #attributes=, included, #initialize
Class Method Details
.all ⇒ Object
13 14 15 |
# File 'lib/katalyst/healthcheck/task.rb', line 13 def all store.read.map { |i| Task.new(i) } end |
.destroy!(name) ⇒ Object
26 27 28 |
# File 'lib/katalyst/healthcheck/task.rb', line 26 def destroy!(name) store.delete(name) end |
.find(name) ⇒ Object
18 19 20 |
# File 'lib/katalyst/healthcheck/task.rb', line 18 def find(name) all.find { |task| task.name == name.to_s } end |
.find!(name) ⇒ Object
22 23 24 |
# File 'lib/katalyst/healthcheck/task.rb', line 22 def find!(name) find(name) || raise("Undefined task '#{name}'") end |
.store ⇒ Object
30 31 32 |
# File 'lib/katalyst/healthcheck/task.rb', line 30 def store Katalyst::Healthcheck.config.store end |
.summary ⇒ String
Returns Summary of task status.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/katalyst/healthcheck/task.rb', line 35 def summary output = [] all.each do |task| output << "#{task.name}:" fields = [ ["Status", task.ok? ? "OK" : "FAIL"], ["Description", task.description], ["Error", task.error], ] fields.select { |i| i[1] }.each { |i| output << " #{i.join(': ')}" } end output.join("\n") end |
Instance Method Details
#healthy! ⇒ Task
Mark this task as healthy and save state
78 79 80 81 82 83 84 |
# File 'lib/katalyst/healthcheck/task.rb', line 78 def healthy! self.last_time = DateTime.now self.error = nil self.status = :ok save end |
#next_time ⇒ Object
70 71 72 73 74 |
# File 'lib/katalyst/healthcheck/task.rb', line 70 def next_time return nil if interval.nil? (last_time || created_at) + interval.seconds end |
#ok? ⇒ Boolean
61 62 63 |
# File 'lib/katalyst/healthcheck/task.rb', line 61 def ok? status&.to_sym != :fail && on_schedule? end |
#on_schedule? ⇒ Boolean
Returns true if this background task is running on schedule.
66 67 68 |
# File 'lib/katalyst/healthcheck/task.rb', line 66 def on_schedule? next_time.nil? || next_time + TASK_GRACE_PERIOD > DateTime.now end |
#reload ⇒ Object
104 105 106 |
# File 'lib/katalyst/healthcheck/task.rb', line 104 def reload Task.find(name) end |
#save ⇒ Task
Save task state
97 98 99 100 101 102 |
# File 'lib/katalyst/healthcheck/task.rb', line 97 def save self.created_at ||= DateTime.now self.updated_at = DateTime.now store.update(name, attributes) self end |
#unhealthy!(error = nil) ⇒ Task
Mark this task as unhealthy and save state
88 89 90 91 92 93 |
# File 'lib/katalyst/healthcheck/task.rb', line 88 def unhealthy!(error = nil) self.error = error || "Fail" self.status = :fail save end |