Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Allow `deliver_later` using a custom ApplicationJob

Oliver Günther3aa3dea5125d
diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb
index 27492b82ee0..3e1a666c1a6 100644
--- a/app/controllers/account_controller.rb
+++ b/app/controllers/account_controller.rb
@@ -112,7 +112,7 @@ class AccountController < ApplicationController
# create a new token for password recovery
token = Token::Recovery.new(user_id: user.id)
if token.save
- UserMailer.password_lost(token).deliver_now
+ UserMailer.password_lost(token).deliver_later
flash[:notice] = l(:notice_account_lost_email_sent)
redirect_to action: 'login', back_url: home_url
return
@@ -462,7 +462,7 @@ class AccountController < ApplicationController
end
def send_activation_email!(token)
- UserMailer.user_signed_up(token).deliver_now
+ UserMailer.user_signed_up(token).deliver_later
end
def pending_auth_source_registration?
@@ -581,7 +581,7 @@ class AccountController < ApplicationController
# Sends an email to the administrators
admins = User.admin.active
admins.each do |admin|
- UserMailer.account_activation_requested(admin, user).deliver_now
+ UserMailer.account_activation_requested(admin, user).deliver_later
end
account_pending
elsif block_given?
diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb
index 3d24a891000..7a99d5da511 100644
--- a/app/controllers/admin_controller.rb
+++ b/app/controllers/admin_controller.rb
@@ -55,7 +55,7 @@ class AdminController < ApplicationController
# Force ActionMailer to raise delivery errors so we can catch it
ActionMailer::Base.raise_delivery_errors = true
begin
- @test = UserMailer.test_mail(User.current).deliver_now
+ @test = UserMailer.test_mail(User.current).deliver_later
flash[:notice] = I18n.t(:notice_email_sent, value: User.current.mail)
rescue => e
flash[:error] = I18n.t(:notice_email_error, value: Redmine::CodesetUtil.replace_invalid_utf8(e.message.dup))
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 0d1ca05693f..91850bb6588 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -173,7 +173,7 @@ class UsersController < ApplicationController
end
if @user.active? && send_information
- UserMailer.account_information(@user, @user.password).deliver_now
+ UserMailer.account_information(@user, @user.password).deliver_later
end
end
@@ -235,7 +235,7 @@ class UsersController < ApplicationController
elsif @user.save
flash[:notice] = I18n.t(:notice_successful_update)
if was_activated
- UserMailer.account_activated(@user).deliver_now
+ UserMailer.account_activated(@user).deliver_later
end
else
flash[:error] = I18n.t(:error_status_change_failed,
diff --git a/app/mailers/base_mailer.rb b/app/mailers/base_mailer.rb
index 59e81330a7e..2ac076e14c0 100644
--- a/app/mailers/base_mailer.rb
+++ b/app/mailers/base_mailer.rb
@@ -35,6 +35,9 @@ class BaseMailer < ActionMailer::Base
include OpenProject::LocaleHelper
+ # Send all delayed mails with the following job
+ self.delivery_job = ::MailerJob
+
# wrap in a lambda to allow changing at run-time
default from: Proc.new { Setting.mail_from }
diff --git a/app/models/comment.rb b/app/models/comment.rb
index d848257b82b..a480cb1b0b2 100644
--- a/app/models/comment.rb
+++ b/app/models/comment.rb
@@ -52,7 +52,7 @@ class Comment < ActiveRecord::Base
recipients = commented.recipients + commented.watcher_recipients
recipients.uniq.each do |user|
- UserMailer.news_comment_added(user, self, User.current).deliver_now
+ UserMailer.news_comment_added(user, self, User.current).deliver_later
end
end
end
diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb
index 5444b76c724..c5cb2215ef3 100644
--- a/app/models/mail_handler.rb
+++ b/app/models/mail_handler.rb
@@ -99,7 +99,7 @@ class MailHandler < ActionMailer::Base
@user = MailHandler.create_user_from_email(email)
if @user
log "[#{@user.login}] account created"
- UserMailer.account_information(@user, @user.password).deliver_now
+ UserMailer.account_information(@user, @user.password).deliver_later
else
log "could not create account for [#{sender_email}]", :error
return false
diff --git a/app/models/message.rb b/app/models/message.rb
index 74afa218ea0..250fbcf8f8a 100644
--- a/app/models/message.rb
+++ b/app/models/message.rb
@@ -153,7 +153,7 @@ class Message < ActiveRecord::Base
forum.watcher_recipients
to_mail.uniq.each do |user|
- UserMailer.message_posted(user, self, User.current).deliver_now
+ UserMailer.message_posted(user, self, User.current).deliver_later
end
end
end
diff --git a/app/models/news.rb b/app/models/news.rb
index 41e8b622e2b..bfc1170bd99 100644
--- a/app/models/news.rb
+++ b/app/models/news.rb
@@ -109,7 +109,7 @@ class News < ActiveRecord::Base
def send_news_added_mail
if Setting.notified_events.include?('news_added')
recipients.uniq.each do |user|
- UserMailer.news_added(user, self, User.current).deliver_now
+ UserMailer.news_added(user, self, User.current).deliver_later
end
end
end
diff --git a/app/models/wiki_content.rb b/app/models/wiki_content.rb
index 550f7a79cb7..b7ecbceb79b 100644
--- a/app/models/wiki_content.rb
+++ b/app/models/wiki_content.rb
@@ -92,7 +92,7 @@ class WikiContent < ActiveRecord::Base
return unless Setting.notified_events.include?('wiki_content_added')
create_recipients.uniq.each do |user|
- UserMailer.wiki_content_added(user, self, User.current).deliver_now
+ UserMailer.wiki_content_added(user, self, User.current).deliver_later
end
end
@@ -100,7 +100,7 @@ class WikiContent < ActiveRecord::Base
return unless Setting.notified_events.include?('wiki_content_updated')
update_recipients.uniq.each do |user|
- UserMailer.wiki_content_updated(user, self, User.current).deliver_now
+ UserMailer.wiki_content_updated(user, self, User.current).deliver_later
end
end
diff --git a/app/workers/application_job.rb b/app/workers/application_job.rb
index 6edc82a383e..fd9b058b932 100644
--- a/app/workers/application_job.rb
+++ b/app/workers/application_job.rb
@@ -26,7 +26,9 @@
# See docs/COPYRIGHT.rdoc for more details.
#++
-class ApplicationJob
+require 'active_job'
+
+class ApplicationJob < ::ActiveJob::Base
##
# Return a priority number on the given payload
@@ -48,7 +50,7 @@ class ApplicationJob
end
module Setup
- def perform
+ def perform(*args)
before_perform!
with_clean_request_store { super }
end
diff --git a/app/workers/deliver_invitation_job.rb b/app/workers/deliver_invitation_job.rb
index e39e5293056..ca424584ff4 100644
--- a/app/workers/deliver_invitation_job.rb
+++ b/app/workers/deliver_invitation_job.rb
@@ -36,7 +36,7 @@ class DeliverInvitationJob < ApplicationJob
def perform
if token
- UserMailer.user_signed_up(token).deliver_now
+ UserMailer.user_signed_up(token).deliver_later
else
Rails.logger.warn "Can't deliver invitation. The token is missing: #{token_id}"
end
diff --git a/app/workers/mail_user_job.rb b/app/workers/mailer_job.rb
similarity index 58%
rename from app/workers/mail_user_job.rb
rename to app/workers/mailer_job.rb
index cfe01835b50..b54b5bea905 100644
--- a/app/workers/mail_user_job.rb
+++ b/app/workers/mailer_job.rb
@@ -28,7 +28,7 @@
#++
##
-# We use this extra job instead of just calling
+# This job gets called when internally using
#
# ```
# UserMailer.some_mail("some param").deliver_later
@@ -39,55 +39,33 @@
# We want it to run in an `ApplicationJob` because of the shared setup required
# such as reloading the mailer configuration and resetting the request store.
#
-# Hence instead of the line above you would deliver an email later like this:
+# This class is automatically used whenever you call
#
-# ```
-# Delayed::Job.enqueue MailUserJob.new(:some_mail, "some param")
-# # or like this:
-# MailUserJob.some_mail "some_param"
-# ```
-class MailUserJob < ApplicationJob
- attr_reader :mail
+# UserMailer.method(*args).deliver_later
+class MailerJob < ApplicationJob
+ queue_as { ActionMailer::Base.deliver_later_queue_name }
- def initialize(mail, *args)
- @mail = mail
- @serialized_params = args.map { |arg| serialize_param arg }
- end
+ rescue_from StandardError, with: :handle_exception_with_mailer_class
- def perform
- UserMailer.send(mail, *params).deliver_now
- end
-
- def params
- @params ||= @serialized_params.map do |type, param, model_name|
- if type == :model
- deserialize_model param, model_name
- else
- param
- end
- end
- end
-
- def self.method_missing(method, *args, &block)
- UserMailer.send method unless UserMailer.respond_to? method # fail with NoMethodError
-
- job = MailUserJob.new method, *args
-
- Delayed::Job.enqueue job,
- priority: ::ApplicationJob.priority_number(:notification)
+ def perform(mailer, mail_method, delivery_method, *args)
+ mailer.constantize.public_send(mail_method, *args).send(delivery_method)
end
private
- def serialize_param(param)
- if param.is_a? ActiveRecord::Base
- [:model, param.id, param.class.name]
- else
- [:plain, param]
+ # "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 deserialize_model(id, model_name)
- model_name.constantize.find_by(id: id)
+ def handle_exception_with_mailer_class(exception)
+ if klass = mailer_class
+ klass.handle_exception exception
+ else
+ raise exception
+ end
end
end
diff --git a/lib/open_project/enterprise.rb b/lib/open_project/enterprise.rb
index 0ff142edf03..a65e0061033 100644
--- a/lib/open_project/enterprise.rb
+++ b/lib/open_project/enterprise.rb
@@ -73,7 +73,7 @@ module OpenProject
# the user limit having been reached.
def send_activation_limit_notification_about(user)
User.active.admin.each do |admin|
- MailUserJob.activation_limit_reached(user.mail, admin)
+ UserMailer.activation_limit_reached(user.mail, admin).deliver_later
end
end