Class: Rearview::MonitorTask

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Logger
Defined in:
lib/rearview/monitor_task.rb

Defined Under Namespace

Classes: MonitorTaskError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#logger

Constructor Details

#initialize(job, initial_delay = 0, start = true) ⇒ MonitorTask

Returns a new instance of MonitorTask.



9
10
11
12
13
14
# File 'lib/rearview/monitor_task.rb', line 9

def initialize(job,initial_delay=0,start=true)
  logger.debug "#{self} intialize initial_delay:#{initial_delay} start:#{start}"
  @job = job
  @initial_delay = initial_delay
  schedule if start
end

Instance Attribute Details

#initial_delayObject (readonly)

Returns the value of attribute initial_delay.



8
9
10
# File 'lib/rearview/monitor_task.rb', line 8

def initial_delay
  @initial_delay
end

#jobObject (readonly)

Returns the value of attribute job.



8
9
10
# File 'lib/rearview/monitor_task.rb', line 8

def job
  @job
end

#timerObject (readonly)

Returns the value of attribute timer.



8
9
10
# File 'lib/rearview/monitor_task.rb', line 8

def timer
  @timer
end

Instance Method Details

#runObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rearview/monitor_task.rb', line 28

def run
  logger.debug "#{self} run"
  @initial_delay = 0
  result = Rearview::MonitorRunner.run(@job.metrics, @job.monitor_expr, @job.minutes)
  ActiveRecord::Base.connection_pool.with_connection do
    @job.last_run = Time.now.utc
    Rearview::ResultsHandler.new(@job,result).run
  end
rescue
  logger.error "#{self} run failed: #{$!}\n#{$@.join("\n")}"
  ActiveRecord::Base.connection_pool.with_connection do
    @job.last_run = Time.now.utc
    @job.error!
  end
ensure
  schedule
end

#scheduleObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rearview/monitor_task.rb', line 15

def schedule
  logger.debug "#{self} schedule"
  if ActiveRecord::Base.connection_pool.active_connection?
    ActiveRecord::Base.connection_pool.release_connection
  end
  # TODO is this really necessary?
  if(@timer)
    @timer.cancel
  end
  delay = @job.delay + @initial_delay
  logger.debug "#{self} next run in #{delay}s"
  @timer = after(delay) { self.run }
end