web47command 0.0.3 → 0.0.9

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: 3ec04e840dab56171aaa7e29aab015888e461d1f2487f53dc854b86baebe799f
4
- data.tar.gz: 2cf3644fbd84c46a0caaf5c271ebe293a4a608181bc3ef7ecf189a6090490e63
3
+ metadata.gz: 4265d833318b6297e0ac77b126602c7fe90a32f9a840c92c1029795502bd0fdd
4
+ data.tar.gz: 608abfe1fe0fe4984383174154a8be71f8d4949c173650c2652717126c0c8270
5
5
  SHA512:
6
- metadata.gz: 1840d4ca423bb53b9b86e091f51ee146a444b7bf44dc2904a073f3e9fb8a3cbca247a93dc4d409177f83098ae4ae1ab8e371918f59671e524e054b0aecdb8180
7
- data.tar.gz: 4fe65ce773a652b7cd77954fa35a2aff89d58e54dc00b72ef701f10303b6186b86595b6489560e75132610edc48fa935510702f8fe447d85fe31668a9d78d510
6
+ metadata.gz: c6a5f1c5cb1d0370ee03360f736d71f5c837beeb9eebcc44753266075b50a6eb97c3d91b862383862ce2b8c227c63e3aa7e1ea430b6faf1c818ab2b5b34ec74d
7
+ data.tar.gz: 795a1a37742073f7e4f9663853e27e19b7972a4c83d07a94040e109060dbe128800ca1a3308410e12bfd7fc87f1195a3bdfe5055b3cd52a929a28c64129ab2f9
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`
@@ -25,15 +25,25 @@ class CommandJob
25
25
  field :max_retries, type: Integer, default: 5
26
26
  field :result, type: String
27
27
  field :error_message, type: String
28
+ field :started_at, type: Time
29
+ field :finished_at, type: Time
28
30
  #
29
31
  # Relationships
30
32
  #
31
33
  has_many :logs, class_name: 'CommandJobLog', dependent: :destroy
34
+ belongs_to :started_by, class_name: 'User', optional: true
32
35
  #
33
36
  # Validations
34
37
  #
35
38
  validates :state, inclusion: { in: ALL_STATES }
36
39
 
40
+ #
41
+ # Who started this job
42
+ #
43
+ def display_started_by
44
+ started_by.present? ? started_by.name : 'System'
45
+ end
46
+
37
47
  #
38
48
  # Default time to keep a job before auto archiving it
39
49
  #
@@ -52,113 +62,165 @@ class CommandJob
52
62
  # True if in new status
53
63
  #
54
64
  def new_job?
55
- STATE_NEW.eql?(state)
65
+ job_state?(STATE_NEW)
56
66
  end
57
67
 
58
68
  #
59
69
  # True if in WIP status
60
70
  #
61
71
  def work_in_progress?
62
- [STATE_RETRYING, STATE_WIP].include?(state)
72
+ job_state?([STATE_WIP, STATE_RETRYING])
63
73
  end
64
74
 
65
75
  #
66
76
  # True if in success status
67
77
  #
68
78
  def succeeded?
69
- STATE_SUCCESS.eql?(state)
79
+ job_state?(STATE_SUCCESS)
70
80
  end
71
81
 
72
82
  #
73
83
  # True if in fail status
74
84
  #
75
- def failed?
76
- STATE_FAIL.eql?(state)
85
+ def failure?
86
+ job_state?(STATE_FAIL)
77
87
  end
78
88
 
79
89
  #
80
90
  # If we is finished, failed or success
81
91
  #
82
92
  def completed?
83
- [STATE_CANCELLED, STATE_FAIL, STATE_SUCCESS].include?(state)
93
+ job_state?([STATE_CANCELLED, STATE_FAIL, STATE_SUCCESS])
84
94
  end
85
95
 
86
96
  #
87
97
  # Job has not finished, failure or success
88
98
  #
89
- def incomplete?
99
+ def running?
90
100
  !completed?
91
101
  end
92
102
 
93
- alias running? incomplete?
103
+ alias incomplete? running?
94
104
 
95
105
  #
96
106
  # If we are cancelled
97
107
  #
98
108
  def cancelled?
99
- STATE_CANCELLED.eql?(state)
109
+ job_state?(STATE_CANCELLED)
100
110
  end
101
111
 
102
- def job_cancelled?(job = self)
103
- job.reload
104
- job.cancelled?
105
- rescue StandardError => error
106
- App47Logger.log_warn "Unable to check job cancelled #{job}", error
107
- true
112
+ def failure_or_cancelled?
113
+ job_state?([STATE_FAIL, STATE_CANCELLED], true)
108
114
  end
109
115
 
110
- def failure_or_cancelled?(job = self)
111
- job.reload
112
- 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)
113
123
  rescue StandardError => error
114
- App47Logger.log_warn "Unable to check job cancelled #{job}", error
115
- true
124
+ App47Logger.log_warn "Unable to check job failed or cancelled #{self.inspect}", error
125
+ default
116
126
  end
117
127
 
118
128
  #
119
129
  # Return the job's status and information in a hash that could be used to return to a calling
120
130
  # api
121
131
  #
122
- def status
132
+ def current_status
123
133
  status = { state: state }
124
134
  status[:message] = error_message if error_message.present?
125
135
  status
126
136
  end
127
137
 
128
- def perform
129
- if fail?
130
- self.retries = 0
131
- 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
132
174
  end
175
+ end
133
176
 
134
- begin
135
- save!
136
- run
137
- rescue StandardError => error
138
- log_error 'Unable to start job', error
139
- 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
140
188
  end
141
189
  end
142
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
+ #
208
+ # Run the job, handling any failures that might happen
209
+ #
143
210
  def run
144
- set state: STATE_WIP
145
- # Run the job
146
- run!
147
- set error_message: '', state: STATE_SUCCESS
211
+ run! unless cancelled?
148
212
  rescue StandardError => error
149
- if retries >= max_retries
213
+ if (retries + 1) >= max_retries
150
214
  log_error "Unable to run job id: #{id}, done retrying", error
151
215
  set state: STATE_FAIL, error_message: "Failed final attempt: #{error.message}"
152
216
  else
153
217
  log_error "Unable to run job id: #{id}, retrying!!", error
154
218
  add_log "Unable to run job: #{error.message}, retrying!!"
155
- set error_message: "Failed attempt # #{retries}: #{error.message}", retries: retries + 1
156
- delay(run_at: 10.seconds.from_now).run
219
+ set error_message: "Failed attempt # #{retries}: #{error.message}", retries: retries + 1, state: STATE_RETRYING
220
+ run
157
221
  end
158
222
  end
159
223
 
160
- handle_asynchronously :run if Delayed::Worker.delay_jobs
161
-
162
224
  #
163
225
  # Determine the correct action to take and get it started
164
226
  #
@@ -253,7 +315,11 @@ class CommandJob
253
315
  end
254
316
  output = 'Success' if output.blank?
255
317
  command = mask_keywords(command, options[:mask_texts])
256
- logs.create!(dir: dir, command: command, message: output)
318
+ if block_given?
319
+ yield output
320
+ else
321
+ logs.create!(dir: dir, command: command, message: output)
322
+ end
257
323
  check_for_text(output, options[:error_texts], true)
258
324
  check_for_text(output, options[:required_texts], false)
259
325
  output
@@ -301,4 +367,5 @@ class CommandJob
301
367
  def sort_fields
302
368
  %i[created_at]
303
369
  end
370
+
304
371
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Web47command
4
- VERSION = '0.0.3'
4
+ VERSION = '0.0.9'
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.3
4
+ version: 0.0.9
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-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport