-
Notifications
You must be signed in to change notification settings - Fork 19
/
asyncable.rb
268 lines (214 loc) · 6.8 KB
/
asyncable.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# frozen_string_literal: true
# History of this class is in docs/asyncable-models.md
#
# Mixin module to apply to an ActiveRecord class, to make it easier to process via
# an ActiveJob and retry it beyond the retry logic of ActiveJob.
# This becomes necessary when a Job has multiple external service calls, each of
# which may fail and cause retries beyond the "normal" retry window.
# See ClaimReview and RequestIssuesUpdate e.g.
# rubocop:disable Metrics/ModuleLength
module Asyncable
extend ActiveSupport::Concern
include RunAsyncable
included do
has_many :job_notes, as: :job
end
# class methods to scope queries based on class-defined columns
# we expect 5 column types:
# * last_submitted_at : when the job is eligible to run (can be reset to restart the job)
# * submitted_at : when the job first became eligible to run
# * attempted_at : flag the job as having run
# * processed_at : flag the job as concluded
# * error : any error message captured from a failed attempt.
# These column names can be overridden in consuming classes as needed.
class_methods do
REQUIRES_PROCESSING_WINDOW_DAYS = 4
DEFAULT_REQUIRES_PROCESSING_RETRY_WINDOW_HOURS = 3
PROCESS_DELAY_VBMS_OFFSET_HOURS = 7
def processing_retry_interval_hours
self::DEFAULT_REQUIRES_PROCESSING_RETRY_WINDOW_HOURS
end
def requires_processing_until
self::REQUIRES_PROCESSING_WINDOW_DAYS.days.ago
end
def last_submitted_at_column
:last_submitted_at
end
def submitted_at_column
:submitted_at
end
def attempted_at_column
:attempted_at
end
def processed_at_column
:processed_at
end
def error_column
:error
end
def canceled_at_column
:canceled_at
end
def unexpired
where(arel_table[last_submitted_at_column].gt(requires_processing_until))
end
def canceled
where.not(canceled_at_column => nil)
end
def processable
where(arel_table[last_submitted_at_column].lteq(Time.zone.now))
.where(processed_at_column => nil)
.where(canceled_at_column => nil)
end
def never_attempted
where(attempted_at_column => nil).where(
arel_table[last_submitted_at_column].lteq(processing_retry_interval_hours.hours.ago)
)
end
def previously_attempted_ready_for_retry
where(arel_table[attempted_at_column].lt(processing_retry_interval_hours.hours.ago))
end
def attemptable
previously_attempted_ready_for_retry.or(never_attempted).where(canceled_at_column => nil)
end
def order_by_oldest_submitted
order(last_submitted_at_column => :asc)
end
def requires_processing
processable.attemptable.unexpired.order_by_oldest_submitted
end
def expired_without_processing
where(processed_at_column => nil)
.where(arel_table[last_submitted_at_column].lteq(requires_processing_until))
end
def attempted_without_being_submitted
where(arel_table[attempted_at_column].lteq(Time.zone.now)).where(last_submitted_at_column => nil)
end
def with_error
where.not(error_column => nil)
end
def potentially_stuck
processable
.or(with_error)
.or(attempted_without_being_submitted)
.where(canceled_at_column => nil)
.order_by_oldest_submitted
end
def processed
where.not(processed_at_column => nil)
end
def processed_or_canceled
processed.or(canceled)
end
end
def submit_for_processing!(delay: 0)
# One minute offset to prevent "this date is in the future" errors with external services
when_to_start = delay.try(:to_datetime) ? delay.to_datetime + 1.minute : Time.zone.now + delay
# Subtract the `processing_retry_interval_hours` from the delay time,
# to offset its presence in the `previously_attmpted_ready_for_retry` logic.
if delay != 0
when_to_start -= self.class.processing_retry_interval_hours.hours
end
update!(
self.class.last_submitted_at_column => when_to_start,
self.class.submitted_at_column => delay.try(:to_datetime) ? delay.to_datetime : Time.zone.now,
self.class.processed_at_column => nil
)
end
def processed!
update!(self.class.processed_at_column => Time.zone.now) unless processed?
end
def attempted!
update!(self.class.attempted_at_column => Time.zone.now)
end
def canceled!
return if processed?
update!(self.class.canceled_at_column => Time.zone.now)
end
def processed?
!!self[self.class.processed_at_column]
end
def attempted?
!!self[self.class.attempted_at_column]
end
def submitted?
!!self[self.class.submitted_at_column]
end
def canceled?
!!self[self.class.canceled_at_column]
end
def asyncable_status
if processed?
:processed
elsif canceled?
:canceled
elsif attempted?
:attempted
elsif submitted?
:submitted
else
:not_yet_submitted
end
end
def asyncable_user
nil # abstract method intended to be overridden
end
def sanitized_error
# keep PII out of output
(self[self.class.error_column] || "none").gsub(/\s.+/m, "")
end
def expired_without_processing?
return false if processed?
last_submitted = self[self.class.last_submitted_at_column]
return false unless last_submitted
last_submitted < self.class.requires_processing_until
end
def submitted_and_ready?
submitted? && self[self.class.last_submitted_at_column] <= Time.zone.now
end
def submitted_not_processed?
submitted? && !processed?
end
def previously_attempted?
self[self.class.last_submitted_at_column] != self[self.class.submitted_at_column]
end
def sort_by_last_submitted_at
self[self.class.last_submitted_at_column] || Time.zone.now
end
def clear_error!
update!(self.class.error_column => nil)
end
def update_error!(err)
update!(self.class.error_column => err)
end
def restart!
update!(
self.class.last_submitted_at_column => Time.zone.now,
self.class.processed_at_column => nil,
self.class.attempted_at_column => nil,
self.class.canceled_at_column => nil,
self.class.error_column => nil
)
end
def asyncable_ui_hash
{
klass: self.class.to_s,
id: id,
last_submitted_at: self[self.class.last_submitted_at_column],
submitted_at: self[self.class.submitted_at_column],
attempted_at: self[self.class.attempted_at_column],
processed_at: self[self.class.processed_at_column],
canceled_at: self[self.class.canceled_at_column],
error: self[self.class.error_column],
veteran_file_number: try(:veteran).try(:file_number),
user: asyncable_user&.css_id
}
end
def path
"/asyncable_jobs/#{self.class}/jobs/#{id}"
end
def label
"#{self.class} #{id}"
end
end
# rubocop:enable Metrics/ModuleLength