-
Notifications
You must be signed in to change notification settings - Fork 21.9k
Add MailDeliveryJob for unified mail delivery #34591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_job" | ||
|
||
module ActionMailer | ||
# The <tt>ActionMailer::NewDeliveryJob</tt> class is used when you | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like this one mention of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. Thanks |
||
# want to send emails outside of the request-response cycle. It supports | ||
# sending either parameterized or normal mail. | ||
# | ||
# Exceptions are rescued and handled by the mailer class. | ||
class MailDeliveryJob < ActiveJob::Base # :nodoc: | ||
queue_as { ActionMailer::Base.deliver_later_queue_name } | ||
|
||
rescue_from StandardError, with: :handle_exception_with_mailer_class | ||
|
||
def perform(mailer, mail_method, delivery_method, args:, params: nil) #:nodoc: | ||
mailer_class = params ? mailer.constantize.with(params) : mailer.constantize | ||
mailer_class.public_send(mail_method, *args).send(delivery_method) | ||
end | ||
|
||
private | ||
# "Deserialize" the mailer class name by hand in case another argument | ||
# (like a Global ID reference) raised DeserializationError. | ||
def mailer_class | ||
if mailer = Array(@serialized_arguments).first || Array(arguments).first | ||
mailer.constantize | ||
end | ||
end | ||
|
||
def handle_exception_with_mailer_class(exception) | ||
if klass = mailer_class | ||
klass.handle_exception exception | ||
else | ||
raise exception | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# frozen_string_literal: true | ||
|
||
require "abstract_unit" | ||
require "active_job" | ||
require "mailers/params_mailer" | ||
require "mailers/delayed_mailer" | ||
|
||
class LegacyDeliveryJobTest < ActiveSupport::TestCase | ||
include ActiveJob::TestHelper | ||
|
||
class LegacyDeliveryJob < ActionMailer::DeliveryJob | ||
end | ||
|
||
class LegacyParmeterizedDeliveryJob < ActionMailer::Parameterized::DeliveryJob | ||
end | ||
|
||
setup do | ||
@previous_logger = ActiveJob::Base.logger | ||
ActiveJob::Base.logger = Logger.new(nil) | ||
|
||
@previous_delivery_method = ActionMailer::Base.delivery_method | ||
ActionMailer::Base.delivery_method = :test | ||
|
||
@previous_deliver_later_queue_name = ActionMailer::Base.deliver_later_queue_name | ||
ActionMailer::Base.deliver_later_queue_name = :test_queue | ||
end | ||
|
||
teardown do | ||
ActiveJob::Base.logger = @previous_logger | ||
ParamsMailer.deliveries.clear | ||
|
||
ActionMailer::Base.delivery_method = @previous_delivery_method | ||
ActionMailer::Base.deliver_later_queue_name = @previous_deliver_later_queue_name | ||
end | ||
|
||
test "should send parameterized mail correctly" do | ||
mail = ParamsMailer.with(inviter: "[email protected]", invitee: "[email protected]").invitation | ||
args = [ | ||
"ParamsMailer", | ||
"invitation", | ||
"deliver_now", | ||
{ inviter: "[email protected]", invitee: "[email protected]" }, | ||
] | ||
|
||
with_delivery_job(LegacyParmeterizedDeliveryJob) do | ||
assert_deprecated do | ||
assert_performed_with(job: LegacyParmeterizedDeliveryJob, args: args) do | ||
mail.deliver_later | ||
end | ||
end | ||
end | ||
end | ||
|
||
test "should send mail correctly" do | ||
mail = DelayedMailer.test_message(1, 2, 3) | ||
args = [ | ||
"DelayedMailer", | ||
"test_message", | ||
"deliver_now", | ||
1, | ||
2, | ||
3, | ||
] | ||
|
||
with_delivery_job(LegacyDeliveryJob) do | ||
assert_deprecated do | ||
assert_performed_with(job: LegacyDeliveryJob, args: args) do | ||
mail.deliver_later | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def with_delivery_job(job) | ||
old_params_delivery_job = ParamsMailer.delivery_job | ||
old_regular_delivery_job = DelayedMailer.delivery_job | ||
ParamsMailer.delivery_job = job | ||
DelayedMailer.delivery_job = job | ||
yield | ||
ensure | ||
ParamsMailer.delivery_job = old_params_delivery_job | ||
DelayedMailer.delivery_job = old_regular_delivery_job | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
class ParameterizedTest < ActiveSupport::TestCase | ||
include ActiveJob::TestHelper | ||
|
||
class DummyDeliveryJob < ActionMailer::DeliveryJob | ||
class DummyDeliveryJob < ActionMailer::MailDeliveryJob | ||
end | ||
|
||
setup do | ||
|
@@ -42,9 +42,10 @@ class DummyDeliveryJob < ActionMailer::DeliveryJob | |
"ParamsMailer", | ||
"invitation", | ||
"deliver_now", | ||
{ inviter: "[email protected]", invitee: "[email protected]" }, | ||
params: { inviter: "[email protected]", invitee: "[email protected]" }, | ||
args: [], | ||
] | ||
assert_performed_with(job: ActionMailer::DeliveryJob, args: args) do | ||
assert_performed_with(job: ActionMailer::MailDeliveryJob, args: args) do | ||
@mail.deliver_later | ||
end | ||
end | ||
|
@@ -68,7 +69,8 @@ class DummyDeliveryJob < ActionMailer::DeliveryJob | |
"ParamsMailer", | ||
"invitation", | ||
"deliver_now", | ||
{ inviter: "[email protected]", invitee: "[email protected]" }, | ||
params: { inviter: "[email protected]", invitee: "[email protected]" }, | ||
args: [], | ||
] | ||
|
||
with_delivery_job DummyDeliveryJob do | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
Parameterized::DeliveryJob
isn't released yet can we instead deprecate justDeliveryJob
and then recommend the new one? If you need theparams
key then you should use the new one.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed
Parameterized::DeliveryJob
from the warning, but the warning will still happen for parameterized delivery jobs because its a subclass. I assume this is good enough?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking that
Parameterized::DeliveryJob
has never been released so we can delete the code that supportedParameterized::DeliveryJob
and anyone who needs aParameterized::DeliveryJob
would use the newMailDeliveryJob
instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is released in all versions since
5.1.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see I was thinking of this PR 251b9d4 which is only in master.