Skip to content

Commit 2270903

Browse files
committed
Expanded batch readme
1 parent 8cdc14f commit 2270903

File tree

1 file changed

+53
-3
lines changed

1 file changed

+53
-3
lines changed

README.md

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,12 +403,62 @@ class ApplicationMailer < ActionMailer::Base
403403

404404
## Batch jobs
405405

406+
SolidQueue offers support for batching jobs. This allows you to track progress of a set of jobs,
407+
and optionally trigger callbacks based on their status. It supports the following:
408+
409+
- Relating jobs to a batch, to track their status
410+
- Three available callbacks to fire:
411+
- `on_finish`: Fired when all jobs have finished, including retries. Fires even when some jobs have failed.
412+
- `on_success`: Fired when all jobs have succeeded, including retries. Will not fire if any jobs have failed, but will fire if jobs have been discarded using `discard_on`
413+
- `on_failure`: Fired the _first_ time a job fails, after all retries are exhausted.
414+
- If a job is part of a batch, it can enqueue more jobs for that batch using `batch#enqueue`
415+
- Batches can be nested within other batches, creating a hierarchy. Outer batches will not finish until all nested batches have finished.
416+
406417
```rb
407-
SolidQueue::JobBatch.enqueue(on_finish: BatchCompletionJob) do
408-
5.times.map { |i| SleepyJob.perform_later(i) }
418+
class SleepyJob < ApplicationJob
419+
def perform(seconds_to_sleep)
420+
Rails.logger.info "Feeling #{seconds_to_sleep} seconds sleepy..."
421+
sleep seconds_to_sleep
422+
end
423+
end
424+
425+
class MultiStepJob < ApplicationJob
426+
def perform
427+
batch.enqueue do
428+
SleepyJob.perform_later(5)
429+
# Because of this nested batch, the top-level batch won't finish until the inner,
430+
# 10 second job finishes
431+
# Both jobs will still run simultaneously
432+
SolidQueue::JobBatch.enqueue do
433+
SleepyJob.perform_later(10)
434+
end
435+
end
436+
end
437+
end
438+
439+
class BatchFinishJob < ApplicationJob
440+
def perform(batch) # batch is always the default first argument
441+
Rails.logger.info "Good job finishing all jobs"
442+
end
443+
end
444+
445+
class BatchSuccessJob < ApplicationJob
446+
def perform(batch) # batch is always the default first argument
447+
Rails.logger.info "Good job finishing all jobs, and all of them worked!"
448+
end
449+
end
450+
451+
class BatchFailureJob < ApplicationJob
452+
def perform(batch) # batch is always the default first argument
453+
Rails.logger.info "At least one job failed, sorry!"
454+
end
409455
end
410456
411-
SolidQueue::JobBatch.enqueue(on_success: BatchCompletionJob) do
457+
SolidQueue::JobBatch.enqueue(
458+
on_finish: BatchFinishJob,
459+
on_success: BatchSuccessJob,
460+
on_failure: BatchFailureJob
461+
) do
412462
5.times.map { |i| SleepyJob.perform_later(i) }
413463
end
414464
```

0 commit comments

Comments
 (0)