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

Avoid rescheduling twice for follows relations

Christophe Bliard3824f59cc8de
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 685ca081910..ddee93e1ca9 100644
--- a/app/workers/work_packages/apply_working_days_change_job.rb
+++ b/app/workers/work_packages/apply_working_days_change_job.rb
@@ -32,21 +32,28 @@ class WorkPackages::ApplyWorkingDaysChangeJob < ApplicationJob
def perform(user_id:)
user = User.find(user_id)
+ each_applicable_work_package do |work_package|
+ WorkPackages::UpdateService
+ .new(user:, model: work_package, contract_class: EmptyContract)
+ .call(duration: work_package.duration)
+ end
+ end
+
+ private
+
+ def each_applicable_work_package
WorkPackage
.where(ignore_non_working_days: false)
- .order(created_at: :asc)
- .find_each do |work_package|
+ .order(start_date: :asc)
+ .pluck(:id)
+ .each do |id|
+ work_package = WorkPackage.find(id)
next if dates_and_duration_match?(work_package)
- WorkPackages::UpdateService
- .new(user:, model: work_package, contract_class: EmptyContract)
- .call(duration: work_package.duration)
- work_package.save
+ yield work_package
end
end
- private
-
def dates_and_duration_match?(work_package)
days.working?(work_package.start_date) \
&& days.working?(work_package.due_date) \
diff --git a/spec/services/work_packages/set_schedule_service_working_days_spec.rb b/spec/services/work_packages/set_schedule_service_working_days_spec.rb
index 7a1d43b6941..e041f06bb68 100644
--- a/spec/services/work_packages/set_schedule_service_working_days_spec.rb
+++ b/spec/services/work_packages/set_schedule_service_working_days_spec.rb
@@ -1022,6 +1022,77 @@ describe WorkPackages::SetScheduleService, 'working days' do
end
end
+ def set_non_working_week_days(*days)
+ days.each do |day|
+ wday = %w[xxx monday tuesday wednesday thursday friday saturday sunday].index(day.downcase)
+ WeekDay.find_by!(day: wday).update(working: false)
+ end
+ end
+
+ context 'when moving forward due to days and predecessor due date now being non-working days' do
+ let_schedule(<<~CHART)
+ days | MTWTFSS |
+ work_package | XX |
+ follower1 | X | follows work_package
+ follower2 | XX | follows follower1
+ CHART
+
+ before do
+ # Tuesday, Thursday, and Friday are now non-working days. So work_package
+ # was starting on Monday and now is being shifted to Tuesday by the
+ # SetAttributesService.
+ #
+ # Below instructions reproduce the conditions in which such scheduling
+ # must happen.
+ set_non_working_week_days('tuesday', 'thursday', 'friday')
+ change_schedule([work_package], <<~CHART)
+ days | MTWTFSS |
+ work_package | X.X |
+ CHART
+ end
+
+ it 'reschedules all the followers keeping the delay and compacting the extra spaces' do
+ expect(subject.all_results).to match_schedule(<<~CHART)
+ days | MTWTFSSm w m |
+ work_package | X.X |
+ follower1 | X |
+ follower2 | X....X |
+ CHART
+ end
+ end
+
+ context 'when moving forward due to days and predecessor start date now being non-working days' do
+ let_schedule(<<~CHART)
+ days | MTWTFSS |
+ work_package | XX |
+ follower1 | X | follows work_package
+ follower2 | XX | follows follower1
+ CHART
+
+ before do
+ # Monday, Thursday, and Friday are now non-working days. So work_package
+ # was starting on Monday and now is being shifted to Tuesday by the
+ # SetAttributesService.
+ #
+ # Below instructions reproduce the conditions in which such scheduling
+ # must happen.
+ set_non_working_week_days('monday', 'thursday', 'friday')
+ change_schedule([work_package], <<~CHART)
+ days | MTWTFSS |
+ work_package | XX |
+ CHART
+ end
+
+ it 'reschedules all the followers without crossing each other' do
+ expect(subject.all_results).to match_schedule(<<~CHART)
+ days | MTWTFSS tw tw |
+ work_package | XX |
+ follower1 | X |
+ follower2 | X.....X |
+ CHART
+ end
+ end
+
context 'when moving backwards' do
let_schedule(<<~CHART)
days | MTWTFSSm sm sm |
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 2d6cdf9ac30..d19c19df4bd 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
@@ -1,3 +1,31 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-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.
+#++
+
require 'rails_helper'
RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob do
@@ -6,9 +34,11 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob do
shared_let(:user) { create(:user) }
shared_let(:week_days) { create(:week_days_with_saturday_and_sunday_as_weekend) }
- def set_non_working_week_day(day)
- wday = %w[xxx monday tuesday wednesday thursday friday saturday sunday].index(day.downcase)
- WeekDay.find_by(day: wday).update(working: false)
+ def set_non_working_week_days(*days)
+ days.each do |day|
+ wday = %w[xxx monday tuesday wednesday thursday friday saturday sunday].index(day.downcase)
+ WeekDay.find_by!(day: wday).update(working: false)
+ end
end
context 'when a work package includes a date that is now a non-working day' do
@@ -18,7 +48,7 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob do
CHART
before do
- set_non_working_week_day('wednesday')
+ set_non_working_week_days('wednesday')
end
it 'moves the finish date to the corresponding number of now-excluded days to maintain duration [#31992]' do
@@ -37,7 +67,7 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob do
CHART
before do
- set_non_working_week_day('wednesday')
+ set_non_working_week_days('wednesday')
end
it 'does not move any dates' do
@@ -60,7 +90,7 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob do
CHART
before do
- set_non_working_week_day('wednesday')
+ set_non_working_week_days('wednesday')
end
it 'updates all impacted work packages' do
@@ -76,6 +106,32 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob do
end
end
+ context 'when having multiple work packages following each other' do
+ let_schedule(<<~CHART, ignore_non_working_days: false)
+ days | MTWTFSS |
+ wp1 | X..XX | follows wp2
+ wp2 | X | follows wp3
+ wp3 | XXX |
+ CHART
+
+ before do
+ set_non_working_week_days('tuesday', 'wednesday', 'friday')
+ end
+
+ it 'updates them only once' do
+ expect { job.perform_now(user_id: user.id) }
+ .to change { WorkPackage.pluck(:lock_version) }
+ .from([0, 0, 0])
+ .to([1, 1, 1])
+ expect(WorkPackage.all).to match_schedule(<<~CHART)
+ days | MTWTFSSmtwtfssmtwtfss |
+ wp1 | X..X...X |
+ wp2 | X |
+ wp3 | X..X...X |
+ CHART
+ end
+ end
+
context 'when a work package was scheduled to start on a date that is now a non-working day' do
let_schedule(<<~CHART, ignore_non_working_days: false)
days | MTWTFSS |
@@ -83,7 +139,7 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob do
CHART
before do
- set_non_working_week_day('wednesday')
+ set_non_working_week_days('wednesday')
end
it 'moves the start date to the earliest working day in the future, ' \
@@ -104,7 +160,7 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob do
CHART
before do
- set_non_working_week_day('wednesday')
+ set_non_working_week_days('wednesday')
end
it 'moves the follower start date by consequence of the predecessor dates shift [#31992]' do