Class: Katalyst::Healthcheck::Task

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Store::Attributes

#attributes, #attributes=, included, #initialize

Class Method Details

.allObject



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

Parameters:

  • name (Symbol)

    name of the task



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

.storeObject



30
31
32
# File 'lib/katalyst/healthcheck/task.rb', line 30

def store
  Katalyst::Healthcheck.config.store
end

.summaryString

Returns Summary of task status.

Returns:

  • (String)

    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

Returns:

  • (Task)

    This task



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_timeObject



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

Returns:

  • (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.

Returns:

  • (Boolean)

    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

#reloadObject



104
105
106
# File 'lib/katalyst/healthcheck/task.rb', line 104

def reload
  Task.find(name)
end

#saveTask

Save task state

Returns:

  • (Task)

    This task



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

Returns:

  • (Task)

    This task



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