-
Notifications
You must be signed in to change notification settings - Fork 0
Publisher best practices
Adam Mikulasev edited this page Dec 28, 2024
·
9 revisions
By default, the default Outboxer publisher maps model events to sidekiq job handlers like this:
when Accountify::Models::Invoice::CreatedEvent
then Accountify::Invoice::CreatedJob.perform_async
# bin/outboxer_publisher
Outboxer::Publisher.publish do |message|
OutboxerIntegration::Message::PublishJob.perform_async({
'messageable_id' => message[:messageable_id],
'messageable_type' => message[:messageable_type] })
end
# app/jobs/outboxer_integration/message/publish_job.rb
module OutboxerIntegration
module Message
class PublishJob
include Sidekiq::Job
sidekiq_options retry: false, backtrace: true
MESSAGEABLE_TYPE_REGEX = /\A([A-Za-z]+)::Models::([A-Za-z]+)::([A-Za-z]+)Event\z/
def perform(args)
messageable_type = args['messageable_type']
if !messageable_type.match(MESSAGEABLE_TYPE_REGEX)
raise StandardError, "Unexpected class name format: #{messageable_type}"
end
namespace, model, event = messageable_type.match(MESSAGEABLE_TYPE_REGEX).captures
job_class_name = "#{namespace}::#{model}::#{event}Job"
job_class = job_class_name.constantize
job_class.perform_async({ 'id' => args['messageable_id'] })
end
end
end
end