Class: MonitoredProcess

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/monitored_process.rb

Constant Summary collapse

STATE =
{ :running => 1, :finished => 2, :finished_with_errors => 3 }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#log_lambdaObject

Returns the value of attribute log_lambda.



5
6
7
# File 'lib/monitored_process.rb', line 5

def log_lambda
  @log_lambda
end

Class Method Details

.get_process_namesObject



137
138
139
# File 'lib/monitored_process.rb', line 137

def self.get_process_names
  MonitoredProcess.uniq.pluck(:name)
end

.is_running?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/monitored_process.rb', line 48

def self.is_running?
  return self.is_running_by_name? (self.name)
end

.is_running_by_name?(process_name) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/monitored_process.rb', line 32

def self.is_running_by_name? (process_name)
  processes = MonitoredProcess.where(:name => process_name, :state => STATE[:running])
  return false if processes.blank?

  is_running = false
  processes.each do |process|
    begin
      Process.getpgid(process.pid)
      is_running = true
    rescue Errno::ESRCH
      process.finish_with_errors! "Unknown error / Process was terminated"
    end
  end
  return is_running
end

.last_time_executedObject



27
28
29
30
# File 'lib/monitored_process.rb', line 27

def self.last_time_executed
  p = MonitoredProcess.where(:name => self.to_s).last
  return p.updated_at if !p.blank?
end

.list(params) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/monitored_process.rb', line 104

def self.list(params)
  parameters = []
  parameters = params.clone if !params.blank?
  conditions = []

  if !params.blank?
    if !params[:start_date].blank?
      conditions << "created_at >= :start_date"
    end
    if !params[:end_date].blank?
      conditions << "created_at <= :end_date"
    end
    if !params[:process_type].blank?
      conditions << "name <= :process_type"
    end
  end

  parameters[:sort] = "id_desc" if params[:sort].blank?

  where(conditions.join(" and "), parameters).sorted(parameters[:sort]).paginate(:page => parameters[:page], :per_page => 100)
end

.start_processObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/monitored_process.rb', line 12

def self.start_process
  p = MonitoredProcess.new
  p.name = self.to_s
  p.state = STATE[:running]
  p.pid = Process.pid
  p.console_output = ""
  p.save!

  p.log_lambda = lambda do |message|
    puts "#{DateTime.now} - #{message}"
  end

  return p
end

.translate_state(state) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/monitored_process.rb', line 126

def self.translate_state(state)
  case state
  when STATE[:running]
    "Running"
  when STATE[:finished]
    "Finished"
  when STATE[:finished_with_errors]
    "Finished with errors"
  end
end

Instance Method Details

#calculate_elapsed_secondsObject



84
85
86
87
88
89
# File 'lib/monitored_process.rb', line 84

def calculate_elapsed_seconds
  self.elapsed_seconds = Time.parse(DateTime.now.to_s) - Time.parse(self.created_at.to_s)

  # Set elapsed time
  self.calculate_elapsed_time
end

#calculate_elapsed_timeObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/monitored_process.rb', line 69

def calculate_elapsed_time
  begin
    hours_with_fraction = self.elapsed_seconds / 3600.0
    hours = hours_with_fraction.truncate
    minutes_with_fraction = (hours_with_fraction - hours) * 60
    minutes = minutes_with_fraction.truncate
    seconds_with_fraction = (minutes_with_fraction - minutes) * 60
    seconds = seconds_with_fraction.truncate

    self.elapsed_time = "#{hours.to_s.rjust(2, "0")}:#{minutes.to_s.rjust(2, "0")}:#{seconds.to_s.rjust(2, "0")}"
  rescue
    self.elapsed_time "00:00:00"
  end
end

#finishObject



57
58
59
60
# File 'lib/monitored_process.rb', line 57

def finish
  set_finished_status
  return self.save
end

#finish!Object



52
53
54
55
# File 'lib/monitored_process.rb', line 52

def finish!
  set_finished_status
  self.save!
end

#finish_with_errors!(error_description) ⇒ Object



62
63
64
65
66
67
# File 'lib/monitored_process.rb', line 62

def finish_with_errors! (error_description)
  self.state = STATE[:finished_with_errors]
  self.error_description = error_description
  self.calculate_elapsed_seconds
  self.save!
end

#log(text) ⇒ Object



96
97
98
# File 'lib/monitored_process.rb', line 96

def log(text)
  log_lambda.call(text) if !log_lambda.blank?
end

#output(text) ⇒ Object



91
92
93
94
# File 'lib/monitored_process.rb', line 91

def output(text)
  self.console_output += text + "\n"
  log_lambda.call(text) if !log_lambda.blank?
end

#send_status_emailObject



100
101
102
# File 'lib/monitored_process.rb', line 100

def send_status_email
  MonitoredProcessMailer.process_execution_result_email(self).deliver
end