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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Allow `deliver_later` using a custom ApplicationJob
Oliver Günther3aa3dea5125d
diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rbindex 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 recoverytoken = Token::Recovery.new(user_id: user.id)if token.save- UserMailer.password_lost(token).deliver_now+ UserMailer.password_lost(token).deliver_laterflash[:notice] = l(:notice_account_lost_email_sent)redirect_to action: 'login', back_url: home_urlreturn@@ -462,7 +462,7 @@ class AccountController < ApplicationControllerenddef send_activation_email!(token)- UserMailer.user_signed_up(token).deliver_now+ UserMailer.user_signed_up(token).deliver_laterenddef pending_auth_source_registration?@@ -581,7 +581,7 @@ class AccountController < ApplicationController# Sends an email to the administratorsadmins = User.admin.activeadmins.each do |admin|- UserMailer.account_activation_requested(admin, user).deliver_now+ UserMailer.account_activation_requested(admin, user).deliver_laterendaccount_pendingelsif block_given?diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rbindex 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 itActionMailer::Base.raise_delivery_errors = truebegin- @test = UserMailer.test_mail(User.current).deliver_now+ @test = UserMailer.test_mail(User.current).deliver_laterflash[:notice] = I18n.t(:notice_email_sent, value: User.current.mail)rescue => eflash[: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.rbindex 0d1ca05693f..91850bb6588 100644--- a/app/controllers/users_controller.rb+++ b/app/controllers/users_controller.rb@@ -173,7 +173,7 @@ class UsersController < ApplicationControllerendif @user.active? && send_information- UserMailer.account_information(@user, @user.password).deliver_now+ UserMailer.account_information(@user, @user.password).deliver_laterendend@@ -235,7 +235,7 @@ class UsersController < ApplicationControllerelsif @user.saveflash[:notice] = I18n.t(:notice_successful_update)if was_activated- UserMailer.account_activated(@user).deliver_now+ UserMailer.account_activated(@user).deliver_laterendelseflash[:error] = I18n.t(:error_status_change_failed,diff --git a/app/mailers/base_mailer.rb b/app/mailers/base_mailer.rbindex 59e81330a7e..2ac076e14c0 100644--- a/app/mailers/base_mailer.rb+++ b/app/mailers/base_mailer.rb@@ -35,6 +35,9 @@ class BaseMailer < ActionMailer::Baseinclude OpenProject::LocaleHelper+ # Send all delayed mails with the following job+ self.delivery_job = ::MailerJob+# wrap in a lambda to allow changing at run-timedefault from: Proc.new { Setting.mail_from }diff --git a/app/models/comment.rb b/app/models/comment.rbindex d848257b82b..a480cb1b0b2 100644--- a/app/models/comment.rb+++ b/app/models/comment.rb@@ -52,7 +52,7 @@ class Comment < ActiveRecord::Baserecipients = commented.recipients + commented.watcher_recipientsrecipients.uniq.each do |user|- UserMailer.news_comment_added(user, self, User.current).deliver_now+ UserMailer.news_comment_added(user, self, User.current).deliver_laterendendenddiff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rbindex 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 @userlog "[#{@user.login}] account created"- UserMailer.account_information(@user, @user.password).deliver_now+ UserMailer.account_information(@user, @user.password).deliver_laterelselog "could not create account for [#{sender_email}]", :errorreturn falsediff --git a/app/models/message.rb b/app/models/message.rbindex 74afa218ea0..250fbcf8f8a 100644--- a/app/models/message.rb+++ b/app/models/message.rb@@ -153,7 +153,7 @@ class Message < ActiveRecord::Baseforum.watcher_recipientsto_mail.uniq.each do |user|- UserMailer.message_posted(user, self, User.current).deliver_now+ UserMailer.message_posted(user, self, User.current).deliver_laterendendenddiff --git a/app/models/news.rb b/app/models/news.rbindex 41e8b622e2b..bfc1170bd99 100644--- a/app/models/news.rb+++ b/app/models/news.rb@@ -109,7 +109,7 @@ class News < ActiveRecord::Basedef send_news_added_mailif 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_laterendendenddiff --git a/app/models/wiki_content.rb b/app/models/wiki_content.rbindex 550f7a79cb7..b7ecbceb79b 100644--- a/app/models/wiki_content.rb+++ b/app/models/wiki_content.rb@@ -92,7 +92,7 @@ class WikiContent < ActiveRecord::Basereturn 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_laterendend@@ -100,7 +100,7 @@ class WikiContent < ActiveRecord::Basereturn 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_laterendenddiff --git a/app/workers/application_job.rb b/app/workers/application_job.rbindex 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 ApplicationJobendmodule Setup- def perform+ def perform(*args)before_perform!with_clean_request_store { super }enddiff --git a/app/workers/deliver_invitation_job.rb b/app/workers/deliver_invitation_job.rbindex e39e5293056..ca424584ff4 100644--- a/app/workers/deliver_invitation_job.rb+++ b/app/workers/deliver_invitation_job.rb@@ -36,7 +36,7 @@ class DeliverInvitationJob < ApplicationJobdef performif token- UserMailer.user_signed_up(token).deliver_now+ UserMailer.user_signed_up(token).deliver_laterelseRails.logger.warn "Can't deliver invitation. The token is missing: #{token_id}"enddiff --git a/app/workers/mail_user_job.rb b/app/workers/mailer_job.rbsimilarity index 58%rename from app/workers/mail_user_job.rbrename to app/workers/mailer_job.rbindex 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)endprivate- 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.constantizeendend- 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+ endendenddiff --git a/lib/open_project/enterprise.rb b/lib/open_project/enterprise.rbindex 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_laterendend