Class: MonitoredProcess::Base
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- MonitoredProcess::Base
- Defined in:
- lib/monitored_process.rb
Constant Summary collapse
- STATE =
{ :running => 1, :finished => 2, :finished_with_errors => 3 }
Instance Attribute Summary collapse
-
#log_lambda ⇒ Object
Returns the value of attribute log_lambda.
Class Method Summary collapse
- .get_process_names ⇒ Object
- .is_running? ⇒ Boolean
- .is_running_by_name?(process_name) ⇒ Boolean
- .last_time_executed ⇒ Object
- .list(params) ⇒ Object
- .start_process ⇒ Object
- .translate_state(state) ⇒ Object
Instance Method Summary collapse
- #calculate_elapsed_seconds ⇒ Object
- #calculate_elapsed_time ⇒ Object
- #finish ⇒ Object
- #finish! ⇒ Object
- #finish_with_errors!(error_description) ⇒ Object
- #log(text) ⇒ Object
- #output(text) ⇒ Object
- #send_status_email ⇒ Object
Instance Attribute Details
#log_lambda ⇒ Object
Returns the value of attribute log_lambda.
6 7 8 |
# File 'lib/monitored_process.rb', line 6 def log_lambda @log_lambda end |
Class Method Details
.get_process_names ⇒ Object
138 139 140 |
# File 'lib/monitored_process.rb', line 138 def self.get_process_names MonitoredProcess.uniq.pluck(:name) end |
.is_running? ⇒ Boolean
49 50 51 |
# File 'lib/monitored_process.rb', line 49 def self.is_running? return self.is_running_by_name? (self.name) end |
.is_running_by_name?(process_name) ⇒ Boolean
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/monitored_process.rb', line 33 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_executed ⇒ Object
28 29 30 31 |
# File 'lib/monitored_process.rb', line 28 def self.last_time_executed p = MonitoredProcess.where(:name => self.to_s).last return p.updated_at if !p.blank? end |
.list(params) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/monitored_process.rb', line 105 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_process ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/monitored_process.rb', line 13 def self.start_process p = MonitoredProcess::Base.new p.name = self.to_s p.state = STATE[:running] p.pid = Process.pid p.console_output = "" p.save! p.log_lambda = lambda do || puts "#{DateTime.now} - #{}" end return p end |
Instance Method Details
#calculate_elapsed_seconds ⇒ Object
85 86 87 88 89 90 |
# File 'lib/monitored_process.rb', line 85 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_time ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/monitored_process.rb', line 70 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 |
#finish ⇒ Object
58 59 60 61 |
# File 'lib/monitored_process.rb', line 58 def finish set_finished_status return self.save end |
#finish! ⇒ Object
53 54 55 56 |
# File 'lib/monitored_process.rb', line 53 def finish! set_finished_status self.save! end |
#finish_with_errors!(error_description) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/monitored_process.rb', line 63 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
97 98 99 |
# File 'lib/monitored_process.rb', line 97 def log(text) log_lambda.call(text) if !log_lambda.blank? end |
#output(text) ⇒ Object
92 93 94 95 |
# File 'lib/monitored_process.rb', line 92 def output(text) self.console_output += text + "\n" log_lambda.call(text) if !log_lambda.blank? end |
#send_status_email ⇒ Object
101 102 103 |
# File 'lib/monitored_process.rb', line 101 def send_status_email MonitoredProcessMailer.process_execution_result_email(self).deliver end |