Class: Hooksmith::Jobs::DispatcherJob

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
lib/hooksmith/jobs/dispatcher_job.rb

Overview

ActiveJob for asynchronous webhook processing.

This job wraps the Dispatcher to process webhooks in the background, allowing your webhook endpoint to respond quickly while processing happens asynchronously.

Examples:

Basic usage in a controller

class WebhooksController < ApplicationController
  def stripe
    Hooksmith::Jobs::DispatcherJob.perform_later(
      provider: 'stripe',
      event: params[:type],
      payload: params.to_unsafe_h
    )
    head :ok
  end
end

With custom queue

Hooksmith::Jobs::DispatcherJob.set(queue: :webhooks).perform_later(...)

With retry configuration (in your application)

# config/initializers/hooksmith.rb
Hooksmith::Jobs::DispatcherJob.retry_on StandardError, wait: :polynomially_longer, attempts: 5

Instance Method Summary collapse

Instance Method Details

#perform(provider:, event:, payload:, **options) ⇒ Object

Performs the webhook dispatch asynchronously.

Parameters:

  • provider (String, Symbol)

    the webhook provider name

  • event (String, Symbol)

    the event type

  • payload (Hash)

    the webhook payload

  • options (Hash)

    additional options

Options Hash (**options):

  • :skip_idempotency_check (Boolean) — default: false

    skip duplicate checking

Returns:

  • (Object)

    the result from the processor



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hooksmith/jobs/dispatcher_job.rb', line 41

def perform(provider:, event:, payload:, **options)
  provider = provider.to_s
  event = event.to_s

  if check_idempotency?(options)
    key = Hooksmith::Idempotency.extract_key(provider:, payload:)
    if key && Hooksmith::Idempotency.already_processed?(provider:, key:)
      Hooksmith.logger.info("Skipping duplicate webhook: #{provider}/#{event} (key=#{key})")
      return nil
    end
  end

  Hooksmith::Dispatcher.new(provider:, event:, payload:).run!
end