monitored_process 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/monitored_process.rb +146 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0dd316a0297622d8e876f755d961ab155f065be5
4
+ data.tar.gz: 05131bec52fe46501742e1130350a434532bb115
5
+ SHA512:
6
+ metadata.gz: 0225f8ee40b9b9868803b7c99563ffa42d06ef4a5c6fd3aa8b355e6dd84a56ed7e0021cf6dd09d859670f02e79feefbb3977443c3f8833e21a4a5022f25dd6d0
7
+ data.tar.gz: b67a91b4c3cf97f0940d728a2066a3c414b17bd8847665dd31ce535561f1579a432def5f6af12c993b3cbfbc9172b4c3fe42ef9d520b41c7a7573c1c1a650f5e
@@ -0,0 +1,146 @@
1
+ require 'active_record'
2
+
3
+ class MonitoredProcess < ActiveRecord::Base
4
+
5
+ attr_accessor :log_lambda
6
+
7
+ STATE = { :running => 1, :finished => 2, :finished_with_errors => 3 }
8
+
9
+ # VALIDATIONS
10
+ validates_presence_of :name, :state, :pid
11
+
12
+ def self.start_process
13
+ p = MonitoredProcess.new
14
+ p.name = self.to_s
15
+ p.state = STATE[:running]
16
+ p.pid = Process.pid
17
+ p.console_output = ""
18
+ p.save!
19
+
20
+ p.log_lambda = lambda do |message|
21
+ puts "#{DateTime.now} - #{message}"
22
+ end
23
+
24
+ return p
25
+ end
26
+
27
+ def self.last_time_executed
28
+ p = MonitoredProcess.where(:name => self.to_s).last
29
+ return p.updated_at if !p.blank?
30
+ end
31
+
32
+ def self.is_running_by_name? (process_name)
33
+ processes = MonitoredProcess.where(:name => process_name, :state => STATE[:running])
34
+ return false if processes.blank?
35
+
36
+ is_running = false
37
+ processes.each do |process|
38
+ begin
39
+ Process.getpgid(process.pid)
40
+ is_running = true
41
+ rescue Errno::ESRCH
42
+ process.finish_with_errors! "Unknown error / Process was terminated"
43
+ end
44
+ end
45
+ return is_running
46
+ end
47
+
48
+ def self.is_running?
49
+ return self.is_running_by_name? (self.name)
50
+ end
51
+
52
+ def finish!
53
+ set_finished_status
54
+ self.save!
55
+ end
56
+
57
+ def finish
58
+ set_finished_status
59
+ return self.save
60
+ end
61
+
62
+ def finish_with_errors! (error_description)
63
+ self.state = STATE[:finished_with_errors]
64
+ self.error_description = error_description
65
+ self.calculate_elapsed_seconds
66
+ self.save!
67
+ end
68
+
69
+ def calculate_elapsed_time
70
+ begin
71
+ hours_with_fraction = self.elapsed_seconds / 3600.0
72
+ hours = hours_with_fraction.truncate
73
+ minutes_with_fraction = (hours_with_fraction - hours) * 60
74
+ minutes = minutes_with_fraction.truncate
75
+ seconds_with_fraction = (minutes_with_fraction - minutes) * 60
76
+ seconds = seconds_with_fraction.truncate
77
+
78
+ self.elapsed_time = "#{hours.to_s.rjust(2, "0")}:#{minutes.to_s.rjust(2, "0")}:#{seconds.to_s.rjust(2, "0")}"
79
+ rescue
80
+ self.elapsed_time "00:00:00"
81
+ end
82
+ end
83
+
84
+ def calculate_elapsed_seconds
85
+ self.elapsed_seconds = Time.parse(DateTime.now.to_s) - Time.parse(self.created_at.to_s)
86
+
87
+ # Set elapsed time
88
+ self.calculate_elapsed_time
89
+ end
90
+
91
+ def output(text)
92
+ self.console_output += text + "\n"
93
+ log_lambda.call(text) if !log_lambda.blank?
94
+ end
95
+
96
+ def log(text)
97
+ log_lambda.call(text) if !log_lambda.blank?
98
+ end
99
+
100
+ def send_status_email
101
+ MonitoredProcessMailer.process_execution_result_email(self).deliver
102
+ end
103
+
104
+ def self.list(params)
105
+ parameters = []
106
+ parameters = params.clone if !params.blank?
107
+ conditions = []
108
+
109
+ if !params.blank?
110
+ if !params[:start_date].blank?
111
+ conditions << "created_at >= :start_date"
112
+ end
113
+ if !params[:end_date].blank?
114
+ conditions << "created_at <= :end_date"
115
+ end
116
+ if !params[:process_type].blank?
117
+ conditions << "name <= :process_type"
118
+ end
119
+ end
120
+
121
+ parameters[:sort] = "id_desc" if params[:sort].blank?
122
+
123
+ where(conditions.join(" and "), parameters).sorted(parameters[:sort]).paginate(:page => parameters[:page], :per_page => 100)
124
+ end
125
+
126
+ def self.translate_state(state)
127
+ case state
128
+ when STATE[:running]
129
+ "Running"
130
+ when STATE[:finished]
131
+ "Finished"
132
+ when STATE[:finished_with_errors]
133
+ "Finished with errors"
134
+ end
135
+ end
136
+
137
+ def self.get_process_names
138
+ MonitoredProcess.uniq.pluck(:name)
139
+ end
140
+
141
+ private
142
+ def set_finished_status
143
+ self.state = STATE[:finished]
144
+ self.calculate_elapsed_seconds
145
+ end
146
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: monitored_process
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Juan Manuel Vallejo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A class that inherits from MonitoredProcess can easily register execution
14
+ details and keep track of process execution.
15
+ email: jmvallejo@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/monitored_process.rb
21
+ homepage: http://rubygems.org/gems/monitored_process
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.0.7
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: A simple gem that helps keeping track of running processes.
45
+ test_files: []