Class: Minicron::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/minicron/monitor.rb

Overview

Used to monitor the executions in the database and look for any failures or missed executions based on the schedules minicron knows about

Instance Method Summary collapse

Constructor Details

#initializeMonitor


11
12
13
# File 'lib/minicron/monitor.rb', line 11

def initialize
  @active = false
end

Instance Method Details

#running?Boolean

Is the execution monitor running?


75
76
77
# File 'lib/minicron/monitor.rb', line 75

def running?
  @active
end

#setup_dbObject

Establishes a database connection


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/minicron/monitor.rb', line 16

def setup_db
  case Minicron.config['database']['type']
  when 'mysql'
    # Establish a database connection
    ActiveRecord::Base.establish_connection(
      :adapter => 'mysql2',
      :host => Minicron.config['database']['host'],
      :database => Minicron.config['database']['database'],
      :username => Minicron.config['database']['username'],
      :password => Minicron.config['database']['password']
    )
  else
    fail Exception, "The database #{Minicron.config['database']['type']} is not supported"
  end
end

#start!Object

Starts the execution monitor in a new thread


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/minicron/monitor.rb', line 33

def start!
  # Activate the monitor
  @active = true

  # Establish a database connection
  setup_db

  # Start a thread for the monitor
  @thread = Thread.new do
    # Delay the start of the monitor loop for a moniter so we don't immediately
    # send an alert for a job the system wasn't 'up' for
    sleep 60

    # While the monitor is active run it in a loop ~every minute
    while @active
      # Get all the schedules
      schedules = Minicron::Hub::Schedule.all

      # Loop every schedule we know about
      schedules.each do |schedule|
        begin
          monitor(schedule)
        rescue Exception => e
          if Minicron.config['trace']
            puts e.message
            puts e.backtrace
          end
        end
      end

      sleep 60
    end
  end
end

#stop!Object

Stops the execution monitor


69
70
71
72
# File 'lib/minicron/monitor.rb', line 69

def stop!
  @active = false
  @thread.join
end