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

[33413] Use around hook to reload config in jobs

Oliver Güntherc94c1951b9fc
diff --git a/app/workers/application_job.rb b/app/workers/application_job.rb
index 542ecd08187..66536d1b49c 100644
--- a/app/workers/application_job.rb
+++ b/app/workers/application_job.rb
@@ -29,6 +29,11 @@
require 'active_job'
class ApplicationJob < ::ActiveJob::Base
+ around_perform do |_job, block|
+ reload_mailer_configuration!
+ with_clean_request_store { block.call }
+ end
+
##
# Return a priority number on the given payload
def self.priority_number(prio = :default)
@@ -52,8 +57,31 @@ class ApplicationJob < ::ActiveJob::Base
end
end
- def self.inherited(child)
- child.prepend Setup
+ # Resets the thread local request store.
+ # This should be done, because normal application code expects the RequestStore to be
+ # invalidated between multiple requests and does usually not care whether it is executed
+ # from a request or from a delayed job.
+ # For a delayed job, each job execution is the thing that comes closest to
+ # the concept of a new request.
+ def with_clean_request_store
+ store = RequestStore.store
+
+ begin
+ RequestStore.clear!
+ yield
+ ensure
+ # Reset to previous value
+ RequestStore.clear!
+ RequestStore.store.merge! store
+ end
+ end
+
+ # Reloads the thread local ActionMailer configuration.
+ # Since the email configuration is now done in the web app, we need to
+ # make sure that any changes to the configuration is correctly picked up
+ # by the background jobs at runtime.
+ def reload_mailer_configuration!
+ OpenProject::Configuration.reload_mailer_configuration!
end
# Delayed jobs can have a status:
@@ -63,42 +91,4 @@ class ApplicationJob < ::ActiveJob::Base
def status_reference
nil
end
-
- module Setup
- def perform(*args)
- before_perform!
- with_clean_request_store { super }
- end
-
- def before_perform!
- reload_mailer_configuration!
- end
-
- # Resets the thread local request store.
- # This should be done, because normal application code expects the RequestStore to be
- # invalidated between multiple requests and does usually not care whether it is executed
- # from a request or from a delayed job.
- # For a delayed job, each job execution is the thing that comes closest to
- # the concept of a new request.
- def with_clean_request_store
- store = RequestStore.store
-
- begin
- RequestStore.clear!
- yield
- ensure
- # Reset to previous value
- RequestStore.clear!
- RequestStore.store.merge! store
- end
- end
-
- # Reloads the thread local ActionMailer configuration.
- # Since the email configuration is now done in the web app, we need to
- # make sure that any changes to the configuration is correctly picked up
- # by the background jobs at runtime.
- def reload_mailer_configuration!
- OpenProject::Configuration.reload_mailer_configuration!
- end
- end
end
diff --git a/spec/workers/application_job_spec.rb b/spec/workers/application_job_spec.rb
index 386f47d0d94..336154e82e3 100644
--- a/spec/workers/application_job_spec.rb
+++ b/spec/workers/application_job_spec.rb
@@ -47,10 +47,10 @@ describe ApplicationJob do
end)
RequestStore[:test_value] = 'my value'
- expect { job.perform }.not_to change { RequestStore[:test_value] }
+ expect { job.perform_now }.not_to change { RequestStore[:test_value] }
RequestStore[:test_value] = 'my value2'
- expect { job.perform }.not_to change { RequestStore[:test_value] }
+ expect { job.perform_now }.not_to change { RequestStore[:test_value] }
expect(RequestStore[:test_value]).to eq 'my value2'
end