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

Listen to the notifications in a separate listener class

Oliver Güntherba6465f106d9
diff --git a/app/workers/active_job/job_status_listener.rb b/app/workers/active_job/job_status_listener.rb
index 225e304c9d4..70121cdd241 100644
--- a/app/workers/active_job/job_status_listener.rb
+++ b/app/workers/active_job/job_status_listener.rb
@@ -26,60 +26,94 @@
# See docs/COPYRIGHT.rdoc for more details.
#++
-class JobStatusListener
- class << self
- def register!
- # Listen to enqueues
- ActiveSupport::Notifications.subscribe(/enqueue(_at)?\.active_job/) do |job, **_args|
- create_job_status(job) unless job.store_status?
- end
+module ActiveJob
+ class JobStatusListener
+ class << self
+ def register!
+ # Listen to enqueues
+ ActiveSupport::Notifications.subscribe(/enqueue(_at)?\.active_job/) do |_name, job:, **_args|
+ create_job_status(job) unless job.store_status?
+ end
- # Start of process
- ActiveSupport::Notifications.subscribe("perform_start.active_job") do |job, **_args|
- on_start(job) unless job.store_status?
- end
+ # Start of process
+ ActiveSupport::Notifications.subscribe('perform_start.active_job') do |job:, **_args|
+ on_start(job) unless job.store_status?
+ end
+
+ # Complete, or failure
+ ActiveSupport::Notifications.subscribe('perform.active_job') do |job:, exception_object: nil, **_args|
+ on_performed(job, exception_object) unless job.store_status?
+ end
+
+ # Retry stopped -> failure
+ ActiveSupport::Notifications.subscribe('retry_stopped.active_job') do |job:, error: nil, **_args|
+ on_performed(job, error) unless job.store_status?
+ end
+
+ # Retry enqueued
+ ActiveSupport::Notifications.subscribe('enqueue_retry.active_job') do |job, error: nil, **_args|
+ on_requeue(job, error) unless job.store_status?
+ end
- # Complete, or failure
- ActiveSupport::Notifications.subscribe("perform.active_job") do |job:, exception_object: nil, **_args|
- on_performed(job, exception_object) unless job.store_status?
+ # Discarded job
+ ActiveSupport::Notifications.subscribe('discard.active_job') do |job:, error: nil, **_args|
+ on_cancelled(job, error) unless job.store_status?
+ end
end
- end
- private
+ private
- ##
- # Create a status object when enqueuing a
- # new job through activejob that stores statuses
- def create_job_status(job)
- Delayed::Job::Status.create status: :in_queue,
- reference: job.status_reference,
- job_id: job.job_id
- end
+ ##
+ # Create a status object when enqueuing a
+ # new job through activejob that stores statuses
+ def create_job_status(job)
+ Delayed::Job::Status.create status: :in_queue,
+ reference: job.status_reference,
+ user: User.current,
+ job_id: job.job_id
+ end
- ##
- # On start processing a new job
- def on_start(job)
- update_status job, code: :in_process
- end
+ ##
+ # On start processing a new job
+ def on_start(job)
+ update_status job, code: :in_process
+ end
- ##
- # On job performed, update status
- def on_performed(job, exception_object)
- if exception_object
+ ##
+ # On requeuing a job after error
+ def on_requeue(job, error)
update_status job,
- code: :failure,
- message: exception_object.to_s
- else
- update_status job, code: :success
+ code: :in_queue,
+ message: I18n.t('background_jobs.status.error_requeue', message: error)
+ end
+
+ ##
+ # On cancellation due to the given error
+ def on_cancelled(job, error)
+ update_status job,
+ code: :cancelled,
+ message: I18n.t('background_jobs.status.cancelled_due_to', message: error)
+ end
+
+ ##
+ # On job performed, update status
+ def on_performed(job, exception_object)
+ if exception_object
+ update_status job,
+ code: :failure,
+ message: exception_object.to_s
+ else
+ update_status job, code: :success
+ end
end
- end
- ##
- # Update the status code for a given job
- def update_status(job, code:, message: nil)
- Delayed::Job::Status
- .where(job_id: job.job_id)
- .update_all(status: code, message: message)
+ ##
+ # Update the status code for a given job
+ def update_status(job, code:, message: nil)
+ Delayed::Job::Status
+ .where(job_id: job.job_id)
+ .update_all(status: code, message: message)
+ end
end
end
end
diff --git a/config/initializers/job_status.rb b/config/initializers/job_status.rb
index 275cf124340..ca5b556aae0 100644
--- a/config/initializers/job_status.rb
+++ b/config/initializers/job_status.rb
@@ -32,46 +32,4 @@
# indenpendently from the job itself (which is deleted once successful or after max attempts).
# That way, the result of a background job is available even after the original job is gone.
-ActiveSupport::Notifications.subscribe "perform.active_job" do |job:, exception_object: nil, **_args|
- next unless job.status_reference
-
- # job.provider_job_id is not filled at this point as
- # the ActiveJob adapter for DelayedJob is only setting it
- # on enqueue and enqueue_at.
- if exception_object
- dj_job_attempts = Delayed::Job.where(id: Delayed::Job::Status.of_reference(job.status_reference).select(:job_id))
- .pluck(:attempts)
- .first || 1
-
- new_status = if dj_job_attempts + 1 >= Delayed::Worker.max_attempts
- :failure
- else
- :error
- end
-
- Delayed::Job::Status
- .of_reference(job.status_reference)
- .update(status: new_status,
- message: exception_object)
- else
- Delayed::Job::Status
- .of_reference(job.status_reference)
- .update(status: :success)
- end
-end
-
-ActiveSupport::Notifications.subscribe "enqueue.active_job" do |job:, **_args|
- if job.status_reference
- Delayed::Job::Status.create(status: :in_queue,
- reference: job.status_reference,
- job_id: job.provider_job_id)
- end
-end
-
-ActiveSupport::Notifications.subscribe "enqueue_at.active_job" do |job:, **_args|
- if job.status_reference
- Delayed::Job::Status.create(status: :in_queue,
- reference: job.status_reference,
- job_id: job.provider_job_id)
- end
-end
+::ActiveJob::JobStatusListener.register!
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 13f85a5a8ff..22b4a6b6996 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -100,6 +100,11 @@ en:
no_results_content_title: There are currently no authentication modes.
no_results_content_text: Create a new authentication mode
+ background_jobs:
+ status:
+ error_requeue: "Job experienced an error but is retrying. The error was: %{message}"
+ cancelled_due_to: "Job was cancelled due to error: %{message}"
+
ldap_auth_sources:
technical_warning_html: |
This LDAP form requires technical knowledge of your LDAP / Active Directory setup.