web47command 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f7a5d228fd6711bc83e6321edb8488455218fd1c88fd0f2002b28b56806515a5
4
- data.tar.gz: 32044f2e427b2d62e50f35801291ca26a32bf875cf066267f7592602a4059758
3
+ metadata.gz: 507c57273933c17f93c493bc950163b4434ffc5ac3a0396ee83aabc3c60d9355
4
+ data.tar.gz: 2036dbb4fcb481b60c377c91dd2eaec8b9fbf61628526a063358b1b7bab58b95
5
5
  SHA512:
6
- metadata.gz: d642520ec8e7aa6dab40703a8228cd05917f87a4a6ad2ba606fb8434dbd59497f96b8d30682e13f26a5356c5c5401635ff925b706b9061954fe078baeb40f6fe
7
- data.tar.gz: aadce6c8f98a4d7ef683d4c0591be8371bfeae8fe5236a0c366a8b5a6238a4ca6c642d2275cca8bc87d5904b497dd06cb583f912588d608311bcd889d9f060d7
6
+ metadata.gz: 391a11146443d5312c30d595a2e494339f1461854117512a9ebd1cb13b24f32c25f91fff710290265e94b774ecc27c4b31437e5cc3e48953110ca20fee965991
7
+ data.tar.gz: 16b2084ce7f199e310462056e386d69fb559ef227b20e48b1f4b92e169826a9543c24e13e61fba045b30935ddb7febd5f8bc1b1283bc84fa6fc5e37ec49d731a
data/README.md CHANGED
@@ -73,3 +73,5 @@ _Please do not ship to production code using the git repo, as the production ser
73
73
  2. `JobLog`
74
74
  3. `CommandLogLog`
75
75
  4. `TrimJobs`
76
+ ### Remove the following files
77
+ 1. Change `status` on a job to `current_status`
@@ -41,7 +41,7 @@ class CommandJob
41
41
  # Who started this job
42
42
  #
43
43
  def display_started_by
44
- started_by.present? ? started_by.display_name : 'System'
44
+ started_by.present? ? started_by.name : 'System'
45
45
  end
46
46
 
47
47
  #
@@ -62,113 +62,164 @@ class CommandJob
62
62
  # True if in new status
63
63
  #
64
64
  def new_job?
65
- STATE_NEW.eql?(state)
65
+ job_state?(STATE_NEW)
66
66
  end
67
67
 
68
68
  #
69
69
  # True if in WIP status
70
70
  #
71
71
  def work_in_progress?
72
- [STATE_RETRYING, STATE_WIP].include?(state)
72
+ job_state?([STATE_WIP, STATE_RETRYING])
73
73
  end
74
74
 
75
75
  #
76
76
  # True if in success status
77
77
  #
78
78
  def succeeded?
79
- STATE_SUCCESS.eql?(state)
79
+ job_state?(STATE_SUCCESS)
80
80
  end
81
81
 
82
82
  #
83
83
  # True if in fail status
84
84
  #
85
- def failed?
86
- STATE_FAIL.eql?(state)
85
+ def failure?
86
+ job_state?(STATE_FAIL)
87
87
  end
88
88
 
89
89
  #
90
90
  # If we is finished, failed or success
91
91
  #
92
92
  def completed?
93
- [STATE_CANCELLED, STATE_FAIL, STATE_SUCCESS].include?(state)
93
+ job_state?([STATE_CANCELLED, STATE_FAIL, STATE_SUCCESS])
94
94
  end
95
95
 
96
96
  #
97
97
  # Job has not finished, failure or success
98
98
  #
99
- def incomplete?
99
+ def running?
100
100
  !completed?
101
101
  end
102
102
 
103
- alias running? incomplete?
103
+ alias incomplete? running?
104
104
 
105
105
  #
106
106
  # If we are cancelled
107
107
  #
108
108
  def cancelled?
109
- STATE_CANCELLED.eql?(state)
109
+ job_state?(STATE_CANCELLED)
110
110
  end
111
111
 
112
- def job_cancelled?(job = self)
113
- job.reload
114
- job.cancelled?
115
- rescue StandardError => error
116
- App47Logger.log_warn "Unable to check job cancelled #{job}", error
117
- true
112
+ def failure_or_cancelled?
113
+ job_state?([STATE_FAIL, STATE_CANCELLED], true)
118
114
  end
119
115
 
120
- def failure_or_cancelled?(job = self)
121
- job.reload
122
- job.failure? || job.cancelled?
116
+ #
117
+ # Fetch the latest version of this instance from the database and check the state against the required
118
+ # state. If there is a match, then return true, otherwise return false.
119
+ # If there is an error, return the default.
120
+ #
121
+ def job_state?(states, default = false)
122
+ states.is_a?(Array) ? states.include?(state) : states.eql?(state)
123
123
  rescue StandardError => error
124
- App47Logger.log_warn "Unable to check job cancelled #{job}", error
125
- true
124
+ App47Logger.log_warn "Unable to check job failed or cancelled #{self.inspect}", error
125
+ default
126
126
  end
127
127
 
128
128
  #
129
129
  # Return the job's status and information in a hash that could be used to return to a calling
130
130
  # api
131
131
  #
132
- def status
132
+ def current_status
133
133
  status = { state: state }
134
134
  status[:message] = error_message if error_message.present?
135
135
  status
136
136
  end
137
137
 
138
- def perform
139
- if fail?
140
- self.retries = 0
141
- self.error_message = 'Retrying'
138
+ #
139
+ # Perform this job in the background
140
+ #
141
+ def perform_later
142
+ perform
143
+ end
144
+
145
+ handle_asynchronously :perform_later
146
+
147
+ #
148
+ # Steps to execute before a run
149
+ #
150
+ def before_run
151
+ case state
152
+ when STATE_NEW
153
+ set retries: 0,
154
+ started_at: Time.now.utc,
155
+ finished_at: nil,
156
+ error_message: nil,
157
+ result: nil,
158
+ state: STATE_WIP
159
+ when STATE_RETRYING
160
+ set retries: 0,
161
+ started_at: Time.now.utc,
162
+ finished_at: nil,
163
+ error_message: nil,
164
+ result: nil
165
+ when STATE_FAIL
166
+ set retries: 0,
167
+ started_at: Time.now.utc,
168
+ finished_at: nil,
169
+ error_message: nil,
170
+ state: STATE_RETRYING,
171
+ result: nil
172
+ else
173
+ set retries: 0, started_at: Time.now.utc, finished_at: nil, result: nil
142
174
  end
175
+ end
143
176
 
144
- begin
145
- save!
146
- run
147
- rescue StandardError => error
148
- log_error 'Unable to start job', error
149
- set state: STATE_FAIL, error_message: error.message
177
+ #
178
+ # Steps to execute after a run
179
+ #
180
+ def after_run
181
+ case state
182
+ when STATE_RETRYING, STATE_WIP
183
+ set finished_at: Time.now.utc, error_message: nil, state: STATE_SUCCESS
184
+ when STATE_SUCCESS
185
+ set finished_at: Time.now.utc, error_message: nil
186
+ else
187
+ set finished_at: Time.now.utc
150
188
  end
151
189
  end
152
190
 
191
+ #
192
+ #
193
+ # Perform the command job
194
+ #
195
+ def perform
196
+ before_run
197
+ run
198
+ after_run
199
+ rescue StandardError => error
200
+ log_error 'Unable to start job', error
201
+ set state: STATE_FAIL, error_message: error.message
202
+ end
203
+
204
+ alias perform_now perform
205
+
206
+ #
207
+ # Run the job, handling any failures that might happen
208
+ #
153
209
  def run
154
- set state: STATE_WIP
155
- # Run the job
156
- run!
157
- set error_message: '', state: STATE_SUCCESS
210
+ run! unless cancelled?
158
211
  rescue StandardError => error
159
- if retries >= max_retries
212
+ if (retries + 1) >= max_retries
160
213
  log_error "Unable to run job id: #{id}, done retrying", error
161
214
  set state: STATE_FAIL, error_message: "Failed final attempt: #{error.message}"
162
215
  else
163
216
  log_error "Unable to run job id: #{id}, retrying!!", error
164
217
  add_log "Unable to run job: #{error.message}, retrying!!"
165
- set error_message: "Failed attempt # #{retries}: #{error.message}", retries: retries + 1
166
- delay(run_at: 10.seconds.from_now).run
218
+ set error_message: "Failed attempt # #{retries}: #{error.message}", retries: retries + 1, state: STATE_RETRYING
219
+ run
167
220
  end
168
221
  end
169
222
 
170
- handle_asynchronously :run if Delayed::Worker.delay_jobs
171
-
172
223
  #
173
224
  # Determine the correct action to take and get it started
174
225
  #
@@ -191,6 +242,8 @@ class CommandJob
191
242
  download = URI.parse(file_url).open
192
243
  IO.copy_stream(download, file_path)
193
244
  add_log "Downloaded file: #{file_url} to #{file_path}"
245
+ rescue StandardError => error
246
+ raise "Unable to download file from #{file_url} to #{file_path}, error: ##{error.message}"
194
247
  end
195
248
 
196
249
  #
@@ -203,6 +256,8 @@ class CommandJob
203
256
  else
204
257
  add_log "File not found: #{from_path}, copy not performed"
205
258
  end
259
+ rescue StandardError => error
260
+ raise "Unable to copy file from #{from_path} to #{to_path}, error: ##{error.message}"
206
261
  end
207
262
 
208
263
  #
@@ -263,7 +318,11 @@ class CommandJob
263
318
  end
264
319
  output = 'Success' if output.blank?
265
320
  command = mask_keywords(command, options[:mask_texts])
266
- logs.create!(dir: dir, command: command, message: output)
321
+ if block_given?
322
+ yield output
323
+ else
324
+ logs.create!(dir: dir, command: command, message: output)
325
+ end
267
326
  check_for_text(output, options[:error_texts], true)
268
327
  check_for_text(output, options[:required_texts], false)
269
328
  output
@@ -311,4 +370,5 @@ class CommandJob
311
370
  def sort_fields
312
371
  %i[created_at]
313
372
  end
373
+
314
374
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Web47command
4
- VERSION = '0.0.4'
4
+ VERSION = '0.1.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: web47command
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Schroeder
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-11 00:00:00.000000000 Z
11
+ date: 2021-01-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport