good_job 3.29.5 → 3.30.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +32 -0
- data/README.md +3 -3
- data/app/controllers/good_job/performances_controller.rb +23 -0
- data/app/filters/good_job/base_filter.rb +1 -1
- data/app/helpers/good_job/application_helper.rb +1 -0
- data/app/models/concerns/good_job/error_events.rb +2 -2
- data/app/models/good_job/base_execution.rb +594 -2
- data/app/models/good_job/discrete_execution.rb +18 -2
- data/app/models/good_job/execution.rb +2 -597
- data/app/models/good_job/job.rb +1 -1
- data/app/views/good_job/performances/show.html.erb +50 -0
- data/app/views/good_job/shared/_navbar.erb +5 -0
- data/config/locales/de.yml +11 -0
- data/config/locales/en.yml +11 -0
- data/config/locales/es.yml +11 -0
- data/config/locales/fr.yml +11 -0
- data/config/locales/it.yml +11 -0
- data/config/locales/ja.yml +11 -0
- data/config/locales/ko.yml +11 -0
- data/config/locales/nl.yml +11 -0
- data/config/locales/pt-BR.yml +11 -0
- data/config/locales/ru.yml +11 -0
- data/config/locales/tr.yml +11 -0
- data/config/locales/uk.yml +11 -0
- data/config/routes.rb +2 -0
- data/lib/generators/good_job/templates/install/migrations/create_good_jobs.rb.erb +1 -0
- data/lib/generators/good_job/templates/update/migrations/13_create_good_job_process_lock_ids.rb.erb +1 -0
- data/lib/generators/good_job/templates/update/migrations/14_create_good_job_process_lock_indexes.rb.erb +1 -0
- data/lib/generators/good_job/templates/update/migrations/15_create_good_job_execution_duration.rb.erb +15 -0
- data/lib/good_job/version.rb +1 -1
- data/lib/good_job.rb +2 -2
- metadata +5 -2
@@ -2,606 +2,11 @@
|
|
2
2
|
|
3
3
|
module GoodJob
|
4
4
|
# Active Record model that represents an +ActiveJob+ job.
|
5
|
+
# Most behavior is currently in BaseExecution in anticipation of
|
6
|
+
# moving behavior into +GoodJob::Job+.
|
5
7
|
class Execution < BaseExecution
|
6
|
-
# Raised if something attempts to execute a previously completed Execution again.
|
7
|
-
PreviouslyPerformedError = Class.new(StandardError)
|
8
|
-
|
9
|
-
# String separating Error Class from Error Message
|
10
|
-
ERROR_MESSAGE_SEPARATOR = ": "
|
11
|
-
|
12
|
-
# ActiveJob jobs without a +queue_name+ attribute are placed on this queue.
|
13
|
-
DEFAULT_QUEUE_NAME = 'default'
|
14
|
-
# ActiveJob jobs without a +priority+ attribute are given this priority.
|
15
|
-
DEFAULT_PRIORITY = 0
|
16
|
-
|
17
8
|
self.table_name = 'good_jobs'
|
18
|
-
self.advisory_lockable_column = 'active_job_id'
|
19
|
-
self.implicit_order_column = 'created_at'
|
20
|
-
|
21
|
-
define_model_callbacks :perform
|
22
|
-
define_model_callbacks :perform_unlocked, only: :after
|
23
|
-
|
24
|
-
set_callback :perform, :around, :reset_batch_values
|
25
|
-
set_callback :perform_unlocked, :after, :continue_discard_or_finish_batch
|
26
|
-
|
27
|
-
# Parse a string representing a group of queues into a more readable data
|
28
|
-
# structure.
|
29
|
-
# @param string [String] Queue string
|
30
|
-
# @return [Hash]
|
31
|
-
# How to match a given queue. It can have the following keys and values:
|
32
|
-
# - +{ all: true }+ indicates that all queues match.
|
33
|
-
# - +{ exclude: Array<String> }+ indicates the listed queue names should
|
34
|
-
# not match.
|
35
|
-
# - +{ include: Array<String> }+ indicates the listed queue names should
|
36
|
-
# match.
|
37
|
-
# - +{ include: Array<String>, ordered_queues: true }+ indicates the listed
|
38
|
-
# queue names should match, and dequeue should respect queue order.
|
39
|
-
# @example
|
40
|
-
# GoodJob::Execution.queue_parser('-queue1,queue2')
|
41
|
-
# => { exclude: [ 'queue1', 'queue2' ] }
|
42
|
-
def self.queue_parser(string)
|
43
|
-
string = string.strip.presence || '*'
|
44
9
|
|
45
|
-
case string.first
|
46
|
-
when '-'
|
47
|
-
exclude_queues = true
|
48
|
-
string = string[1..]
|
49
|
-
when '+'
|
50
|
-
ordered_queues = true
|
51
|
-
string = string[1..]
|
52
|
-
end
|
53
|
-
|
54
|
-
queues = string.split(',').map(&:strip)
|
55
|
-
|
56
|
-
if queues.include?('*')
|
57
|
-
{ all: true }
|
58
|
-
elsif exclude_queues
|
59
|
-
{ exclude: queues }
|
60
|
-
elsif ordered_queues
|
61
|
-
{
|
62
|
-
include: queues,
|
63
|
-
ordered_queues: true,
|
64
|
-
}
|
65
|
-
else
|
66
|
-
{ include: queues }
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
belongs_to :batch, class_name: 'GoodJob::BatchRecord', optional: true, inverse_of: :executions
|
71
10
|
belongs_to :job, class_name: 'GoodJob::Job', foreign_key: 'active_job_id', primary_key: 'active_job_id', optional: true, inverse_of: :executions
|
72
|
-
has_many :discrete_executions, class_name: 'GoodJob::DiscreteExecution', foreign_key: 'active_job_id', primary_key: 'active_job_id', inverse_of: :execution # rubocop:disable Rails/HasManyOrHasOneDependent
|
73
|
-
|
74
|
-
after_destroy lambda {
|
75
|
-
GoodJob::DiscreteExecution.where(active_job_id: active_job_id).delete_all if discrete? # TODO: move into association `dependent: :delete_all` after v4
|
76
|
-
self.class.active_job_id(active_job_id).delete_all
|
77
|
-
}, if: -> { @_destroy_job }
|
78
|
-
|
79
|
-
# Get executions with given ActiveJob ID
|
80
|
-
# @!method active_job_id(active_job_id)
|
81
|
-
# @!scope class
|
82
|
-
# @param active_job_id [String]
|
83
|
-
# ActiveJob ID
|
84
|
-
# @return [ActiveRecord::Relation]
|
85
|
-
scope :active_job_id, ->(active_job_id) { where(active_job_id: active_job_id) }
|
86
|
-
|
87
|
-
# Get executions that have not yet finished (succeeded or discarded).
|
88
|
-
# @!method unfinished
|
89
|
-
# @!scope class
|
90
|
-
# @return [ActiveRecord::Relation]
|
91
|
-
scope :unfinished, -> { where(finished_at: nil) }
|
92
|
-
|
93
|
-
# Get executions that are not scheduled for a later time than now (i.e. jobs that
|
94
|
-
# are not scheduled or scheduled for earlier than the current time).
|
95
|
-
# @!method only_scheduled
|
96
|
-
# @!scope class
|
97
|
-
# @return [ActiveRecord::Relation]
|
98
|
-
scope :only_scheduled, -> { where(arel_table['scheduled_at'].lteq(bind_value('scheduled_at', DateTime.current, ActiveRecord::Type::DateTime))).or(where(scheduled_at: nil)) }
|
99
|
-
|
100
|
-
# Order executions by priority (highest priority first).
|
101
|
-
# @!method priority_ordered
|
102
|
-
# @!scope class
|
103
|
-
# @return [ActiveRecord::Relation]
|
104
|
-
scope :priority_ordered, (lambda do
|
105
|
-
if GoodJob.configuration.smaller_number_is_higher_priority
|
106
|
-
order('priority ASC NULLS LAST')
|
107
|
-
else
|
108
|
-
order('priority DESC NULLS LAST')
|
109
|
-
end
|
110
|
-
end)
|
111
|
-
|
112
|
-
# Order executions by created_at, for first-in first-out
|
113
|
-
# @!method creation_ordered
|
114
|
-
# @!scope class
|
115
|
-
# @return [ActiveRecord:Relation]
|
116
|
-
scope :creation_ordered, -> { order(created_at: :asc) }
|
117
|
-
|
118
|
-
# Order executions for de-queueing
|
119
|
-
# @!method dequeueing_ordered(parsed_queues)
|
120
|
-
# @!scope class
|
121
|
-
# @param parsed_queues [Hash]
|
122
|
-
# optional output of .queue_parser, parsed queues, will be used for
|
123
|
-
# ordered queues.
|
124
|
-
# @return [ActiveRecord::Relation]
|
125
|
-
scope :dequeueing_ordered, (lambda do |parsed_queues|
|
126
|
-
relation = self
|
127
|
-
relation = relation.queue_ordered(parsed_queues[:include]) if parsed_queues && parsed_queues[:ordered_queues] && parsed_queues[:include]
|
128
|
-
relation = relation.priority_ordered.creation_ordered
|
129
|
-
|
130
|
-
relation
|
131
|
-
end)
|
132
|
-
|
133
|
-
# Order executions in order of queues in array param
|
134
|
-
# @!method queue_ordered(queues)
|
135
|
-
# @!scope class
|
136
|
-
# @param queues [Array<string] ordered names of queues
|
137
|
-
# @return [ActiveRecord::Relation]
|
138
|
-
scope :queue_ordered, (lambda do |queues|
|
139
|
-
clauses = queues.map.with_index do |queue_name, index|
|
140
|
-
"WHEN queue_name = '#{queue_name}' THEN #{index}"
|
141
|
-
end
|
142
|
-
|
143
|
-
order(
|
144
|
-
Arel.sql("(CASE #{clauses.join(' ')} ELSE #{queues.length} END)")
|
145
|
-
)
|
146
|
-
end)
|
147
|
-
|
148
|
-
# Order jobs by scheduled or created (oldest first).
|
149
|
-
# @!method schedule_ordered
|
150
|
-
# @!scope class
|
151
|
-
# @return [ActiveRecord::Relation]
|
152
|
-
scope :schedule_ordered, -> { order(coalesce_scheduled_at_created_at.asc) }
|
153
|
-
|
154
|
-
# Get completed jobs before the given timestamp. If no timestamp is
|
155
|
-
# provided, get *all* completed jobs. By default, GoodJob
|
156
|
-
# destroys jobs after they're completed, meaning this returns no jobs.
|
157
|
-
# However, if you have changed {GoodJob.preserve_job_records}, this may
|
158
|
-
# find completed Jobs.
|
159
|
-
# @!method finished(timestamp = nil)
|
160
|
-
# @!scope class
|
161
|
-
# @param timestamp (Float)
|
162
|
-
# Get jobs that finished before this time (in epoch time).
|
163
|
-
# @return [ActiveRecord::Relation]
|
164
|
-
scope :finished, ->(timestamp = nil) { timestamp ? where(arel_table['finished_at'].lteq(bind_value('finished_at', timestamp, ActiveRecord::Type::DateTime))) : where.not(finished_at: nil) }
|
165
|
-
|
166
|
-
# Get Jobs that started but not finished yet.
|
167
|
-
# @!method running
|
168
|
-
# @!scope class
|
169
|
-
# @return [ActiveRecord::Relation]
|
170
|
-
scope :running, -> { where.not(performed_at: nil).where(finished_at: nil) }
|
171
|
-
|
172
|
-
# Get Jobs that do not have subsequent retries
|
173
|
-
# @!method running
|
174
|
-
# @!scope class
|
175
|
-
# @return [ActiveRecord::Relation]
|
176
|
-
scope :head, -> { where(retried_good_job_id: nil) }
|
177
|
-
|
178
|
-
# Get Jobs have errored that will not be retried further
|
179
|
-
# @!method running
|
180
|
-
# @!scope class
|
181
|
-
# @return [ActiveRecord::Relation]
|
182
|
-
scope :dead, -> { head.where.not(error: nil) }
|
183
|
-
|
184
|
-
# Get Jobs on queues that match the given queue string.
|
185
|
-
# @!method queue_string(string)
|
186
|
-
# @!scope class
|
187
|
-
# @param string [String]
|
188
|
-
# A string expression describing what queues to select. See
|
189
|
-
# {Execution.queue_parser} or
|
190
|
-
# {file:README.md#optimize-queues-threads-and-processes} for more details
|
191
|
-
# on the format of the string. Note this only handles individual
|
192
|
-
# semicolon-separated segments of that string format.
|
193
|
-
# @return [ActiveRecord::Relation]
|
194
|
-
scope :queue_string, (lambda do |string|
|
195
|
-
parsed = queue_parser(string)
|
196
|
-
|
197
|
-
if parsed[:all]
|
198
|
-
all
|
199
|
-
elsif parsed[:exclude]
|
200
|
-
where.not(queue_name: parsed[:exclude]).or where(queue_name: nil)
|
201
|
-
elsif parsed[:include]
|
202
|
-
where(queue_name: parsed[:include])
|
203
|
-
end
|
204
|
-
end)
|
205
|
-
|
206
|
-
def self.build_for_enqueue(active_job, overrides = {})
|
207
|
-
new(**enqueue_args(active_job, overrides))
|
208
|
-
end
|
209
|
-
|
210
|
-
# Construct arguments for GoodJob::Execution from an ActiveJob instance.
|
211
|
-
def self.enqueue_args(active_job, overrides = {})
|
212
|
-
if active_job.priority && GoodJob.configuration.smaller_number_is_higher_priority.nil?
|
213
|
-
GoodJob.deprecator.warn(<<~DEPRECATION)
|
214
|
-
The next major version of GoodJob (v4.0) will change job `priority` to give smaller numbers higher priority (default: `0`), in accordance with Active Job's definition of priority.
|
215
|
-
To opt-in to this behavior now, set `config.good_job.smaller_number_is_higher_priority = true` in your GoodJob initializer or application.rb.
|
216
|
-
To not opt-in yet, but silence this deprecation warning, set `config.good_job.smaller_number_is_higher_priority = false`.
|
217
|
-
DEPRECATION
|
218
|
-
end
|
219
|
-
|
220
|
-
execution_args = {
|
221
|
-
active_job_id: active_job.job_id,
|
222
|
-
queue_name: active_job.queue_name.presence || DEFAULT_QUEUE_NAME,
|
223
|
-
priority: active_job.priority || DEFAULT_PRIORITY,
|
224
|
-
serialized_params: active_job.serialize,
|
225
|
-
}
|
226
|
-
execution_args[:scheduled_at] = Time.zone.at(active_job.scheduled_at) if active_job.scheduled_at
|
227
|
-
execution_args[:concurrency_key] = active_job.good_job_concurrency_key if active_job.respond_to?(:good_job_concurrency_key)
|
228
|
-
|
229
|
-
if active_job.respond_to?(:good_job_labels) && active_job.good_job_labels.any? && labels_migrated?
|
230
|
-
labels = active_job.good_job_labels.dup
|
231
|
-
labels.map! { |label| label.to_s.strip.presence }
|
232
|
-
labels.tap(&:compact!).tap(&:uniq!)
|
233
|
-
execution_args[:labels] = labels
|
234
|
-
end
|
235
|
-
|
236
|
-
reenqueued_current_execution = CurrentThread.active_job_id && CurrentThread.active_job_id == active_job.job_id
|
237
|
-
current_execution = CurrentThread.execution
|
238
|
-
|
239
|
-
if reenqueued_current_execution
|
240
|
-
if GoodJob::BatchRecord.migrated?
|
241
|
-
execution_args[:batch_id] = current_execution.batch_id
|
242
|
-
execution_args[:batch_callback_id] = current_execution.batch_callback_id
|
243
|
-
end
|
244
|
-
execution_args[:cron_key] = current_execution.cron_key
|
245
|
-
else
|
246
|
-
if GoodJob::BatchRecord.migrated?
|
247
|
-
execution_args[:batch_id] = GoodJob::Batch.current_batch_id
|
248
|
-
execution_args[:batch_callback_id] = GoodJob::Batch.current_batch_callback_id
|
249
|
-
end
|
250
|
-
execution_args[:cron_key] = CurrentThread.cron_key
|
251
|
-
execution_args[:cron_at] = CurrentThread.cron_at
|
252
|
-
end
|
253
|
-
|
254
|
-
execution_args.merge(overrides)
|
255
|
-
end
|
256
|
-
|
257
|
-
# Finds the next eligible Execution, acquire an advisory lock related to it, and
|
258
|
-
# executes the job.
|
259
|
-
# @yield [Execution, nil] The next eligible Execution, or +nil+ if none found, before it is performed.
|
260
|
-
# @return [ExecutionResult, nil]
|
261
|
-
# If a job was executed, returns an array with the {Execution} record, the
|
262
|
-
# return value for the job's +#perform+ method, and the exception the job
|
263
|
-
# raised, if any (if the job raised, then the second array entry will be
|
264
|
-
# +nil+). If there were no jobs to execute, returns +nil+.
|
265
|
-
def self.perform_with_advisory_lock(lock_id:, parsed_queues: nil, queue_select_limit: nil)
|
266
|
-
execution = nil
|
267
|
-
result = nil
|
268
|
-
|
269
|
-
unfinished.dequeueing_ordered(parsed_queues).only_scheduled.limit(1).with_advisory_lock(select_limit: queue_select_limit) do |executions|
|
270
|
-
execution = executions.first
|
271
|
-
if execution&.executable?
|
272
|
-
yield(execution) if block_given?
|
273
|
-
result = execution.perform(lock_id: lock_id)
|
274
|
-
else
|
275
|
-
execution = nil
|
276
|
-
yield(nil) if block_given?
|
277
|
-
end
|
278
|
-
end
|
279
|
-
|
280
|
-
execution&.run_callbacks(:perform_unlocked)
|
281
|
-
result
|
282
|
-
end
|
283
|
-
|
284
|
-
# Fetches the scheduled execution time of the next eligible Execution(s).
|
285
|
-
# @param after [DateTime]
|
286
|
-
# @param limit [Integer]
|
287
|
-
# @param now_limit [Integer, nil]
|
288
|
-
# @return [Array<DateTime>]
|
289
|
-
def self.next_scheduled_at(after: nil, limit: 100, now_limit: nil)
|
290
|
-
query = advisory_unlocked.unfinished.schedule_ordered
|
291
|
-
|
292
|
-
after ||= Time.current
|
293
|
-
after_bind = bind_value('scheduled_at', after, ActiveRecord::Type::DateTime)
|
294
|
-
after_query = query.where(arel_table['scheduled_at'].gt(after_bind)).or query.where(scheduled_at: nil).where(arel_table['created_at'].gt(after_bind))
|
295
|
-
after_at = after_query.limit(limit).pluck(:scheduled_at, :created_at).map { |timestamps| timestamps.compact.first }
|
296
|
-
|
297
|
-
if now_limit&.positive?
|
298
|
-
now_query = query.where(arel_table['scheduled_at'].lt(bind_value('scheduled_at', Time.current, ActiveRecord::Type::DateTime))).or query.where(scheduled_at: nil)
|
299
|
-
now_at = now_query.limit(now_limit).pluck(:scheduled_at, :created_at).map { |timestamps| timestamps.compact.first }
|
300
|
-
end
|
301
|
-
|
302
|
-
Array(now_at) + after_at
|
303
|
-
end
|
304
|
-
|
305
|
-
# Places an ActiveJob job on a queue by creating a new {Execution} record.
|
306
|
-
# @param active_job [ActiveJob::Base]
|
307
|
-
# The job to enqueue.
|
308
|
-
# @param scheduled_at [Float]
|
309
|
-
# Epoch timestamp when the job should be executed, if blank will delegate to the ActiveJob instance
|
310
|
-
# @param create_with_advisory_lock [Boolean]
|
311
|
-
# Whether to establish a lock on the {Execution} record after it is created.
|
312
|
-
# @return [Execution]
|
313
|
-
# The new {Execution} instance representing the queued ActiveJob job.
|
314
|
-
def self.enqueue(active_job, scheduled_at: nil, create_with_advisory_lock: false)
|
315
|
-
ActiveSupport::Notifications.instrument("enqueue_job.good_job", { active_job: active_job, scheduled_at: scheduled_at, create_with_advisory_lock: create_with_advisory_lock }) do |instrument_payload|
|
316
|
-
current_execution = CurrentThread.execution
|
317
|
-
|
318
|
-
retried = current_execution && current_execution.active_job_id == active_job.job_id
|
319
|
-
if retried
|
320
|
-
if current_execution.discrete?
|
321
|
-
execution = current_execution
|
322
|
-
execution.assign_attributes(enqueue_args(active_job, { scheduled_at: scheduled_at }))
|
323
|
-
execution.scheduled_at ||= Time.current
|
324
|
-
# TODO: these values ideally shouldn't be persisted until the current_execution is finished
|
325
|
-
# which will require handling `retry_job` being called from outside the execution context.
|
326
|
-
execution.performed_at = nil
|
327
|
-
execution.finished_at = nil
|
328
|
-
else
|
329
|
-
execution = build_for_enqueue(active_job, { scheduled_at: scheduled_at })
|
330
|
-
end
|
331
|
-
else
|
332
|
-
execution = build_for_enqueue(active_job, { scheduled_at: scheduled_at })
|
333
|
-
execution.make_discrete if discrete_support?
|
334
|
-
end
|
335
|
-
|
336
|
-
if create_with_advisory_lock
|
337
|
-
if execution.persisted?
|
338
|
-
execution.advisory_lock
|
339
|
-
else
|
340
|
-
execution.create_with_advisory_lock = true
|
341
|
-
end
|
342
|
-
end
|
343
|
-
|
344
|
-
instrument_payload[:execution] = execution
|
345
|
-
execution.save!
|
346
|
-
|
347
|
-
if retried
|
348
|
-
CurrentThread.execution_retried = execution
|
349
|
-
CurrentThread.execution.retried_good_job_id = execution.id unless current_execution.discrete?
|
350
|
-
else
|
351
|
-
CurrentThread.execution_retried = nil
|
352
|
-
end
|
353
|
-
|
354
|
-
active_job.provider_job_id = execution.id
|
355
|
-
execution
|
356
|
-
end
|
357
|
-
end
|
358
|
-
|
359
|
-
def self.format_error(error)
|
360
|
-
raise ArgumentError unless error.is_a?(Exception)
|
361
|
-
|
362
|
-
[error.class.to_s, ERROR_MESSAGE_SEPARATOR, error.message].join
|
363
|
-
end
|
364
|
-
|
365
|
-
# Execute the ActiveJob job this {Execution} represents.
|
366
|
-
# @return [ExecutionResult]
|
367
|
-
# An array of the return value of the job's +#perform+ method and the
|
368
|
-
# exception raised by the job, if any. If the job completed successfully,
|
369
|
-
# the second array entry (the exception) will be +nil+ and vice versa.
|
370
|
-
def perform(lock_id:)
|
371
|
-
run_callbacks(:perform) do
|
372
|
-
raise PreviouslyPerformedError, 'Cannot perform a job that has already been performed' if finished_at
|
373
|
-
|
374
|
-
job_performed_at = Time.current
|
375
|
-
discrete_execution = nil
|
376
|
-
result = GoodJob::CurrentThread.within do |current_thread|
|
377
|
-
current_thread.reset
|
378
|
-
current_thread.execution = self
|
379
|
-
|
380
|
-
existing_performed_at = performed_at
|
381
|
-
if existing_performed_at
|
382
|
-
current_thread.execution_interrupted = existing_performed_at
|
383
|
-
|
384
|
-
if discrete?
|
385
|
-
interrupt_error_string = self.class.format_error(GoodJob::InterruptError.new("Interrupted after starting perform at '#{existing_performed_at}'"))
|
386
|
-
self.error = interrupt_error_string
|
387
|
-
self.error_event = ERROR_EVENT_INTERRUPTED if self.class.error_event_migrated?
|
388
|
-
|
389
|
-
discrete_execution_attrs = {
|
390
|
-
error: interrupt_error_string,
|
391
|
-
finished_at: job_performed_at,
|
392
|
-
}
|
393
|
-
discrete_execution_attrs[:error_event] = GoodJob::ErrorEvents::ERROR_EVENT_ENUMS[GoodJob::ErrorEvents::ERROR_EVENT_INTERRUPTED] if self.class.error_event_migrated?
|
394
|
-
discrete_executions.where(finished_at: nil).where.not(performed_at: nil).update_all(discrete_execution_attrs) # rubocop:disable Rails/SkipsModelValidations
|
395
|
-
end
|
396
|
-
end
|
397
|
-
|
398
|
-
if discrete?
|
399
|
-
transaction do
|
400
|
-
discrete_execution_attrs = {
|
401
|
-
job_class: job_class,
|
402
|
-
queue_name: queue_name,
|
403
|
-
serialized_params: serialized_params,
|
404
|
-
scheduled_at: (scheduled_at || created_at),
|
405
|
-
created_at: job_performed_at,
|
406
|
-
}
|
407
|
-
discrete_execution_attrs[:process_id] = lock_id if GoodJob::DiscreteExecution.columns_hash.key?("process_id")
|
408
|
-
|
409
|
-
execution_attrs = {
|
410
|
-
performed_at: job_performed_at,
|
411
|
-
executions_count: ((executions_count || 0) + 1),
|
412
|
-
}
|
413
|
-
if GoodJob::Execution.columns_hash.key?("locked_by_id")
|
414
|
-
execution_attrs[:locked_by_id] = lock_id
|
415
|
-
execution_attrs[:locked_at] = Time.current
|
416
|
-
end
|
417
|
-
|
418
|
-
discrete_execution = discrete_executions.create!(discrete_execution_attrs)
|
419
|
-
update!(execution_attrs)
|
420
|
-
end
|
421
|
-
else
|
422
|
-
execution_attrs = {
|
423
|
-
performed_at: job_performed_at,
|
424
|
-
}
|
425
|
-
if GoodJob::Execution.columns_hash.key?("locked_by_id")
|
426
|
-
execution_attrs[:locked_by_id] = lock_id
|
427
|
-
execution_attrs[:locked_at] = Time.current
|
428
|
-
end
|
429
|
-
|
430
|
-
update!(execution_attrs)
|
431
|
-
end
|
432
|
-
|
433
|
-
ActiveSupport::Notifications.instrument("perform_job.good_job", { execution: self, process_id: current_thread.process_id, thread_name: current_thread.thread_name }) do |instrument_payload|
|
434
|
-
value = ActiveJob::Base.execute(active_job_data)
|
435
|
-
|
436
|
-
if value.is_a?(Exception)
|
437
|
-
handled_error = value
|
438
|
-
value = nil
|
439
|
-
end
|
440
|
-
handled_error ||= current_thread.error_on_retry || current_thread.error_on_discard
|
441
|
-
|
442
|
-
error_event = if handled_error == current_thread.error_on_discard
|
443
|
-
ERROR_EVENT_DISCARDED
|
444
|
-
elsif handled_error == current_thread.error_on_retry
|
445
|
-
ERROR_EVENT_RETRIED
|
446
|
-
elsif handled_error == current_thread.error_on_retry_stopped
|
447
|
-
ERROR_EVENT_RETRY_STOPPED
|
448
|
-
elsif handled_error
|
449
|
-
ERROR_EVENT_HANDLED
|
450
|
-
end
|
451
|
-
|
452
|
-
instrument_payload.merge!(
|
453
|
-
value: value,
|
454
|
-
handled_error: handled_error,
|
455
|
-
retried: current_thread.execution_retried.present?,
|
456
|
-
error_event: error_event
|
457
|
-
)
|
458
|
-
ExecutionResult.new(value: value, handled_error: handled_error, error_event: error_event, retried: current_thread.execution_retried)
|
459
|
-
rescue StandardError => e
|
460
|
-
error_event = if e.is_a?(GoodJob::InterruptError)
|
461
|
-
ERROR_EVENT_INTERRUPTED
|
462
|
-
elsif e == current_thread.error_on_retry_stopped
|
463
|
-
ERROR_EVENT_RETRY_STOPPED
|
464
|
-
else
|
465
|
-
ERROR_EVENT_UNHANDLED
|
466
|
-
end
|
467
|
-
|
468
|
-
instrument_payload[:unhandled_error] = e
|
469
|
-
ExecutionResult.new(value: nil, unhandled_error: e, error_event: error_event)
|
470
|
-
end
|
471
|
-
end
|
472
|
-
|
473
|
-
job_attributes = if self.class.columns_hash.key?("locked_by_id")
|
474
|
-
{ locked_by_id: nil, locked_at: nil }
|
475
|
-
else
|
476
|
-
{}
|
477
|
-
end
|
478
|
-
|
479
|
-
job_error = result.handled_error || result.unhandled_error
|
480
|
-
if job_error
|
481
|
-
error_string = self.class.format_error(job_error)
|
482
|
-
|
483
|
-
job_attributes[:error] = error_string
|
484
|
-
job_attributes[:error_event] = result.error_event if self.class.error_event_migrated?
|
485
|
-
if discrete_execution
|
486
|
-
discrete_execution.error = error_string
|
487
|
-
discrete_execution.error_event = result.error_event
|
488
|
-
discrete_execution.error_backtrace = job_error.backtrace if discrete_execution.class.backtrace_migrated?
|
489
|
-
end
|
490
|
-
else
|
491
|
-
job_attributes[:error] = nil
|
492
|
-
job_attributes[:error_event] = nil
|
493
|
-
end
|
494
|
-
job_attributes.delete(:error_event) unless self.class.error_event_migrated?
|
495
|
-
|
496
|
-
job_finished_at = Time.current
|
497
|
-
job_attributes[:finished_at] = job_finished_at
|
498
|
-
discrete_execution.finished_at = job_finished_at if discrete_execution
|
499
|
-
|
500
|
-
retry_unhandled_error = result.unhandled_error && GoodJob.retry_on_unhandled_error
|
501
|
-
reenqueued = result.retried? || retried_good_job_id.present? || retry_unhandled_error
|
502
|
-
if reenqueued
|
503
|
-
if discrete_execution
|
504
|
-
job_attributes[:performed_at] = nil
|
505
|
-
job_attributes[:finished_at] = nil
|
506
|
-
else
|
507
|
-
job_attributes[:retried_good_job_id] = retried_good_job_id
|
508
|
-
job_attributes[:finished_at] = nil if retry_unhandled_error
|
509
|
-
end
|
510
|
-
end
|
511
|
-
|
512
|
-
preserve_unhandled = (result.unhandled_error && (GoodJob.retry_on_unhandled_error || GoodJob.preserve_job_records == :on_unhandled_error))
|
513
|
-
if GoodJob.preserve_job_records == true || reenqueued || preserve_unhandled || cron_key.present?
|
514
|
-
if discrete_execution
|
515
|
-
transaction do
|
516
|
-
discrete_execution.save!
|
517
|
-
update!(job_attributes)
|
518
|
-
end
|
519
|
-
else
|
520
|
-
update!(job_attributes)
|
521
|
-
end
|
522
|
-
else
|
523
|
-
destroy_job
|
524
|
-
end
|
525
|
-
|
526
|
-
result
|
527
|
-
end
|
528
|
-
end
|
529
|
-
|
530
|
-
# Tests whether this job is safe to be executed by this thread.
|
531
|
-
# @return [Boolean]
|
532
|
-
def executable?
|
533
|
-
reload.finished_at.blank?
|
534
|
-
rescue ActiveRecord::RecordNotFound
|
535
|
-
false
|
536
|
-
end
|
537
|
-
|
538
|
-
def make_discrete
|
539
|
-
self.is_discrete = true
|
540
|
-
self.id = active_job_id
|
541
|
-
self.job_class = serialized_params['job_class']
|
542
|
-
self.executions_count ||= 0
|
543
|
-
|
544
|
-
current_time = Time.current
|
545
|
-
self.created_at ||= current_time
|
546
|
-
self.scheduled_at ||= current_time
|
547
|
-
end
|
548
|
-
|
549
|
-
# Return formatted serialized_params for display in the dashboard
|
550
|
-
# @return [Hash]
|
551
|
-
def display_serialized_params
|
552
|
-
serialized_params.merge({
|
553
|
-
_good_job: attributes.except('serialized_params', 'locktype', 'owns_advisory_lock'),
|
554
|
-
})
|
555
|
-
end
|
556
|
-
|
557
|
-
def running?
|
558
|
-
if has_attribute?(:locktype)
|
559
|
-
self['locktype'].present?
|
560
|
-
else
|
561
|
-
advisory_locked?
|
562
|
-
end
|
563
|
-
end
|
564
|
-
|
565
|
-
def number
|
566
|
-
serialized_params.fetch('executions', 0) + 1
|
567
|
-
end
|
568
|
-
|
569
|
-
# Time between when this job was expected to run and when it started running
|
570
|
-
def queue_latency
|
571
|
-
now = Time.zone.now
|
572
|
-
expected_start = scheduled_at || created_at
|
573
|
-
actual_start = performed_at || finished_at || now
|
574
|
-
|
575
|
-
actual_start - expected_start unless expected_start >= now
|
576
|
-
end
|
577
|
-
|
578
|
-
# Time between when this job started and finished
|
579
|
-
def runtime_latency
|
580
|
-
(finished_at || Time.zone.now) - performed_at if performed_at
|
581
|
-
end
|
582
|
-
|
583
|
-
# Destroys this execution and all executions within the same job
|
584
|
-
def destroy_job
|
585
|
-
@_destroy_job = true
|
586
|
-
destroy!
|
587
|
-
ensure
|
588
|
-
@_destroy_job = false
|
589
|
-
end
|
590
|
-
|
591
|
-
def job_state
|
592
|
-
state = { queue_name: queue_name }
|
593
|
-
state[:scheduled_at] = scheduled_at if scheduled_at
|
594
|
-
state
|
595
|
-
end
|
596
|
-
|
597
|
-
private
|
598
|
-
|
599
|
-
def reset_batch_values(&block)
|
600
|
-
GoodJob::Batch.within_thread(batch_id: nil, batch_callback_id: nil, &block)
|
601
|
-
end
|
602
|
-
|
603
|
-
def continue_discard_or_finish_batch
|
604
|
-
batch._continue_discard_or_finish(self) if GoodJob::BatchRecord.migrated? && batch.present?
|
605
|
-
end
|
606
11
|
end
|
607
12
|
end
|
data/app/models/good_job/job.rb
CHANGED
@@ -201,7 +201,7 @@ module GoodJob
|
|
201
201
|
active_job.executions = (active_job.executions || 0) + 1
|
202
202
|
|
203
203
|
begin
|
204
|
-
error_class, error_message = execution.error.split(
|
204
|
+
error_class, error_message = execution.error.split(ERROR_MESSAGE_SEPARATOR).map(&:strip)
|
205
205
|
error = error_class.constantize.new(error_message)
|
206
206
|
rescue StandardError
|
207
207
|
error = StandardError.new(execution.error)
|
@@ -0,0 +1,50 @@
|
|
1
|
+
<div class="border-bottom">
|
2
|
+
<h2 class="pt-3 pb-2"><%= t ".title" %></h2>
|
3
|
+
</div>
|
4
|
+
|
5
|
+
<% if @needs_upgrade %>
|
6
|
+
<div class="alert alert-warning">
|
7
|
+
<%= t "shared.needs_migration" %>
|
8
|
+
</div>
|
9
|
+
<% else %>
|
10
|
+
|
11
|
+
<div class="my-3 card">
|
12
|
+
<div class="list-group list-group-flush text-nowrap" role="table">
|
13
|
+
<header class="list-group-item bg-body-tertiary">
|
14
|
+
<div class="row small text-muted text-uppercase align-items-center">
|
15
|
+
<div class="col-12 col-lg-4"><%= t ".job_class" %></div>
|
16
|
+
<div class="col-lg-2 d-none d-lg-block"><%= t ".executions" %></div>
|
17
|
+
|
18
|
+
<div class="col-lg-2 d-none d-lg-block"><%= t ".average_duration" %></div>
|
19
|
+
<div class="col-lg-2 d-none d-lg-block"><%= t ".minimum_duration" %></div>
|
20
|
+
<div class="col-lg-2 d-none d-lg-block"><%= t ".maximum_duration" %></div>
|
21
|
+
</div>
|
22
|
+
</header>
|
23
|
+
|
24
|
+
<% @performances.each do |performance| %>
|
25
|
+
<div role="row" class="list-group-item py-3">
|
26
|
+
<div class="row align-items-center">
|
27
|
+
<div class="col-12 col-lg-4"><%= performance.job_class %></div>
|
28
|
+
<div class="col-6 col-lg-2 text-wrap">
|
29
|
+
<div class="d-lg-none small text-muted mt-1"><%= t ".executions" %></div>
|
30
|
+
<%= performance.executions_count %>
|
31
|
+
</div>
|
32
|
+
|
33
|
+
<div class="col-6 col-lg-2 text-wrap">
|
34
|
+
<div class="d-lg-none small text-muted mt-1"><%= t ".average_duration" %></div>
|
35
|
+
<%= format_duration performance.avg_duration %>
|
36
|
+
</div>
|
37
|
+
<div class="col-6 col-lg-2 text-wrap">
|
38
|
+
<div class="d-lg-none small text-muted mt-1"><%= t ".minimum_duration" %></div>
|
39
|
+
<%= format_duration performance.min_duration %>
|
40
|
+
</div>
|
41
|
+
<div class="col-6 col-lg-2 text-wrap">
|
42
|
+
<div class="d-lg-none small text-muted mt-1"><%= t ".maximum_duration" %></div>
|
43
|
+
<%= format_duration performance.max_duration %>
|
44
|
+
</div>
|
45
|
+
</div>
|
46
|
+
</div>
|
47
|
+
<% end %>
|
48
|
+
</div>
|
49
|
+
</div>
|
50
|
+
<% end %>
|
@@ -39,6 +39,11 @@
|
|
39
39
|
<span data-async-values-target="value" data-async-values-key="processes_count" data-async-values-zero-class="bg-danger" class="badge bg-secondary rounded-pill d-none"></span>
|
40
40
|
<% end %>
|
41
41
|
</li>
|
42
|
+
<li class="nav-item">
|
43
|
+
<%= link_to performance_path, class: ["nav-link", ("active" if controller_name == 'performances')] do %>
|
44
|
+
<%= t(".performances") %>
|
45
|
+
<% end %>
|
46
|
+
</li>
|
42
47
|
</ul>
|
43
48
|
|
44
49
|
<ul class="navbar-nav">
|