Skip to content

Instantly share code, notes, and snippets.

@lloydwatkin
Created November 7, 2024 11:45
Show Gist options
  • Save lloydwatkin/e8c1fcd08902ee08adb54221a7d8b99b to your computer and use it in GitHub Desktop.
Save lloydwatkin/e8c1fcd08902ee08adb54221a7d8b99b to your computer and use it in GitHub Desktop.
ActionMailer using pure Sidekiq

ActionMailer using pure Sidekiq

Steps:

  • Implement monkey patch for ActionMailer
  • Have your mailers extend ApplicationMailer (or set self.delivery_job in your individual mailers)
  • Define ActionMailerJob

This allows you to continue using .deliver_later and .deliver_now as you do currently with ActionJob implementation.

# i.e. Sidekiq job
class ApplicationMailerJob
include Sideiq::Worker
sidekiq_options queue: "low_priority"
def perform(mailer, action, arguments)
mailer.constantize.send(action, arguments).deliver_now
end
end
# Monkey patch for ActionMailer
module ActionMailer
class MessageDelivery
def enqueue_delivery(_delivery_method, options = {})
@args = @args.map do |argument|
if argument.is_a? Hash
argument.deep_stringify_keys
else
argument
end
end
@mailer_class.delivery_job.set(options).perform_async(
@mailer_class.name, @action.to_s, @args
)
end
end
end
class ApplicationMailer < ActionMailer::Base
self.delivery_job = ApplicationMailerJob
end
# e.g.
#
# class UserMailer < ApplicationMailer
# def welcome(email)
# end
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment