web47command 0.0.6 → 0.0.8

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: 964c4beeac4360b088953ffefa4accfff297bf115db494ae1f48374a1f3bc749
4
- data.tar.gz: 5b3abd477f99613639344b0da59f2fc9499550b07298dbbbc6691c2870b60039
3
+ metadata.gz: e4c9af98c7b86e879a9885c598141652ba58bf95a2c356067d55e3af5e8178c3
4
+ data.tar.gz: cc1fcf8e729041d1273908c1da76bbd9810af5206d234335aab5b56fd7ff0587
5
5
  SHA512:
6
- metadata.gz: 10d7cf516421d02777e981bf78a5fd7d16259df6b3bf2b91ff58657eebf3e84f37ead93e1f18f5c9bf3f913ddf868c2d3a085a68c9f2304c93bc07d137b27a2d
7
- data.tar.gz: d6e185ef29dfc1da47cce099da1826462c454601e1f5ab335ad81e79e7cdd25ff9c78776a965a786ef749534ef529a6be8638b0084a1722a421a32c09d6045c2
6
+ metadata.gz: d36c06edcaf174da4ff360a2842e86c6bd1c6c47ced6cb60567fa0623c279bbffda43bee966f479226b1928434aa62309964f5b7214d27236d232a86adb570c0
7
+ data.tar.gz: d4a92dd44cd66e1f352dcdc64bd076c32453c09989ef693d15601710584eec3d8f2b7e690ec4f636d0b2404013428c89510157f00597b3093a41374769c59b12
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,158 @@ 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.failed? || 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 failed?
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
+ else
166
+ set retries: 0, started_at: Time.now.utc, finished_at: nil, result: nil
142
167
  end
168
+ end
143
169
 
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
170
+ #
171
+ # Steps to execute after a run
172
+ #
173
+ def after_run
174
+ case state
175
+ when STATE_RETRYING, STATE_WIP
176
+ set finished_at: Time.now.utc, error_message: nil, state: STATE_SUCCESS
177
+ when STATE_SUCCESS
178
+ set finished_at: Time.now.utc, error_message: nil
179
+ else
180
+ set finished_at: Time.now.utc
150
181
  end
151
182
  end
152
183
 
184
+ #
185
+ #
186
+ # Perform the command job
187
+ #
188
+ def perform
189
+ before_run
190
+ run
191
+ after_run
192
+ rescue StandardError => error
193
+ log_error 'Unable to start job', error
194
+ set state: STATE_FAIL, error_message: error.message
195
+ end
196
+
197
+ alias perform_now perform
198
+
199
+
200
+ #
201
+ # Run the job, handling any failures that might happen
202
+ #
153
203
  def run
154
- set state: STATE_WIP, started_at: Time.now.utc
155
- # Run the job
156
- run!
157
- set error_message: '', state: STATE_SUCCESS, finished_at: Time.now.utc
204
+ run! unless cancelled?
158
205
  rescue StandardError => error
159
- if retries >= max_retries
206
+ if (retries + 1) >= max_retries
160
207
  log_error "Unable to run job id: #{id}, done retrying", error
161
- set state: STATE_FAIL, error_message: "Failed final attempt: #{error.message}", finished_at: Time.now.utc
208
+ set state: STATE_FAIL, error_message: "Failed final attempt: #{error.message}"
162
209
  else
163
210
  log_error "Unable to run job id: #{id}, retrying!!", error
164
211
  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
212
+ set error_message: "Failed attempt # #{retries}: #{error.message}", retries: retries + 1, state: STATE_RETRYING
213
+ run
167
214
  end
168
215
  end
169
216
 
170
- handle_asynchronously :run if Delayed::Worker.delay_jobs
171
-
172
217
  #
173
218
  # Determine the correct action to take and get it started
174
219
  #
@@ -311,4 +356,5 @@ class CommandJob
311
356
  def sort_fields
312
357
  %i[created_at]
313
358
  end
359
+
314
360
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Web47command
4
- VERSION = '0.0.6'
4
+ VERSION = '0.0.8'
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.6
4
+ version: 0.0.8
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: 2020-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport