Skip to content

Commit 296ba6f

Browse files
committed
Expanded batch readme
1 parent f2a1e5b commit 296ba6f

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

547547
## Batch jobs
548548

549+
SolidQueue offers support for batching jobs. This allows you to track progress of a set of jobs,
550+
and optionally trigger callbacks based on their status. It supports the following:
551+
552+
- Relating jobs to a batch, to track their status
553+
- Three available callbacks to fire:
554+
- `on_finish`: Fired when all jobs have finished, including retries. Fires even when some jobs have failed.
555+
- `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`
556+
- `on_failure`: Fired the _first_ time a job fails, after all retries are exhausted.
557+
- If a job is part of a batch, it can enqueue more jobs for that batch using `batch#enqueue`
558+
- Batches can be nested within other batches, creating a hierarchy. Outer batches will not finish until all nested batches have finished.
559+
549560
```rb
550-
SolidQueue::JobBatch.enqueue(on_finish: BatchCompletionJob) do
551-
5.times.map { |i| SleepyJob.perform_later(i) }
561+
class SleepyJob < ApplicationJob
562+
def perform(seconds_to_sleep)
563+
Rails.logger.info "Feeling #{seconds_to_sleep} seconds sleepy..."
564+
sleep seconds_to_sleep
565+
end
566+
end
567+
568+
class MultiStepJob < ApplicationJob
569+
def perform
570+
batch.enqueue do
571+
SleepyJob.perform_later(5)
572+
# Because of this nested batch, the top-level batch won't finish until the inner,
573+
# 10 second job finishes
574+
# Both jobs will still run simultaneously
575+
SolidQueue::JobBatch.enqueue do
576+
SleepyJob.perform_later(10)
577+
end
578+
end
579+
end
580+
end
581+
582+
class BatchFinishJob < ApplicationJob
583+
def perform(batch) # batch is always the default first argument
584+
Rails.logger.info "Good job finishing all jobs"
585+
end
586+
end
587+
588+
class BatchSuccessJob < ApplicationJob
589+
def perform(batch) # batch is always the default first argument
590+
Rails.logger.info "Good job finishing all jobs, and all of them worked!"
591+
end
592+
end
593+
594+
class BatchFailureJob < ApplicationJob
595+
def perform(batch) # batch is always the default first argument
596+
Rails.logger.info "At least one job failed, sorry!"
597+
end
552598
end
553599
554-
SolidQueue::JobBatch.enqueue(on_success: BatchCompletionJob) do
600+
SolidQueue::JobBatch.enqueue(
601+
on_finish: BatchFinishJob,
602+
on_success: BatchSuccessJob,
603+
on_failure: BatchFailureJob
604+
) do
555605
5.times.map { |i| SleepyJob.perform_later(i) }
556606
end
557607
```

0 commit comments

Comments
 (0)