Class: Checkup::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/checkup/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(trigger, description, &block) ⇒ Model

Returns a new instance of Model.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/checkup/model.rb', line 27

def initialize(trigger, description, &block)
  @trigger = trigger.to_s
  @description = description.to_s
  @started_at = Time.now
  
  @services = []
  @notifiers = []
  
  instance_eval(&block) if block_given?
  Model.all << self
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



24
25
26
# File 'lib/checkup/model.rb', line 24

def description
  @description
end

#triggerObject (readonly)

Returns the value of attribute trigger.



23
24
25
# File 'lib/checkup/model.rb', line 23

def trigger
  @trigger
end

Class Method Details

.allObject



4
5
6
# File 'lib/checkup/model.rb', line 4

def all
  @all ||= []
end

.find(trigger) ⇒ Object

Raises:

  • (Errors::Model::MissingTriggerError)


8
9
10
11
12
13
14
15
# File 'lib/checkup/model.rb', line 8

def find(trigger)
  trigger = trigger.to_s
  all.each do |model|
    return model if model.trigger == trigger
  end
  raise Errors::Model::MissingTriggerError,
      "Could not find trigger '#{trigger}'."
end

.find_matching(trigger) ⇒ Object



17
18
19
20
# File 'lib/checkup/model.rb', line 17

def find_matching(trigger)
  regex = /^#{ trigger.to_s.gsub('*', '(.*)') }$/
  all.select {|model| regex =~ model.trigger }
end

Instance Method Details

#elapsed_timeObject



110
111
112
113
114
115
116
117
# File 'lib/checkup/model.rb', line 110

def elapsed_time
  duration  = Time.now.to_i - @started_at.to_i
  hours     = duration / 3600
  remainder = duration - (hours * 3600)
  minutes   = remainder / 60
  seconds   = remainder - (minutes * 60)
  '%02d:%02d:%02d' % [hours, minutes, seconds]
end

#get_class_from_scope(scope, name) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/checkup/model.rb', line 76

def get_class_from_scope(scope, name)
  klass = scope
  name = name.to_s.sub(/^Checkup::Config::/, '')
  name.split('::').each do |chunk|
    klass = klass.const_get(chunk)
  end
  klass
end

#log!(action) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/checkup/model.rb', line 85

def log!(action)
  case action
  when :started
    Logger.message "Started model '(#{trigger})'!\n" +
        "[ checkup #{ Version.current } : #{ RUBY_DESCRIPTION } ]"

  when :finished
    msg = "Finished '(#{ trigger })' " +
          "Completed %s in #{ elapsed_time }"
    if Logger.has_warnings?
      Logger.warn msg % 'Successfully (with Warnings)'
    elsif Logger.has_errors?
      Logger.error msg % 'with errors'
    else
      Logger.message msg % 'Successfully'
    end
  end
end

#notify!(status, origin, message) ⇒ Object



104
105
106
107
108
# File 'lib/checkup/model.rb', line 104

def notify! (status, origin, message)
  @notifiers.each do |notifier|
    notifier.notify! status, origin, message
  end
end

#notify_with(name, &block) ⇒ Object



43
44
45
# File 'lib/checkup/model.rb', line 43

def notify_with(name, &block)
  @notifiers << get_class_from_scope(Notifier, name).new(self, &block)
end

#perform!Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/checkup/model.rb', line 47

def perform!
  log!(:started)

  
  @services.each do |service|
    service_result = true
    begin
      Logger.message "Starting service #{service.identifier}"
      service_result &&= service.perform!
    rescue StandardError => e
      service_result = false
      self.notify! :error, service, "#{service.identifier}: #{e.message}"
      Logger.error e.message
    end
    
    message = "Finished service #{service.identifier}"
    
    if !service_result
      message += " (with errors)"
      Logger.error message
    else
      Logger.message message
    end
    
  end      
  
  log!(:finished)
end

#service(name, &block) ⇒ Object



39
40
41
# File 'lib/checkup/model.rb', line 39

def service(name, &block)
  @services << get_class_from_scope(Service, name).new(self, &block)
end