monitored_process 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +34 -0
  5. data/lib/generators/monitored_process_install/USAGE +8 -0
  6. data/lib/generators/monitored_process_install/monitored_process_install_generator.rb +15 -0
  7. data/lib/generators/monitored_process_install/templates/create_monitored_processes.rb +15 -0
  8. data/lib/monitored_process.rb +149 -146
  9. data/lib/monitored_process/version.rb +3 -0
  10. data/lib/tasks/monitored_process_tasks.rake +4 -0
  11. data/test/dummy/README.rdoc +28 -0
  12. data/test/dummy/Rakefile +6 -0
  13. data/test/dummy/app/assets/javascripts/application.js +13 -0
  14. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  15. data/test/dummy/app/controllers/application_controller.rb +5 -0
  16. data/test/dummy/app/helpers/application_helper.rb +2 -0
  17. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  18. data/test/dummy/bin/bundle +3 -0
  19. data/test/dummy/bin/rails +4 -0
  20. data/test/dummy/bin/rake +4 -0
  21. data/test/dummy/bin/setup +29 -0
  22. data/test/dummy/config.ru +4 -0
  23. data/test/dummy/config/application.rb +26 -0
  24. data/test/dummy/config/boot.rb +5 -0
  25. data/test/dummy/config/database.yml +25 -0
  26. data/test/dummy/config/environment.rb +5 -0
  27. data/test/dummy/config/environments/development.rb +41 -0
  28. data/test/dummy/config/environments/production.rb +79 -0
  29. data/test/dummy/config/environments/test.rb +42 -0
  30. data/test/dummy/config/initializers/assets.rb +11 -0
  31. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  32. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  33. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  34. data/test/dummy/config/initializers/inflections.rb +16 -0
  35. data/test/dummy/config/initializers/mime_types.rb +4 -0
  36. data/test/dummy/config/initializers/session_store.rb +3 -0
  37. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  38. data/test/dummy/config/locales/en.yml +23 -0
  39. data/test/dummy/config/routes.rb +56 -0
  40. data/test/dummy/config/secrets.yml +22 -0
  41. data/test/dummy/db/development.sqlite3 +0 -0
  42. data/test/dummy/db/migrate/20150603215041_create_monitored_processes.rb +15 -0
  43. data/test/dummy/db/migrate/create_monitored_processes.rb +15 -0
  44. data/test/dummy/db/schema.rb +28 -0
  45. data/test/dummy/db/test.sqlite3 +0 -0
  46. data/test/dummy/log/development.log +13 -0
  47. data/test/dummy/log/test.log +30 -0
  48. data/test/dummy/public/404.html +67 -0
  49. data/test/dummy/public/422.html +67 -0
  50. data/test/dummy/public/500.html +66 -0
  51. data/test/dummy/public/favicon.ico +0 -0
  52. data/test/generators/monitored_process_install_generator_test.rb +14 -0
  53. data/test/monitored_process_test.rb +7 -0
  54. data/test/test_helper.rb +19 -0
  55. metadata +133 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 93a6fd0a399261640dc37470faac06435ad48751
4
- data.tar.gz: 1dcae3a7f41e9857e6ccd49702bf282bf3bdab01
3
+ metadata.gz: 1f2c256a81987d5ccea603a1e2734a3a4fd903a0
4
+ data.tar.gz: 2ee225057901afaaab5fe03bcdcf878ea11ccbeb
5
5
  SHA512:
6
- metadata.gz: 23889b5dceb99f3311af09d9497d13e5aa710ee74c952a2309f453ad3cd2bb66785e2243b3e234cac61e016ae0333e708a695fd91fa81c52c034dcaa33082644
7
- data.tar.gz: 5b81828b54285bc3af6d2c44c80ee91374af83f7025b9b14465e62894e499faa214adba9666397ae32fe2a4805d1ace0703f1feaf2cc9cb3fb28cb587f68c881
6
+ metadata.gz: 5642460d1c071c1c46637863694d25dc30556f9f3559417d383658a52f8869b04c2d05f54700a8b1776c72204ed2e06430df81a97bff7f53b2c1943c08d79b45
7
+ data.tar.gz: bb8a2fee836afd58c3e6d706a8c5a7a681de519bfa512c67fa05feaadbe43041bae81f90f3a98eb0704bc4c227678dffaa08d908bbbc5bf484ca9d27e460cc38
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Juan Manuel Vallejo
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = MonitoredProcess
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'MonitoredProcess'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate monitored_process_install Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,15 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/active_record'
3
+
4
+ class MonitoredProcessInstallGenerator < Rails::Generators::Base
5
+ include ::Rails::Generators::Migration
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ def create_migration_file
9
+ migration_template 'create_monitored_processes.rb', 'db/migrate/create_monitored_processes.rb'
10
+ end
11
+
12
+ def self.next_migration_number(dirname)
13
+ ::ActiveRecord::Generators::Base.next_migration_number(dirname)
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ class CreateMonitoredProcesses < ActiveRecord::Migration
2
+ def change
3
+ create_table :monitored_processes do |t|
4
+ t.string :name
5
+ t.integer :state
6
+ t.integer :pid
7
+ t.integer :elapsed_seconds
8
+ t.string :elapsed_time
9
+ t.text :console_output
10
+ t.text :error_description
11
+
12
+ t.timestamps
13
+ end
14
+ end
15
+ end
@@ -1,146 +1,149 @@
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
1
+ module MonitoredProcess
2
+
3
+ class Base < ActiveRecord::Base
4
+ self.table_name = 'monitored_processes'
5
+
6
+ attr_accessor :log_lambda
7
+
8
+ STATE = { :running => 1, :finished => 2, :finished_with_errors => 3 }
9
+
10
+ # VALIDATIONS
11
+ validates_presence_of :name, :state, :pid
12
+
13
+ def self.start_process
14
+ p = MonitoredProcess.new
15
+ p.name = self.to_s
16
+ p.state = STATE[:running]
17
+ p.pid = Process.pid
18
+ p.console_output = ""
19
+ p.save!
20
+
21
+ p.log_lambda = lambda do |message|
22
+ puts "#{DateTime.now} - #{message}"
23
+ end
24
+
25
+ return p
26
+ end
27
+
28
+ def self.last_time_executed
29
+ p = MonitoredProcess.where(:name => self.to_s).last
30
+ return p.updated_at if !p.blank?
31
+ end
32
+
33
+ def self.is_running_by_name? (process_name)
34
+ processes = MonitoredProcess.where(:name => process_name, :state => STATE[:running])
35
+ return false if processes.blank?
36
+
37
+ is_running = false
38
+ processes.each do |process|
39
+ begin
40
+ Process.getpgid(process.pid)
41
+ is_running = true
42
+ rescue Errno::ESRCH
43
+ process.finish_with_errors! "Unknown error / Process was terminated"
44
+ end
45
+ end
46
+ return is_running
47
+ end
48
+
49
+ def self.is_running?
50
+ return self.is_running_by_name? (self.name)
51
+ end
52
+
53
+ def finish!
54
+ set_finished_status
55
+ self.save!
56
+ end
57
+
58
+ def finish
59
+ set_finished_status
60
+ return self.save
61
+ end
62
+
63
+ def finish_with_errors! (error_description)
64
+ self.state = STATE[:finished_with_errors]
65
+ self.error_description = error_description
66
+ self.calculate_elapsed_seconds
67
+ self.save!
68
+ end
69
+
70
+ def calculate_elapsed_time
71
+ begin
72
+ hours_with_fraction = self.elapsed_seconds / 3600.0
73
+ hours = hours_with_fraction.truncate
74
+ minutes_with_fraction = (hours_with_fraction - hours) * 60
75
+ minutes = minutes_with_fraction.truncate
76
+ seconds_with_fraction = (minutes_with_fraction - minutes) * 60
77
+ seconds = seconds_with_fraction.truncate
78
+
79
+ self.elapsed_time = "#{hours.to_s.rjust(2, "0")}:#{minutes.to_s.rjust(2, "0")}:#{seconds.to_s.rjust(2, "0")}"
80
+ rescue
81
+ self.elapsed_time "00:00:00"
82
+ end
83
+ end
84
+
85
+ def calculate_elapsed_seconds
86
+ self.elapsed_seconds = Time.parse(DateTime.now.to_s) - Time.parse(self.created_at.to_s)
87
+
88
+ # Set elapsed time
89
+ self.calculate_elapsed_time
90
+ end
91
+
92
+ def output(text)
93
+ self.console_output += text + "\n"
94
+ log_lambda.call(text) if !log_lambda.blank?
95
+ end
96
+
97
+ def log(text)
98
+ log_lambda.call(text) if !log_lambda.blank?
99
+ end
100
+
101
+ def send_status_email
102
+ MonitoredProcessMailer.process_execution_result_email(self).deliver
103
+ end
104
+
105
+ def self.list(params)
106
+ parameters = []
107
+ parameters = params.clone if !params.blank?
108
+ conditions = []
109
+
110
+ if !params.blank?
111
+ if !params[:start_date].blank?
112
+ conditions << "created_at >= :start_date"
113
+ end
114
+ if !params[:end_date].blank?
115
+ conditions << "created_at <= :end_date"
116
+ end
117
+ if !params[:process_type].blank?
118
+ conditions << "name <= :process_type"
119
+ end
120
+ end
121
+
122
+ parameters[:sort] = "id_desc" if params[:sort].blank?
123
+
124
+ where(conditions.join(" and "), parameters).sorted(parameters[:sort]).paginate(:page => parameters[:page], :per_page => 100)
125
+ end
126
+
127
+ def self.translate_state(state)
128
+ case state
129
+ when STATE[:running]
130
+ "Running"
131
+ when STATE[:finished]
132
+ "Finished"
133
+ when STATE[:finished_with_errors]
134
+ "Finished with errors"
135
+ end
136
+ end
137
+
138
+ def self.get_process_names
139
+ MonitoredProcess.uniq.pluck(:name)
140
+ end
141
+
142
+ private
143
+ def set_finished_status
144
+ self.state = STATE[:finished]
145
+ self.calculate_elapsed_seconds
146
+ end
147
+ end
148
+
149
+ end
@@ -0,0 +1,3 @@
1
+ module MonitoredProcess
2
+ VERSION = "0.0.4"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :monitored_process do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.