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

Let CopyProjectJob redirect to the target project

Oliver Günther58562e737989
diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb
index 25ef4939409..5091310f7b6 100644
--- a/app/controllers/projects_controller.rb
+++ b/app/controllers/projects_controller.rb
@@ -281,7 +281,7 @@ class ProjectsController < ApplicationController
call_result.on_success do
flash[:notice] = t('project.template.copying')
- redirect_to home_path
+ redirect_to job_status_path(call_result.result.job_id)
end
call_result
diff --git a/app/services/projects/instantiate_template_service.rb b/app/services/projects/instantiate_template_service.rb
index 298bb848a29..49cdf348b68 100644
--- a/app/services/projects/instantiate_template_service.rb
+++ b/app/services/projects/instantiate_template_service.rb
@@ -41,7 +41,7 @@ module Projects
end
def after_validate(params, call)
- ::CopyProjectJob.perform_later(
+ job = ::CopyProjectJob.perform_later(
user_id: user.id,
source_project_id: template_id,
target_project_params: project_params(params),
@@ -51,7 +51,7 @@ module Projects
send_mails: true
)
- call
+ ServiceResult.new(success: true, result: job)
end
# Do not actually try to save the project here
diff --git a/app/workers/application_job.rb b/app/workers/application_job.rb
index 81f1d93db31..0d135a53ae7 100644
--- a/app/workers/application_job.rb
+++ b/app/workers/application_job.rb
@@ -29,6 +29,7 @@
require 'active_job'
class ApplicationJob < ::ActiveJob::Base
+ include ::JobStatus::ApplicationJobWithStatus
around_perform do |_job, block|
reload_mailer_configuration!
@@ -84,18 +85,4 @@ class ApplicationJob < ::ActiveJob::Base
def reload_mailer_configuration!
OpenProject::Configuration.reload_mailer_configuration!
end
-
- # Delayed jobs can have a status:
- # Delayed::Job::Status
- # which is related to the job via a reference which is an AR model instance.
- def status_reference
- nil
- end
-
- ##
- # Determine whether to store a status object for this job
- # By default, will only store if status_reference is present
- def store_status?
- !status_reference.nil?
- end
end
diff --git a/app/workers/copy_project_job.rb b/app/workers/copy_project_job.rb
index e53d5bde83a..22dcc6b0fd5 100644
--- a/app/workers/copy_project_job.rb
+++ b/app/workers/copy_project_job.rb
@@ -62,21 +62,40 @@ class CopyProjectJob < ApplicationJob
if target_project
ProjectMailer.copy_project_succeeded(user, source_project, target_project, errors).deliver_now
+ successful_status_update
else
ProjectMailer.copy_project_failed(user, source_project, target_project_name, errors).deliver_now
+ failure_status_update
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
end
def store_status?
true
end
+ def updates_own_status?
+ true
+ end
+
private
+ def successful_status_update
+ update_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
+ end
+
def user
@user ||= User.find user_id
end
@@ -171,4 +190,8 @@ class CopyProjectJob < ApplicationJob
base = error_object.instance_variable_get(:@base)
base.is_a?(Project) ? '' : "#{base.class.model_name.human} '#{base}': "
end
+
+ def url_helpers
+ @url_helpers ||= OpenProject::StaticRouting::StaticUrlHelpers.new
+ end
end
diff --git a/config/initializers/cronjobs.rb b/config/initializers/cronjobs.rb
index e831d344440..00e14d88fcc 100644
--- a/config/initializers/cronjobs.rb
+++ b/config/initializers/cronjobs.rb
@@ -4,7 +4,6 @@ OpenProject::Application.configure do |application|
application.config.to_prepare do
::Cron::CronJob.register! ::Cron::ClearOldSessionsJob,
::Cron::ClearTmpCacheJob,
- ::Cron::ClearUploadedFilesJob,
- ::Cron::ClearOldJobStatusJob
+ ::Cron::ClearUploadedFilesJob
end
end
diff --git a/frontend/src/app/modules/job-status/job-status-modal/job-status.modal.ts b/frontend/src/app/modules/job-status/job-status-modal/job-status.modal.ts
index 4f388af4473..3f0db57a717 100644
--- a/frontend/src/app/modules/job-status/job-status-modal/job-status.modal.ts
+++ b/frontend/src/app/modules/job-status/job-status-modal/job-status.modal.ts
@@ -30,7 +30,7 @@ export class JobStatusModal extends OpModalComponent implements OnInit {
public text = {
title: this.I18n.t('js.job_status.title'),
closePopup: this.I18n.t('js.close_popup_title'),
- exportPreparing: this.I18n.t('js.label_export_preparing')
+ redirect: this.I18n.t('js.job_status.redirect'),
};
/** The job ID reference */
@@ -95,6 +95,12 @@ export class JobStatusModal extends OpModalComponent implements OnInit {
this.message = response.message ||
this.I18n.t(`js.job_status.generic_messages.${status}`, { defaultValue: status });
+ const redirectUrl:string|undefined = response.payload?.redirect;
+ if (redirectUrl !== undefined) {
+ this.message += `. ${this.text.redirect}`;
+ setTimeout(() => window.location.href = redirectUrl, 2000);
+ }
+
this.cdRef.detectChanges();
}
diff --git a/frontend/src/app/modules/job-status/job-status.interface.ts b/frontend/src/app/modules/job-status/job-status.interface.ts
index 81b27afe1fe..3b39dca9fb7 100644
--- a/frontend/src/app/modules/job-status/job-status.interface.ts
+++ b/frontend/src/app/modules/job-status/job-status.interface.ts
@@ -21,6 +21,6 @@ export interface JobStatusInterface {
*/
payload?:{
download?:string;
- redirectTo?:string;
+ redirect?:string;
};
}
\ No newline at end of file
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
new file mode 100644
index 00000000000..d4853f499e7
--- /dev/null
+++ b/modules/job_status/app/workers/job_status/application_job_with_status.rb
@@ -0,0 +1,78 @@
+#-- encoding: UTF-8
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+module JobStatus
+ module ApplicationJobWithStatus
+ # Delayed jobs can have a status:
+ # Delayed::Job::Status
+ # which is related to the job via a reference which is an AR model instance.
+ def status_reference
+ nil
+ end
+
+ ##
+ # Determine whether to store a status object for this job
+ # By default, will only store if status_reference is present
+ def store_status?
+ !status_reference.nil?
+ end
+
+ ##
+ # For more complex handling of status updates
+ # jobs can do success messages themselves.
+ #
+ # In case of exceptions being caught by activejob
+ # the status will be modified outside.
+ def updates_own_status?
+ false
+ end
+
+ protected
+
+ ##
+ # Crafts a payload for a redirection result
+ def redirect_payload(path)
+ { redirect: path }
+ end
+
+ ##
+ # Crafts a payload for a download result
+ 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/app/workers/cron/clear_old_job_status_job.rb b/modules/job_status/app/workers/job_status/cron/clear_old_job_status_job.rb
similarity index 77%
rename from app/workers/cron/clear_old_job_status_job.rb
rename to modules/job_status/app/workers/job_status/cron/clear_old_job_status_job.rb
index 566c9b1a0f8..c1e3f697793 100644
--- a/app/workers/cron/clear_old_job_status_job.rb
+++ b/modules/job_status/app/workers/job_status/cron/clear_old_job_status_job.rb
@@ -28,17 +28,19 @@
# See docs/COPYRIGHT.rdoc for more details.
#++
-module Cron
- class ClearOldJobStatusJob < CronJob
- # runs at 4:15 nightly
- self.cron_expression = '15 4 * * *'
+module JobStatus
+ module Cron
+ class ClearOldJobStatusJob < ::Cron::CronJob
+ # runs at 4:15 nightly
+ self.cron_expression = '15 4 * * *'
- RETENTION_PERIOD = 2.days.freeze
+ RETENTION_PERIOD = 2.days.freeze
- def perform
- Delayed::Job::Status
- .where(Delayed::Job::Status.arel_table[:updated_at].lteq(Time.now - RETENTION_PERIOD))
- .destroy_all
+ def perform
+ ::JobStatus::Status
+ .where(::JobStatus::Status.arel_table[:updated_at].lteq(Time.now - RETENTION_PERIOD))
+ .destroy_all
+ end
end
end
end
diff --git a/modules/job_status/config/locales/js-en.yml b/modules/job_status/config/locales/js-en.yml
index 8d0d555a633..cd8a1535109 100644
--- a/modules/job_status/config/locales/js-en.yml
+++ b/modules/job_status/config/locales/js-en.yml
@@ -2,6 +2,7 @@ en:
js:
job_status:
title: 'Background job status'
+ redirect: 'You are being redirected.'
generic_messages:
in_queue: 'The job has been queued and will be processed shortly.'
in_process: 'The job is currently being processed.'
diff --git a/modules/job_status/lib/open_project/job_status/engine.rb b/modules/job_status/lib/open_project/job_status/engine.rb
index ee54ff1b2d9..a4adcdb28e1 100644
--- a/modules/job_status/lib/open_project/job_status/engine.rb
+++ b/modules/job_status/lib/open_project/job_status/engine.rb
@@ -51,8 +51,10 @@ module OpenProject::JobStatus
# Extends the ActiveJob adapter in use (DelayedJob) by a Status which lives
# 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.
-
EventListener.register!
+
+ # Register the cron job to clear statuses periodically
+ ::Cron::CronJob.register! ::JobStatus::Cron::ClearOldJobStatusJob
end
end
end
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 1ddd0af1c22..391a89b53da 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
@@ -46,7 +46,7 @@ module OpenProject
# Complete, or failure
ActiveSupport::Notifications.subscribe('perform.active_job') do |job:, exception_object: nil, **_args|
Rails.logger.debug do
- successful = exception_object ? "with error: #{exception_object}" : "successful"
+ successful = exception_object ? "with error #{exception_object}" : "successful"
"Background job #{job.inspect} was performed #{successful}."
end
@@ -85,10 +85,10 @@ module OpenProject
# 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
+ ::JobStatus::Status.create status: :in_queue,
+ reference: job.status_reference,
+ user: User.current,
+ job_id: job.job_id
end
##
@@ -114,13 +114,15 @@ module OpenProject
end
##
- # On job performed, update status
+ # On job performed, handle status updates
+ # - on error, always update
+ # - 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
- else
+ elsif !job.updates_own_status?
update_status job, code: :success
end
end
@@ -128,9 +130,9 @@ module OpenProject
##
# Update the status code for a given job
def update_status(job, code:, message: nil)
- Delayed::Job::Status
+ ::JobStatus::Status
.where(job_id: job.job_id)
- .update_all(status: code, message: message)
+ .update_all(status: code, message: message, payload: nil)
end
end
end
diff --git a/spec/controllers/work_packages/exports_controller_spec.rb b/spec/controllers/work_packages/exports_controller_spec.rb
index 166ff53f8b9..b3b717d4dc4 100644
--- a/spec/controllers/work_packages/exports_controller_spec.rb
+++ b/spec/controllers/work_packages/exports_controller_spec.rb
@@ -66,7 +66,7 @@ describe WorkPackages::ExportsController, type: :controller do
let!(:job_status) do
FactoryBot.build_stubbed(:delayed_job_status, status: status, message: job_message).tap do |s|
relation = double('AR relation')
- allow(Delayed::Job::Status)
+ allow(JobStatus::Status)
.to receive(:of_reference)
.with(export)
.and_return(relation)