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
Correctly upsert statuses in all cases
Oliver Güntherd0c0fdd871ef
diff --git a/app/workers/copy_project_job.rb b/app/workers/copy_project_job.rbindex 22dcc6b0fd5..64a88a5cfcd 100644--- a/app/workers/copy_project_job.rb+++ b/app/workers/copy_project_job.rb@@ -61,17 +61,17 @@ class CopyProjectJob < ApplicationJobendif target_project- ProjectMailer.copy_project_succeeded(user, source_project, target_project, errors).deliver_nowsuccessful_status_update+ ProjectMailer.copy_project_succeeded(user, source_project, target_project, errors).deliver_nowelse- ProjectMailer.copy_project_failed(user, source_project, target_project_name, errors).deliver_nowfailure_status_update+ ProjectMailer.copy_project_failed(user, source_project, target_project_name, errors).deliver_nowendrescue StandardError => elogger.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_nowfailure_status_update+ ProjectMailer.copy_project_failed(user, source_project, target_project_name, errors).deliver_nowenddef store_status?@@ -85,15 +85,20 @@ class CopyProjectJob < ApplicationJobprivatedef 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))enddef 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: messageenddef userdiff --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.rbindex 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 JobStatusfalseend+ ##+ # 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 JobStatusdef 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)- endendenddiff --git a/modules/job_status/db/migrate/20200610124259_extend_job_status.rb b/modules/job_status/db/migrate/20200610124259_extend_job_status.rbindex 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 :payloadend+ 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 exportremove_reference :work_package_exports, :userdiff --git a/modules/job_status/lib/open_project/job_status/event_listener.rb b/modules/job_status/lib/open_project/job_status/event_listener.rbindex 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 statusesdef 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_queueend### On start processing a new jobdef on_start(job)- update_status job, code: :in_process+ job.upsert_status status: :in_processend### On requeuing a job after errordef 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 errordef 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 itselfdef 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_selsif !job.updates_own_status?- update_status job, code: :success+ job.upsert_status status: :successendend-- ##- # 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)- endendendenddiff --git a/spec/workers/copy_project_job_spec.rb b/spec/workers/copy_project_job_spec.rbindex 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 dois_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]+ }+ endlet(: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)endlet(: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.errorsend- it 'copies the project' do+ it 'copies the project', :aggregate_failures doexpect(Project.find_by(identifier: params[:identifier])).to eq(@copied_project)- end-- it 'sets descriptive validation errors' doexpect(@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'endend@@ -171,6 +176,12 @@ describe CopyProjectJob, type: :model do.and_return maildoubleexpect { 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_nilendend