Skip to content

Commit 4242aaf

Browse files
committed
Expanded batch readme
1 parent fe6c5d5 commit 4242aaf

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
@@ -497,12 +497,62 @@ class ApplicationMailer < ActionMailer::Base
497497

498498
## Batch jobs
499499

500+
SolidQueue offers support for batching jobs. This allows you to track progress of a set of jobs,
501+
and optionally trigger callbacks based on their status. It supports the following:
502+
503+
- Relating jobs to a batch, to track their status
504+
- Three available callbacks to fire:
505+
- `on_finish`: Fired when all jobs have finished, including retries. Fires even when some jobs have failed.
506+
- `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`
507+
- `on_failure`: Fired the _first_ time a job fails, after all retries are exhausted.
508+
- If a job is part of a batch, it can enqueue more jobs for that batch using `batch#enqueue`
509+
- Batches can be nested within other batches, creating a hierarchy. Outer batches will not finish until all nested batches have finished.
510+
500511
```rb
501-
SolidQueue::JobBatch.enqueue(on_finish: BatchCompletionJob) do
502-
5.times.map { |i| SleepyJob.perform_later(i) }
512+
class SleepyJob < ApplicationJob
513+
def perform(seconds_to_sleep)
514+
Rails.logger.info "Feeling #{seconds_to_sleep} seconds sleepy..."
515+
sleep seconds_to_sleep
516+
end
517+
end
518+
519+
class MultiStepJob < ApplicationJob
520+
def perform
521+
batch.enqueue do
522+
SleepyJob.perform_later(5)
523+
# Because of this nested batch, the top-level batch won't finish until the inner,
524+
# 10 second job finishes
525+
# Both jobs will still run simultaneously
526+
SolidQueue::JobBatch.enqueue do
527+
SleepyJob.perform_later(10)
528+
end
529+
end
530+
end
531+
end
532+
533+
class BatchFinishJob < ApplicationJob
534+
def perform(batch) # batch is always the default first argument
535+
Rails.logger.info "Good job finishing all jobs"
536+
end
537+
end
538+
539+
class BatchSuccessJob < ApplicationJob
540+
def perform(batch) # batch is always the default first argument
541+
Rails.logger.info "Good job finishing all jobs, and all of them worked!"
542+
end
543+
end
544+
545+
class BatchFailureJob < ApplicationJob
546+
def perform(batch) # batch is always the default first argument
547+
Rails.logger.info "At least one job failed, sorry!"
548+
end
503549
end
504550
505-
SolidQueue::JobBatch.enqueue(on_success: BatchCompletionJob) do
551+
SolidQueue::JobBatch.enqueue(
552+
on_finish: BatchFinishJob,
553+
on_success: BatchSuccessJob,
554+
on_failure: BatchFailureJob
555+
) do
506556
5.times.map { |i| SleepyJob.perform_later(i) }
507557
end
508558
```

0 commit comments

Comments
 (0)