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

bail on altering working days before last change is processed

ulferts81d52906d78d
diff --git a/app/contracts/settings/working_days_params_contract.rb b/app/contracts/settings/working_days_params_contract.rb
index 635e08a7956..bfc89faf6f9 100644
--- a/app/contracts/settings/working_days_params_contract.rb
+++ b/app/contracts/settings/working_days_params_contract.rb
@@ -31,6 +31,7 @@ module Settings
include RequiresAdminGuard
validate :working_days_are_present
+ validate :unique_job
protected
@@ -40,6 +41,12 @@ module Settings
end
end
+ def unique_job
+ if WorkPackages::ApplyWorkingDaysChangeJob.scheduled?
+ errors.add :base, :previous_working_day_changes_unprocessed
+ end
+ end
+
def working_days
params[:working_days]
end
diff --git a/app/workers/concerns/scheduled_job.rb b/app/workers/concerns/scheduled_job.rb
new file mode 100644
index 00000000000..dfd0bce8e49
--- /dev/null
+++ b/app/workers/concerns/scheduled_job.rb
@@ -0,0 +1,43 @@
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2022 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 ScheduledJob
+ extend ActiveSupport::Concern
+
+ class_methods do
+ ##
+ # Is there a job scheduled?
+ def scheduled?
+ delayed_job_query.exists?
+ end
+
+ private
+
+ def delayed_job_query
+ Delayed::Job.where('handler LIKE ?', "%job_class: #{name}%")
+ end
+ end
+end
diff --git a/app/workers/cron/cron_job.rb b/app/workers/cron/cron_job.rb
index b0a70442ddf..d5b455091b9 100644
--- a/app/workers/cron/cron_job.rb
+++ b/app/workers/cron/cron_job.rb
@@ -33,6 +33,8 @@ module Cron
# List of registered jobs, requires eager load in dev(!)
class_attribute :registered_jobs, default: []
+ include ScheduledJob
+
class << self
##
# Register new job class(es)
@@ -66,19 +68,9 @@ module Cron
delayed_job&.destroy
end
- ##
- # Is there a job scheduled?
- def scheduled?
- delayed_job_query.exists?
- end
-
def delayed_job
delayed_job_query.first
end
-
- def delayed_job_query
- Delayed::Job.where('handler LIKE ?', "%job_class: #{name}%")
- end
end
end
end
diff --git a/app/workers/work_packages/apply_working_days_change_job.rb b/app/workers/work_packages/apply_working_days_change_job.rb
index 4af64c4f761..25a28206ad4 100644
--- a/app/workers/work_packages/apply_working_days_change_job.rb
+++ b/app/workers/work_packages/apply_working_days_change_job.rb
@@ -28,6 +28,7 @@
class WorkPackages::ApplyWorkingDaysChangeJob < ApplicationJob
queue_with_priority :above_normal
+ include ::ScheduledJob
def perform(user_id:, previous_working_days:)
user = User.find(user_id)
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 8895dadb1b3..7dcc7dc57c0 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -800,6 +800,7 @@ en:
attributes:
base:
working_days_are_missing: 'At least one working day needs to be specified.'
+ previous_working_day_changes_unprocessed: 'The previous changes to the working days configuration have not been applied yet.'
time_entry:
attributes:
hours:
diff --git a/spec/contracts/settings/working_days_params_contract_spec.rb b/spec/contracts/settings/working_days_params_contract_spec.rb
index 585bcb6466e..de1f68ce56a 100644
--- a/spec/contracts/settings/working_days_params_contract_spec.rb
+++ b/spec/contracts/settings/working_days_params_contract_spec.rb
@@ -37,6 +37,13 @@ describe Settings::WorkingDaysParamsContract do
let(:contract) do
described_class.new(setting, current_user, params:)
end
+ let(:apply_job_scheduled) { false }
+
+ before do
+ allow(WorkPackages::ApplyWorkingDaysChangeJob)
+ .to receive(:scheduled?)
+ .and_return(apply_job_scheduled)
+ end
it_behaves_like 'contract is valid for active admins and invalid for regular users'
@@ -45,4 +52,11 @@ describe Settings::WorkingDaysParamsContract do
include_examples 'contract is invalid', base: :working_days_are_missing
end
+
+ context 'with an ApplyWorkingDaysChangeJob already existing' do
+ let(:params) { { working_days: [1, 2, 3] } }
+ let(:apply_job_scheduled) { true }
+
+ include_examples 'contract is invalid', base: :previous_working_day_changes_unprocessed
+ end
end
diff --git a/spec/features/admin/working_days_spec.rb b/spec/features/admin/working_days_spec.rb
index f60809525e7..b4bf1afd454 100644
--- a/spec/features/admin/working_days_spec.rb
+++ b/spec/features/admin/working_days_spec.rb
@@ -137,4 +137,22 @@ describe 'Working Days', type: :feature, js: true do
expect(work_package.due_date)
.to eql(Date.parse('2022-09-23'))
end
+
+ it 'shows an error when a previous change to the working days configuration isn\'t processed yet' do
+ # Have a job already scheduled
+ # Attempting to set the job via simply using the UI would require to change the test setup of how
+ # delayed jobs are handled.
+ ActiveJob::QueueAdapters::DelayedJobAdapter
+ .new
+ .enqueue(WorkPackages::ApplyWorkingDaysChangeJob.new(user_id: 5, previous_working_days: []))
+
+ uncheck 'Tuesday'
+ click_on 'Save'
+
+ # Not executing the background jobs
+ dialog.confirm
+
+ expect(page).to have_selector('.flash.error',
+ text: 'The previous changes to the working days configuration have not been applied yet.')
+ end
end
diff --git a/spec/workers/work_packages/apply_working_days_change_job_spec.rb b/spec/workers/work_packages/apply_working_days_change_job_spec.rb
index c05cc161655..35eab0348f9 100644
--- a/spec/workers/work_packages/apply_working_days_change_job_spec.rb
+++ b/spec/workers/work_packages/apply_working_days_change_job_spec.rb
@@ -377,4 +377,26 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob do
CHART
end
end
+
+ describe '.scheduled?' do
+ context 'with a job already scheduled in the DB' do
+ before do
+ ActiveJob::QueueAdapters::DelayedJobAdapter
+ .new
+ .enqueue(described_class.new(user_id: 5, previous_working_days: [1, 2]))
+ end
+
+ it 'is true' do
+ expect(described_class)
+ .to be_scheduled
+ end
+ end
+
+ context 'without a job already scheduled in the DB' do
+ it 'is false' do
+ expect(described_class)
+ .not_to be_scheduled
+ end
+ end
+ end
end