Skip to content

Handlers

Handlers are like jobs but they don't inherit from ActiveJob::Base which is why they can't affect the message queue directly themselves. Instead, jobs and handlers can route tasks to other handlers. Handlers themselves have routes, but they can be bypassed.

Handler capabilities

Like jobs, handlers support:

  • URL routing
  • staging tasks with stage(*urls)
  • jobs can access the user_agent that retrieved the page
  • ad-hoc HTTP requests with fetch(url)
  • callbacks, but only a subset of job callbacks
  • Content-Type filtering
class ExampleHandler
  include Wayfarer::Handler

  route.to: :index

  def index
    task # => #<Wayfarer::Task>
    page # => #<Wayfarer::Page>
    user_agent # => Browser or HTTP client
  end
end

class DummyJob < ActiveJob::Base
  include Wayfarer::Base

  route.host "example.com", to: ExampleHandler
end

You can also bypass a handler's router and route directly to an instance method:

class DummyJob < ActiveJob::Base
  include Wayfarer::Base

  route.host "example.com", to: [ExampleHandler, :index]
end

class ExampleHandler
  include Wayfarer::Handler

  def index
    task # => #<Wayfarer::Task>
    page # => #<Wayfarer::Page>
    user_agent # => Browser or HTTP client
  end
end

!!! before_action callbacks