acidic_job 1.0.0.pre21 → 1.0.0.pre22
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +32 -3
- data/lib/acidic_job/awaiting.rb +8 -1
- data/lib/acidic_job/version.rb +1 -1
- data/lib/acidic_job.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 892c6a2598d63440a7e749484e790b3f47380954b615bd7b2c4fc4c8de541759
|
4
|
+
data.tar.gz: bf7383000fa8e0fb1c545e5ccb014c7123884eb14da77178dd088bb9b12e73e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 87610f9f920ddb6e34c727a6b4c9e8d98d9ec556bff1dea221e3f588694b4e5f77b482985ba906cc62c5a3593e398e906e8dd6aa815e9c6d3031e410b71abfeb
|
7
|
+
data.tar.gz: 7ca46ec550598f545d3baaafccf09cf082b6eaad01a1d6bb4b8f5a7298d9023b0b33c824183e3c5b30b3a1fbb2a9200286761b4e671adf39569a62b6ac392740
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -280,7 +280,7 @@ class RideCreateJob < ActiveJob::Base
|
|
280
280
|
@params = ride_params
|
281
281
|
|
282
282
|
with_acidity providing: { ride: nil } do
|
283
|
-
step :create_ride_and_audit_record, awaits: [SomeJob]
|
283
|
+
step :create_ride_and_audit_record, awaits: awaits: [SomeJob.with('argument_1', keyword: 'value')]
|
284
284
|
step :create_stripe_charge, args: [1, 2, 3], kwargs: { some: 'thing' }
|
285
285
|
step :send_receipt
|
286
286
|
end
|
@@ -288,6 +288,37 @@ class RideCreateJob < ActiveJob::Base
|
|
288
288
|
end
|
289
289
|
```
|
290
290
|
|
291
|
+
You can also await a batch of jobs by simply passing multiple jobs to the `awaits` array (e.g. `awaits: [SomeJob, AnotherJob.with('argument_1', keyword: 'value')]`). Your top level workflow job will only continue to the next step once all of the jobs in your `awaits` array have successfully finished.
|
292
|
+
|
293
|
+
In some cases, you may need to _dynamically_ determine the collection of jobs that the step should wait for; in these cases, you can pass the name of a method to the `awaits` option:
|
294
|
+
|
295
|
+
```ruby
|
296
|
+
class RideCreateJob < ActiveJob::Base
|
297
|
+
include AcidicJob
|
298
|
+
set_callback :finish, :after, :delete_run_record
|
299
|
+
|
300
|
+
def perform(user_id, ride_params)
|
301
|
+
@user = User.find(user_id)
|
302
|
+
@params = ride_params
|
303
|
+
|
304
|
+
with_acidity providing: { ride: nil } do
|
305
|
+
step :create_ride_and_audit_record, awaits: :dynamic_awaits
|
306
|
+
step :create_stripe_charge, args: [1, 2, 3], kwargs: { some: 'thing' }
|
307
|
+
step :send_receipt
|
308
|
+
end
|
309
|
+
end
|
310
|
+
|
311
|
+
def dynamic_awaits
|
312
|
+
if @params["key"].present?
|
313
|
+
[SomeJob.with('argument_1', keyword: 'value')]
|
314
|
+
else
|
315
|
+
[AnotherJob]
|
316
|
+
end
|
317
|
+
end
|
318
|
+
end
|
319
|
+
```
|
320
|
+
|
321
|
+
|
291
322
|
### Run Finished Callbacks
|
292
323
|
|
293
324
|
When working with workflow jobs that make use of the `awaits` feature for a step, it is important to remember that the `after_perform` callback will be called _as soon as the first `awaits` step has enqueued job_, and **not** when the entire job run has finished. `acidic_job` allows the `perform` method to finish so that the queue for the workflow job is cleared to pick up new work while the `awaits` jobs are running. `acidic_job` will automatically re-enqueue the workflow job and progress to the next step when all of the `awaits` jobs have successfully finished. However, this means that `after_perform` **is not necessarily** the same as `after_finish`. In order to provide the opportunity for you to execute callback logic _if and only if_ a job run has finished, we provide callback hooks for the `finish` event.
|
@@ -318,8 +349,6 @@ class RideCreateJob < ActiveJob::Base
|
|
318
349
|
end
|
319
350
|
```
|
320
351
|
|
321
|
-
You can also await a batch of jobs by simply passing multiple jobs to the `awaits` array (e.g. `awaits: [SomeJob, AnotherJob.with('argument_1', keyword: 'value')]`). Your top level workflow job will only continue to the next step once all of the jobs in your `awaits` array have successfully finished.
|
322
|
-
|
323
352
|
## Testing
|
324
353
|
|
325
354
|
When testing acidic jobs, you are likely to run into `ActiveRecord::TransactionIsolationError`s:
|
data/lib/acidic_job/awaiting.rb
CHANGED
@@ -8,10 +8,17 @@ module AcidicJob
|
|
8
8
|
|
9
9
|
private
|
10
10
|
|
11
|
-
def enqueue_step_parallel_jobs(
|
11
|
+
def enqueue_step_parallel_jobs(jobs_or_jobs_getter, run, step_result)
|
12
12
|
# `batch` is available from Sidekiq::Pro
|
13
13
|
raise SidekiqBatchRequired unless defined?(Sidekiq::Batch)
|
14
14
|
|
15
|
+
jobs = case jobs_or_jobs_getter
|
16
|
+
when Array
|
17
|
+
jobs_or_jobs_getter
|
18
|
+
when Symbol, String
|
19
|
+
method(jobs_or_jobs_getter).call
|
20
|
+
end
|
21
|
+
|
15
22
|
step_batch = Sidekiq::Batch.new
|
16
23
|
# step_batch.description = "AcidicJob::Workflow Step: #{step}"
|
17
24
|
step_batch.on(
|
data/lib/acidic_job/version.rb
CHANGED
data/lib/acidic_job.rb
CHANGED
@@ -148,7 +148,7 @@ module AcidicJob
|
|
148
148
|
break
|
149
149
|
elsif current_step.nil?
|
150
150
|
raise UnknownRecoveryPoint, "Defined workflow does not reference this step: #{recovery_point}"
|
151
|
-
elsif (jobs = current_step.fetch("awaits", [])).
|
151
|
+
elsif !(jobs = current_step.fetch("awaits", []) || []).empty?
|
152
152
|
step = Step.new(current_step, run, self)
|
153
153
|
# Only execute the current step, without yet progressing the recovery_point to the next step.
|
154
154
|
# This ensures that any failures in parallel jobs will have this step retried in the main workflow
|