docs/guides/callbacks.md in wayfarer-0.4.6 vs docs/guides/callbacks.md in wayfarer-0.4.7

- old
+ new

@@ -1,145 +1,43 @@ # Callbacks -## Active Job callbacks +Wayfarer supports a number of callbacks in addition to +[ActiveJob's](https://edgeguides.rubyonrails.org/active_job_basics.html#callbacks). -Wayfarer naturally supports all of [Active Job's life cycle callbacks](https://edgeguides.rubyonrails.org/active_job_basics.html#callbacks). +## Available callbacks -## `before_fetch` +* `before_fetch` +* `around_fetch` +* `after_fetch` +* `before_action` +* `around_action` +* `after_action` +* `after_batch` -Runs before a job fetches a page, either by making an HTTP request, or by -navigating a browser to its task URL. - -```ruby -class DummyJob < Wayfarer::Base - before_fetch :do_something - - private - - def do_something - # before the task.url is fetched - end -end -``` - -## `before_action` - -Runs after a page was fetched, before an action method is called. - -```ruby -class DummyJob < Wayfarer::Base - before_action :do_something - - private - - def do_something - # page is available at this point - end -end -``` - ## `after_batch` -Runs once the last job in a batch performed: +You can register `after_batch` callbacks that run when there are no more tasks +to process in a batch. Wayfarer instruments job execution and in- or decrements +an integer counter in Redis on certain events. When the counter reaches zero, +the current job's `after_batch` callbacks run. -```ruby -class DummyJob < Wayfarer::Base - after_batch do - # All jobs in batch done - end -end -``` +## Conditional callbacks -Internally, a batch counter is in-/decremented on certain events. Once the -counter reaches zero, `after_batch` callbacks runs in declaration order. +You can make callbacks conditional with the `#!ruby :if` and `#!ruby :unless` +keywords, for example to run a callback for some route `action` only: -The counter is incremented when within the batch: - -* A job is enqueued. - -The counter is decremented when: - -* A job succeeds. -* A job errors due to an unhandled exception. -* A job is discarded due to an exception. -* A job errors and thereyby exhausts its maximum attempts. - -!!! attention "Batch callbacks can fail jobs" - - If the last job's `after_batch` callbacks raises an exception, this can lead - to the job getting retried. If the exception raised by the callback is - unhandled or discarded, the callback never fully runs. - -## Callback options - -### Definition styles - -Callbacks can be registered either by supplying a block or a symbol identifying -a callback instance method: - ```ruby -class DummyJob < Wayfarer::Base - before_action do - # ... - end +class DummyJob < ActiveJob::Base + include Wayfarer::Base - before_action :my_callback + route.host "example.com", to: :example + route.to :fallback - private - - def my_callback + before_action unless: -> { action == :fallback } do # ... end -end -``` -### Conditionals - -Callbacks can be registered conditionally with the `:if` and `:unless` keywords: - -```ruby -class DummyJob < Wayfarer::Base - before_fetch :my_callback, if: :my_condition - - private - - def my_callback - end - - def my_condition - end + # ... end ``` -Callbacks can be registered for certain action methods only with the `:only` and -`:except` keywords: - -```ruby -class DummyJob < Wayfarer::Base - before_fetch :do_something, only: :foo - - before_fetch except: [:foo, :qux] do - # runs only before bar - end - - def foo - end - - def bar - end -end - -``` - -### Early termination - -Callbacks that return `false` halt the callback chain: - -```ruby -class DummyJob < Wayfarer::Base - before_action { false } - - before_action do - # never runs - end -end -``` +You can also pass a symbol instead of a block to call an instance method.