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

Correctly upsert statuses in all cases

Oliver Güntherd0c0fdd871ef
diff --git a/app/workers/copy_project_job.rb b/app/workers/copy_project_job.rb
index 22dcc6b0fd5..64a88a5cfcd 100644
--- a/app/workers/copy_project_job.rb
+++ b/app/workers/copy_project_job.rb
@@ -61,17 +61,17 @@ class CopyProjectJob < ApplicationJob
end
if target_project
- ProjectMailer.copy_project_succeeded(user, source_project, target_project, errors).deliver_now
successful_status_update
+ ProjectMailer.copy_project_succeeded(user, source_project, target_project, errors).deliver_now
else
- ProjectMailer.copy_project_failed(user, source_project, target_project_name, errors).deliver_now
failure_status_update
+ ProjectMailer.copy_project_failed(user, source_project, target_project_name, errors).deliver_now
end
rescue StandardError => e
logger.error { "Failed to finish copy project job: #{e} #{e.message}" }
errors = [I18n.t('copy_project.failed_internal')]
- ProjectMailer.copy_project_failed(user, source_project, target_project_name, errors).deliver_now
failure_status_update
+ ProjectMailer.copy_project_failed(user, source_project, target_project_name, errors).deliver_now
end
def store_status?
@@ -85,15 +85,20 @@ class CopyProjectJob < ApplicationJob
private
def successful_status_update
- update_status status: :success,
+ upsert_status status: :success,
message: I18n.t('copy_project.succeeded', target_project_name: target_project.name),
payload: redirect_payload(url_helpers.project_path(target_project))
end
def failure_status_update
- update_status status: :failure,
- message: I18n.t('copy_project.failed') + ". " + errors.join("\n"),
- payload: nil
+ message =
+ if errors
+ "#{I18n.t('copy_project.failed', source_project_name: source_project.name)}. #{errors.join("\n")}"
+ else
+ I18n.t('copy_project.failed')
+ end
+
+ upsert_status status: :failure, message: message
end
def user
diff --git a/modules/job_status/app/workers/job_status/application_job_with_status.rb b/modules/job_status/app/workers/job_status/application_job_with_status.rb
index a4e692d2588..871cc252a58 100644
--- a/modules/job_status/app/workers/job_status/application_job_with_status.rb
+++ b/modules/job_status/app/workers/job_status/application_job_with_status.rb
@@ -53,6 +53,30 @@ module JobStatus
false
end
+ ##
+ # Get the current status object, if any
+ def job_status
+ ::JobStatus::Status
+ .find_by(job_id: job_id)
+ end
+
+ ##
+ # Update the status code for a given job
+ def upsert_status(status:, **args)
+ upsert_attributes = args.reverse_merge(
+ status: status,
+ message: nil,
+ payload: nil,
+ reference_id: status_reference&.id,
+ reference_type: status_reference&.type,
+ user_id: User.current.id,
+ job_id: job_id
+ )
+
+ ::JobStatus::Status.upsert upsert_attributes,
+ unique_by: :job_id
+ end
+
protected
##
@@ -66,13 +90,5 @@ module JobStatus
def download_payload(path)
{ download: path }
end
-
- ##
- # Updates the associated status object
- def update_status(**args)
- ::JobStatus::Status
- .where(job_id: job_id)
- .update_all(args)
- end
end
end
diff --git a/modules/job_status/db/migrate/20200610124259_extend_job_status.rb b/modules/job_status/db/migrate/20200610124259_extend_job_status.rb
index 6cbd952c96a..31c0d03b3ab 100644
--- a/modules/job_status/db/migrate/20200610124259_extend_job_status.rb
+++ b/modules/job_status/db/migrate/20200610124259_extend_job_status.rb
@@ -16,10 +16,17 @@ class ExtendJobStatus < ActiveRecord::Migration[6.0]
change_table :delayed_job_statuses do |t|
t.references :user, index: true
- t.string :job_id, index: true
+ t.string :job_id, index: { unique: true }
t.jsonb :payload
end
+ reversible do |dir|
+ dir.up do
+ change_column_default :delayed_job_statuses, :created_at, -> { 'CURRENT_TIMESTAMP' }
+ change_column_default :delayed_job_statuses, :updated_at, -> { 'CURRENT_TIMESTAMP' }
+ end
+ end
+
# Now that we have user reference on job status
# we don't need it on export
remove_reference :work_package_exports, :user
diff --git a/modules/job_status/lib/open_project/job_status/event_listener.rb b/modules/job_status/lib/open_project/job_status/event_listener.rb
index 53b66da0d6f..4ec75d52857 100644
--- a/modules/job_status/lib/open_project/job_status/event_listener.rb
+++ b/modules/job_status/lib/open_project/job_status/event_listener.rb
@@ -87,31 +87,27 @@ module OpenProject
# Create a status object when enqueuing a
# new job through activejob that stores statuses
def create_job_status(job)
- ::JobStatus::Status.create status: :in_queue,
- reference: job.status_reference,
- user: User.current,
- job_id: job.job_id
+ job.upsert_status status: :in_queue
end
##
# On start processing a new job
def on_start(job)
- update_status job, code: :in_process
+ job.upsert_status status: :in_process
end
##
# On requeuing a job after error
def on_requeue(job, error)
- update_status job,
- code: :in_queue,
- message: I18n.t('background_jobs.status.error_requeue', message: error)
+ job.upsert_status status: :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,
+ upsert_status job,
+ status: :cancelled,
message: I18n.t('background_jobs.status.cancelled_due_to', message: error)
end
@@ -121,21 +117,12 @@ module OpenProject
# - on success, only update if job doesn't do it itself
def on_performed(job, exception_object)
if exception_object
- update_status job,
- code: :failure,
- message: exception_object.to_s
+ job.upsert_status status: :failure,
+ message: exception_object.to_s
elsif !job.updates_own_status?
- update_status job, code: :success
+ job.upsert_status status: :success
end
end
-
- ##
- # Update the status code for a given job
- def update_status(job, code:, message: nil)
- ::JobStatus::Status
- .where(job_id: job.job_id)
- .update_all(status: code, message: message, payload: nil)
- end
end
end
end
diff --git a/spec/workers/copy_project_job_spec.rb b/spec/workers/copy_project_job_spec.rb
index 933534e283c..22360be5904 100644
--- a/spec/workers/copy_project_job_spec.rb
+++ b/spec/workers/copy_project_job_spec.rb
@@ -76,13 +76,16 @@ describe CopyProjectJob, type: :model do
is_required: true,
is_for_all: true)
end
+ let(:job_args) do
+ {
+ user_id: admin.id,
+ source_project_id: source_project.id,
+ target_project_params: params,
+ associations_to_copy: [:work_packages]
+ }
+ end
let(:copy_job) do
- CopyProjectJob.new.tap do |job|
- job.perform user_id: admin.id,
- source_project_id: source_project.id,
- target_project_params: params,
- associations_to_copy: [:work_packages]
- end
+ CopyProjectJob.new(job_args).tap(&:perform_now)
end
let(:params) { {name: 'Copy', identifier: 'copy', type_ids: [type.id], work_package_custom_field_ids: [custom_field.id]} }
@@ -98,12 +101,14 @@ describe CopyProjectJob, type: :model do
@errors = copy_job.errors
end
- it 'copies the project' do
+ it 'copies the project', :aggregate_failures do
expect(Project.find_by(identifier: params[:identifier])).to eq(@copied_project)
- end
-
- it 'sets descriptive validation errors' do
expect(@errors.first).to eq(expected_error_message)
+
+ # expect to create a status
+ expect(copy_job.job_status).to be_present
+ expect(copy_job.job_status[:status]).to eq 'success'
+ expect(copy_job.job_status[:payload]['redirect']).to eq '/projects/copy'
end
end
@@ -171,6 +176,12 @@ describe CopyProjectJob, type: :model do
.and_return maildouble
expect { copy_job }.not_to raise_error
+
+ # expect to create a status
+ expect(copy_job.job_status).to be_present
+ expect(copy_job.job_status[:status]).to eq 'failure'
+ expect(copy_job.job_status[:message]).to include "Cannot copy project #{source_project.name}"
+ expect(copy_job.job_status[:payload]).to be_nil
end
end