Skip to content

Commit

Permalink
Fix documentation for 2nd Batch callback parameter: consistently call…
Browse files Browse the repository at this point in the history
… it `context` (#1476)

* Fix batch `options` => `params` in README.md

* Use `context` to describe second batch callback job argument

* Fix code example to use `context`

* Fix one more usage for `context`

---------

Co-authored-by: Ben Sheldon [he/him] <bensheldon@gmail.com>
  • Loading branch information
martijnversluis and bensheldon committed Sep 12, 2024
1 parent 9a78188 commit f294e30
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -681,15 +681,15 @@ Batches track a set of jobs, and enqueue an optional callback job when all of th
OtherJob.perform_later
end
# When these jobs have finished, it will enqueue your `MyBatchCallbackJob.perform_later(batch, options)`
# When these jobs have finished, it will enqueue your `MyBatchCallbackJob.perform_later(batch, context)`
class MyBatchCallbackJob < ApplicationJob
# Callback jobs must accept a `batch` and `options` argument
def perform(batch, params)
# Callback jobs must accept a `batch` and `context` argument
def perform(batch, context)
# The batch object will contain the Batch's properties, which are mutable
batch.properties[:user] # => <User id: 1, ...>
# Params is a hash containing additional context (more may be added in the future)
params[:event] # => :finish, :success, :discard
# Context is a hash containing additional context (more may be added in the future)
context[:event] # => :finish, :success, :discard
end
end
```
Expand Down Expand Up @@ -757,19 +757,19 @@ Batch callbacks are Active Job jobs that are enqueued at certain events during t
- `:success` - Enqueued only when all jobs in the batch have finished and succeeded.
- `:discard` - Enqueued immediately the first time a job in the batch is discarded.
Callback jobs must accept a `batch` and `params` argument in their `perform` method:
Callback jobs must accept a `batch` and `context` argument in their `perform` method:
```ruby
class MyBatchCallbackJob < ApplicationJob
def perform(batch, params)
def perform(batch, context)
# The batch object will contain the Batch's properties
batch.properties[:user] # => <User id: 1, ...>
# Batches are mutable
batch.properties[:user] = User.find(2)
batch.save
# Params is a hash containing additional context (more may be added in the future)
params[:event] # => :finish, :success, :discard
# Context is a hash containing additional context (more may be added in the future)
context[:event] # => :finish, :success, :discard
end
end
```
Expand Down Expand Up @@ -811,7 +811,7 @@ class WorkJob < ApplicationJob
end
class BatchJob < ApplicationJob
def perform(batch, options)
def perform(batch, context)
if batch.properties[:stage].nil?
batch.enqueue(stage: 1) do
WorkJob.perform_later('a')
Expand Down

0 comments on commit f294e30

Please sign in to comment.