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

Allow bulk move/copy actions to happen in the background

Oliver Güntheref5f78ee8a64
diff --git a/app/controllers/work_packages/bulk_controller.rb b/app/controllers/work_packages/bulk_controller.rb
index 29996a24aed..6a956432b51 100644
--- a/app/controllers/work_packages/bulk_controller.rb
+++ b/app/controllers/work_packages/bulk_controller.rb
@@ -35,7 +35,7 @@ class WorkPackages::BulkController < ApplicationController
include RelationsHelper
include QueriesHelper
- include WorkPackages::FlashBulkError
+ include WorkPackages::BulkErrorMessage
def edit
setup_edit
@@ -50,7 +50,7 @@ class WorkPackages::BulkController < ApplicationController
flash[:notice] = t(:notice_successful_update)
redirect_back_or_default(controller: '/work_packages', action: :index, project_id: @project)
else
- error_flash(@work_packages, @call)
+ flash[:error] = bulk_error_message(@work_packages, @call)
setup_edit
render action: :edit
end
diff --git a/app/controllers/work_packages/flash_bulk_error.rb b/app/controllers/work_packages/bulk_error_message.rb
similarity index 77%
rename from app/controllers/work_packages/flash_bulk_error.rb
rename to app/controllers/work_packages/bulk_error_message.rb
index 7a270196c6d..336a9e395e6 100644
--- a/app/controllers/work_packages/flash_bulk_error.rb
+++ b/app/controllers/work_packages/bulk_error_message.rb
@@ -26,16 +26,16 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-module WorkPackages::FlashBulkError
+module WorkPackages::BulkErrorMessage
extend ActiveSupport::Concern
included do
private
- def error_flash(selected_work_packages, service_result)
- flash[:error] = render_to_string partial: 'work_packages/bulk/errors',
- locals: { service_result:,
- selected_work_packages: }
+ def bulk_error_message(selected_work_packages, service_result)
+ ApplicationController.renderer.render_to_string partial: 'work_packages/bulk/errors',
+ locals: { service_result:,
+ selected_work_packages: }
end
end
end
diff --git a/app/controllers/work_packages/moves_controller.rb b/app/controllers/work_packages/moves_controller.rb
index 9dcb5a0582a..8791f00f6a9 100644
--- a/app/controllers/work_packages/moves_controller.rb
+++ b/app/controllers/work_packages/moves_controller.rb
@@ -27,7 +27,7 @@
#++
class WorkPackages::MovesController < ApplicationController
- include WorkPackages::FlashBulkError
+ include WorkPackages::BulkErrorMessage
default_search_scope :work_packages
before_action :find_work_packages, :check_project_uniqueness
@@ -40,45 +40,59 @@ class WorkPackages::MovesController < ApplicationController
def create
prepare_for_work_package_move
- result = modify_call
-
- set_flash_from_bulk_work_package_save(@work_packages, result)
-
- redirect_after_create(result)
+ perform_operation
end
private
- def modify_call
- klass = if @copy
- WorkPackages::Bulk::CopyService
- else
- WorkPackages::Bulk::MoveService
- end
+ def perform_operation
+ if within_frontend_treshold?
+ perform_in_frontend
+ else
+ perform_in_background
+ end
+ end
- klass
- .new(user: current_user, work_packages: @work_packages)
- .call(attributes_for_create)
+ def within_frontend_treshold?
+ WorkPackageHierarchy.where(ancestor_id: @work_packages).count <= Setting.work_packages_bulk_request_limit
end
- def redirect_after_create(result)
- if params[:follow]
- if result.success? && @work_packages.size == 1
- redirect_to work_package_path(result.dependent_results.first.result)
- else
- redirect_to project_work_packages_path(@target_project || @project)
- end
+ # rubocop:disable Metrics/AbcSize
+ def perform_in_frontend
+ call = job_class
+ .perform_now(**job_args)
+
+ if call.success? && @work_packages.any?
+ flash[:notice] = call.message
+ redirect_to call.result
else
+ flash[:error] = bulk_error_message(@work_packages, call.dependent_results.first)
redirect_back_or_default(project_work_packages_path(@project))
end
end
+ # rubocop:enable Metrics/AbcSize
+
+ def perform_in_background
+ job = job_class.perform_later(**job_args)
+ redirect_to job_status_path(job.job_id)
+ end
+
+ def job_args
+ {
+ user: current_user,
+ work_package_ids: @work_packages.pluck(:id),
+ project: @project,
+ target_project: @target_project,
+ params: attributes_for_create,
+ follow: params[:follow]
+ }
+ end
- def set_flash_from_bulk_work_package_save(work_packages, service_result)
- if service_result.success? && work_packages.any?
- flash[:notice] = @copy ? I18n.t(:notice_successful_create) : I18n.t(:notice_successful_update)
+ def job_class
+ if @copy
+ WorkPackages::BulkCopyJob
else
- error_flash(work_packages,
- service_result)
+ WorkPackages::BulkMoveJob
end
end
diff --git a/app/workers/work_packages/bulk_job.rb b/app/workers/work_packages/bulk_job.rb
new file mode 100644
index 00000000000..8512b67aecd
--- /dev/null
+++ b/app/workers/work_packages/bulk_job.rb
@@ -0,0 +1,111 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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-2013 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+module WorkPackages
+ class BulkJob < ApplicationJob
+ include WorkPackages::BulkErrorMessage
+ queue_with_priority :above_normal
+
+ attr_accessor :work_packages, :user, :follow, :project, :target_project
+
+ def perform(user:, work_package_ids:, project:, target_project:, params:, follow:)
+ self.project = project
+ self.target_project = target_project
+ self.user = user
+ self.follow = follow
+ self.work_packages = WorkPackage.where(id: work_package_ids)
+
+ service_class
+ .new(user:, work_packages:)
+ .call(params)
+ .on_success(&method(:successful_status_update))
+ .on_failure(&method(:failure_status_update))
+ .then(&method(:wrap_result))
+ end
+
+ def store_status?
+ true
+ end
+
+ def updates_own_status?
+ true
+ end
+
+ protected
+
+ def wrap_result(call)
+ message = call.success? ? success_message : bulk_error_message(work_packages, call)
+
+ ServiceResult.new(
+ success: call.success,
+ result: redirect_path(call, follow),
+ message:,
+ dependent_results: [call]
+ )
+ end
+
+ def service_class
+ raise NotImplementedError
+ end
+
+ def successful_status_update(call)
+ path = redirect_path(call, follow)
+ payload = redirect_payload(path)
+
+ if call.errors.any?
+ payload[:errors] = errors
+ end
+
+ upsert_status status: :success,
+ message: success_message,
+ payload:
+ end
+
+ def redirect_path(call, follow)
+ if follow
+ if call.success? && work_packages.size == 1
+ url_helpers.work_package_path(call.dependent_results.first.result)
+ else
+ url_helpers.project_work_packages_path(target_project || project)
+ end
+ else
+ url_helpers.project_work_packages_path(project)
+ end
+ end
+
+ def failure_status_update(call)
+ message = bulk_error_message(work_packages, call)
+
+ upsert_status status: :failure, message:
+ end
+
+ def url_helpers
+ @url_helpers ||= OpenProject::StaticRouting::StaticUrlHelpers.new
+ end
+ end
+end
diff --git a/app/workers/work_packages/copy_job.rb b/app/workers/work_packages/copy_job.rb
new file mode 100644
index 00000000000..261cadbfd31
--- /dev/null
+++ b/app/workers/work_packages/copy_job.rb
@@ -0,0 +1,41 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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-2013 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+module WorkPackages
+ class CopyJob < BulkJob
+ protected
+
+ def service_class
+ Bulk::CopyService
+ end
+
+ def success_message
+ I18n.t(:notice_successful_create)
+ end
+ end
+end
diff --git a/app/workers/work_packages/move_job.rb b/app/workers/work_packages/move_job.rb
new file mode 100644
index 00000000000..257ce0e1091
--- /dev/null
+++ b/app/workers/work_packages/move_job.rb
@@ -0,0 +1,41 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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-2013 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+module WorkPackages
+ class MoveJob < BulkJob
+ protected
+
+ def service_class
+ Bulk::MoveService
+ end
+
+ def success_message
+ I18n.t(:notice_successful_update)
+ end
+ end
+end
diff --git a/config/constants/settings/definition.rb b/config/constants/settings/definition.rb
index fef3c36acda..1fcf584454f 100644
--- a/config/constants/settings/definition.rb
+++ b/config/constants/settings/definition.rb
@@ -989,6 +989,9 @@ module Settings
work_packages_projects_export_limit: {
default: 500
},
+ work_packages_bulk_request_limit: {
+ default: 10
+ },
work_package_list_default_highlighted_attributes: {
default: [],
allowed: -> {