Skip to content

Commit

Permalink
Update config template (#17)
Browse files Browse the repository at this point in the history
and allow lazy resolution of the job class from name, so that Rails may
reload/autoload the class.
  • Loading branch information
julik authored Jul 25, 2024
1 parent c0cadee commit 61aa64e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
10 changes: 9 additions & 1 deletion lib/munster/base_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ def handle(action_dispatch_request)
webhook = Munster::ReceivedWebhook.new(request: action_dispatch_request, handler_event_id: handler_event_id, handler_module_name: handler_module_name)
webhook.save!

Munster.configuration.processing_job_class.perform_later(webhook)
# The configured job class can be a class name or a module, to support lazy loading
job_class_or_module_name = Munster.configuration.processing_job_class
job_class = if job_class_or_module_name.respond_to?(:perform_later)
job_class_or_module_name
else
job_class_or_module_name.constantize
end

job_class.perform_later(webhook)
rescue ActiveRecord::RecordNotUnique # Webhook deduplicated
Rails.logger.info { "#{inspect} Webhook #{handler_event_id} is a duplicate delivery and will not be stored." }
end
Expand Down
20 changes: 16 additions & 4 deletions lib/munster/templates/munster.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
Munster.configure do |config|
# Active Handlers are defined as hash with key as a service_id and handler class that would handle webhook request.
# A Handler must respond to `.new` and return an object roughly matching `Munster::BaseHandler` in terms of interface.
# Use module names (strings) here to allow the handler modules to be lazy-loaded by Rails.
#
# Example:
# {:test => TestHandler, :inactive => InactiveHandler}
# {:test => "TestHandler", :inactive => "InactiveHandler"}
config.active_handlers = {}

# It's possible to overwrite default processing job to enahance it. As example if you want to add proper locking or retry mechanism.
# It's possible to overwrite default processing job to enahance it. As example if you want to add custom
# locking or retry mechanism. You want to inherit that job from Munster::ProcessingJob because the background
# job also manages the webhook state.
#
# Example:
#
Expand All @@ -15,14 +20,21 @@
# end
# end
#
# This is how you can change processing job:
# In the config a string with your job' class name can be used so that the job can be lazy-loaded by Rails:
#
# config.processing_job_class = WebhookProcessingJob
# config.processing_job_class = "WebhookProcessingJob"

# We're using a common interface for error reporting provided by Rails, e.g Rails.error.report. In some cases
# you want to enhance those errors with additional context. As example to provide a namespace:
#
# { appsignal: { namespace: "webhooks" } }
#
# config.error_context = { appsignal: { namespace: "webhooks" } }

# Incoming webhooks will be written into your DB without any prior validation. By default, Munster limits the
# request body size for webhooks to 512 KiB, so that it would not be too easy for an attacker to fill your
# database with junk. However, if you are receiving very large webhook payloads you might need to increase
# that limit (or make it even smaller for extra security)
#
# config.request_body_size_limit = 2.megabytes
end

0 comments on commit 61aa64e

Please sign in to comment.