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
Fix scheduling bug with followers with delay
Christophe Bliardbb0b02e0dbca
diff --git a/app/models/relation.rb b/app/models/relation.rbindex bc4ea4c51b2..2d7cb3b53e0 100644--- a/app/models/relation.rb+++ b/app/models/relation.rb@@ -131,8 +131,10 @@ class Relation < ApplicationRecordenddef successor_soonest_start- if relation_type == TYPE_FOLLOWS && (to.start_date || to.due_date)- (to.due_date || to.start_date) + 1 + (delay || 0)+ if follows? && (to.start_date || to.due_date)+ days = WorkPackages::Shared::Days.for(from)+ relation_start_date = (to.due_date || to.start_date) + 1.day+ days.soonest_working_day(relation_start_date, delay:)endenddiff --git a/app/models/work_package/scheduling_rules.rb b/app/models/work_package/scheduling_rules.rbindex 22ba045b009..957bcc46214 100644--- a/app/models/work_package/scheduling_rules.rb+++ b/app/models/work_package/scheduling_rules.rb@@ -38,7 +38,7 @@ module WorkPackage::SchedulingRules# will not violate the precedes relations (max(finish date, start date) + relation delay)# of this work package or its ancestors# e.g.- # AP(due_date: 2017/07/24)-precedes(delay: 1)-A+ # AP(due_date: 2017/07/25)-precedes(delay: 0)-A# |# parent# |@@ -49,17 +49,16 @@ module WorkPackage::SchedulingRules# CP(due_date: 2017/07/25)-precedes(delay: 2)-C## Then soonest_start for:- # C is 2017/07/27- # B is 2017/07/25- # A is 2017/07/25+ # C is 2017/07/28+ # B is 2017/07/26+ # A is 2017/07/26def soonest_start- # eager load `to` to avoid n+1 on successor_soonest_start+ # eager load `to` and `from` to avoid n+1 on successor_soonest_start@soonest_start ||=Relation.follows_non_manual_ancestors(self)- .includes(:to)- .map(&:successor_soonest_start)- .compact+ .includes(:to, :from)+ .filter_map(&:successor_soonest_start).maxendenddiff --git a/app/services/work_packages/schedule_dependency.rb b/app/services/work_packages/schedule_dependency.rbindex 4c6569c8694..7cbe83bb640 100644--- a/app/services/work_packages/schedule_dependency.rb+++ b/app/services/work_packages/schedule_dependency.rb@@ -151,8 +151,8 @@ class WorkPackages::ScheduleDependency# preload unmoving predecessors, as they influence the computation of Relation#successor_soonest_startknown_work_packages.concat(fetch_unmoving_predecessors)- # rehydrate the predecessors of follows relations- rehydrate_predecessors_of_follows_relations+ # rehydrate the predecessors and followers of follows relations+ rehydrate_follows_relationsend# Returns all the descendants of moved and moving work packages that are not@@ -184,9 +184,13 @@ class WorkPackages::ScheduleDependencyself.known_follows_relations = Relation.follows.where(from_id: known_work_packages.map(&:id))end- # rehydrate the #to member of the preloaded follows relations, to- # prevent triggering additional database requests.- def rehydrate_predecessors_of_follows_relations- known_follows_relations.each { |relation| relation.to = work_package_by_id(relation.to_id) }+ # rehydrate the #to and #from members of the preloaded follows relations, to+ # prevent triggering additional database requests when computing soonest+ # start.+ def rehydrate_follows_relations+ known_follows_relations.each do |relation|+ relation.from = work_package_by_id(relation.from_id)+ relation.to = work_package_by_id(relation.to_id)+ endendenddiff --git a/app/services/work_packages/schedule_dependency/dependency.rb b/app/services/work_packages/schedule_dependency/dependency.rbindex a9581d8b323..f392667286c 100644--- a/app/services/work_packages/schedule_dependency/dependency.rb+++ b/app/services/work_packages/schedule_dependency/dependency.rb@@ -52,10 +52,9 @@ class WorkPackages::ScheduleDependency::Dependencyenddef soonest_start_date- soonest_start = follows_relations+ follows_relations.filter_map(&:successor_soonest_start).max- WorkPackages::Shared::Days.for(work_package).soonest_working_day(soonest_start)enddef start_datediff --git a/app/services/work_packages/shared/all_days.rb b/app/services/work_packages/shared/all_days.rbindex c8b5b4077a1..31d6d033a44 100644--- a/app/services/work_packages/shared/all_days.rb+++ b/app/services/work_packages/shared/all_days.rb@@ -50,8 +50,9 @@ module WorkPackagesstart_date + duration - 1end- def soonest_working_day(date)- date+ def soonest_working_day(date, delay: nil)+ delay ||= 0+ date + delay.days if dateenddef working?(_date)diff --git a/app/services/work_packages/shared/working_days.rb b/app/services/work_packages/shared/working_days.rbindex 60ed7f0adfb..739d6c19c94 100644--- a/app/services/work_packages/shared/working_days.rb+++ b/app/services/work_packages/shared/working_days.rb@@ -61,12 +61,20 @@ module WorkPackagesdue_dateend- def soonest_working_day(date)+ def soonest_working_day(date, delay: nil)return unless date+ delay ||= 0++ while delay > 0+ delay -= 1 if working?(date)+ date += 1+ end+until working?(date)date += 1end+dateenddiff --git a/spec/models/relation_spec.rb b/spec/models/relation_spec.rbindex 56d76d77613..123efc64f2f 100644--- a/spec/models/relation_spec.rb+++ b/spec/models/relation_spec.rb@@ -28,6 +28,8 @@require 'spec_helper'describe Relation, type: :model do+ create_shared_association_defaults_for_work_package_factory+let(:from) { create(:work_package) }let(:to) { create(:work_package) }let(:type) { 'relates' }@@ -127,4 +129,120 @@ describe Relation, type: :model doendendend++ describe '#successor_soonest_start' do+ context 'with a follows relation' do+ let_schedule(<<~CHART)+ days | MTWTFSS |+ main | ] |+ follower | | follows main+ CHART++ it 'returns predecessor due_date + 1' do+ relation = schedule.follows_relation(from: 'follower', to: 'main')+ expect(relation.successor_soonest_start).to eq(schedule.tuesday)+ end+ end++ context 'with a follows relation with predecessor having only start date' do+ let_schedule(<<~CHART)+ days | MTWTFSS |+ main | [ |+ follower | | follows main+ CHART++ it 'returns predecessor start_date + 1' do+ relation = schedule.follows_relation(from: 'follower', to: 'main')+ expect(relation.successor_soonest_start).to eq(schedule.tuesday)+ end+ end++ context 'with a non-follows relation' do+ let_schedule(<<~CHART)+ days | MTWTFSS |+ main | X |+ related | |+ CHART+ let(:relation) { create(:relation, from: main, to: related) }++ it 'returns nil' do+ expect(relation.successor_soonest_start).to be_nil+ end+ end++ context 'with a follows relation with a delay' do+ let_schedule(<<~CHART)+ days | MTWTFSS |+ main | X |+ follower_a | | follows main with delay 0+ follower_b | | follows main with delay 1+ follower_c | | follows main with delay 3+ CHART++ it 'returns predecessor due_date + delay + 1' do+ relation_a = schedule.follows_relation(from: 'follower_a', to: 'main')+ expect(relation_a.successor_soonest_start).to eq(schedule.tuesday)++ relation_b = schedule.follows_relation(from: 'follower_b', to: 'main')+ expect(relation_b.successor_soonest_start).to eq(schedule.wednesday)++ relation_c = schedule.follows_relation(from: 'follower_c', to: 'main')+ expect(relation_c.successor_soonest_start).to eq(schedule.friday)+ end+ end++ context 'with a follows relation with a delay and with non-working days in the delay period' do+ let_schedule(<<~CHART)+ days | MTWTFSSmtw |+ main | X░ ░ ░░ ░ |+ follower_delay0 | ░ ░ ░░ ░ | follows main with delay 0+ follower_delay1 | ░ ░ ░░ ░ | follows main with delay 1+ follower_delay2 | ░ ░ ░░ ░ | follows main with delay 2+ follower_delay3 | ░ ░ ░░ ░ | follows main with delay 3+ CHART++ it 'returns a date such as the number of working days between both work package is equal to the delay' do+ set_work_week('monday', 'wednesday', 'friday')++ relation_delay0 = schedule.follows_relation(from: 'follower_delay0', to: 'main')+ expect(relation_delay0.successor_soonest_start).to eq(schedule.wednesday)++ relation_delay1 = schedule.follows_relation(from: 'follower_delay1', to: 'main')+ expect(relation_delay1.successor_soonest_start).to eq(schedule.friday)++ relation_delay2 = schedule.follows_relation(from: 'follower_delay2', to: 'main')+ expect(relation_delay2.successor_soonest_start).to eq(schedule.monday + 7.days)++ relation_delay3 = schedule.follows_relation(from: 'follower_delay3', to: 'main')+ expect(relation_delay3.successor_soonest_start).to eq(schedule.wednesday + 7.days)+ end+ end++ context 'with a follows relation with a delay, non-working days, and follower ignoring non-working days' do+ let_schedule(<<~CHART)+ days | MTWTFSSmtw |+ main | X░ ░ ░░ ░ |+ follower_delay0 | ░ ░ ░░ ░ | follows main with delay 0, working days include weekends+ follower_delay1 | ░ ░ ░░ ░ | follows main with delay 1, working days include weekends+ follower_delay2 | ░ ░ ░░ ░ | follows main with delay 2, working days include weekends+ follower_delay3 | ░ ░ ░░ ░ | follows main with delay 3, working days include weekends+ CHART++ it 'returns predecessor due_date + delay + 1 (like without non-working days)' do+ set_work_week('monday', 'wednesday', 'friday')++ relation_delay0 = schedule.follows_relation(from: 'follower_delay0', to: 'main')+ expect(relation_delay0.successor_soonest_start).to eq(schedule.tuesday)++ relation_delay1 = schedule.follows_relation(from: 'follower_delay1', to: 'main')+ expect(relation_delay1.successor_soonest_start).to eq(schedule.wednesday)++ relation_delay2 = schedule.follows_relation(from: 'follower_delay2', to: 'main')+ expect(relation_delay2.successor_soonest_start).to eq(schedule.thursday)++ relation_delay3 = schedule.follows_relation(from: 'follower_delay3', to: 'main')+ expect(relation_delay3.successor_soonest_start).to eq(schedule.friday)+ end+ end+ endenddiff --git a/spec/services/work_packages/set_schedule_service_working_days_spec.rb b/spec/services/work_packages/set_schedule_service_working_days_spec.rbindex ef5dd655f92..51dc5dbb26e 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@@ -273,6 +273,29 @@ describe WorkPackages::SetScheduleService, 'working days' doCHARTendend++ context 'with the follower having a delay overlapping non-working days' do+ let_schedule(<<~CHART)+ days | MTWTFSS |+ work_package | X |+ follower | XX | follows work_package with delay 2+ CHART++ before do+ change_schedule([work_package], <<~CHART)+ days | MTWTFSS |+ work_package | X |+ CHART+ end++ it 'reschedules the follower to start after the non-working days and the delay' do+ expect(subject.all_results).to match_schedule(<<~CHART)+ | MTWTFSSmtwt |+ work_package | X |+ follower | XX |+ CHART+ end+ endendcontext 'when predecessor moved backwards' do@@ -1086,9 +1109,9 @@ describe WorkPackages::SetScheduleService, 'working days' doit 'schedules parent to start and end at soonest working start date and the child to start at the parent start' doexpect(subject.all_results).to match_schedule(<<~CHART)- days | MTWTFSS |- work_package | [ |- new_parent | X |+ days | MTWTFSS |+ work_package | [ |+ new_parent | X |CHARTendend@@ -1109,9 +1132,9 @@ describe WorkPackages::SetScheduleService, 'working days' doit 'schedules the moved work package to start at the parent soonest date and sets due date to keep the same duration ' \'and schedules the parent dates to match the child dates' doexpect(subject.all_results).to match_schedule(<<~CHART)- days | MTWTFSS |- work_package | XXXX |- new_parent | XXXX |+ days | MTWTFSS |+ work_package | XXXX |+ new_parent | XXXX |CHARTendenddiff --git a/spec/services/work_packages/shared/all_days_spec.rb b/spec/services/work_packages/shared/all_days_spec.rbindex 0e7ff518bb7..206d3ead50e 100644--- a/spec/services/work_packages/shared/all_days_spec.rb+++ b/spec/services/work_packages/shared/all_days_spec.rb@@ -142,16 +142,33 @@ RSpec.describe WorkPackages::Shared::AllDays doexpect(subject.soonest_working_day(nil)).to be_nilend+ context 'with delay' do+ it 'returns the soonest working day from the given day, after a configurable delay of working days' do+ expect(subject.soonest_working_day(sunday_2022_07_31, delay: nil)).to eq(sunday_2022_07_31)+ expect(subject.soonest_working_day(sunday_2022_07_31, delay: 0)).to eq(sunday_2022_07_31)+ expect(subject.soonest_working_day(sunday_2022_07_31, delay: 1)).to eq(Date.new(2022, 8, 1))+ end+ end+context 'with weekend days (Saturday and Sunday)', :weekend_saturday_sunday doit 'returns the given day' doexpect(subject.soonest_working_day(sunday_2022_07_31)).to eq(sunday_2022_07_31)end++ context 'with delay' do+ include_examples 'soonest working day with delay', date: Date.new(2022, 1, 1), delay: 30, expected: Date.new(2022, 1, 31)+ endendcontext 'with some non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 doit 'returns the given day' doexpect(subject.soonest_working_day(Date.new(2022, 12, 25))).to eq(Date.new(2022, 12, 25))end++ context 'with delay' do+ include_examples 'soonest working day with delay', date: Date.new(2022, 12, 24), delay: 7,+ expected: Date.new(2022, 12, 31)+ endendendenddiff --git a/spec/services/work_packages/shared/shared_examples_days.rb b/spec/services/work_packages/shared/shared_examples_days.rbindex 03fa9680a27..abaa8a64267 100644--- a/spec/services/work_packages/shared/shared_examples_days.rb+++ b/spec/services/work_packages/shared/shared_examples_days.rb@@ -81,3 +81,9 @@ RSpec.shared_examples 'soonest working day' do |date:, expected:|expect(subject.soonest_working_day(date)).to eq(expected)endend++RSpec.shared_examples 'soonest working day with delay' do |date:, delay:, expected:|+ it "soonest_working_day(#{date.to_fs(:wday_date)}, delay: #{delay.inspect}) => #{expected.to_fs(:wday_date)}" do+ expect(subject.soonest_working_day(date, delay:)).to eq(expected)+ end+enddiff --git a/spec/services/work_packages/shared/working_days_spec.rb b/spec/services/work_packages/shared/working_days_spec.rbindex 899332ce07c..676eb090450 100644--- a/spec/services/work_packages/shared/working_days_spec.rb+++ b/spec/services/work_packages/shared/working_days_spec.rb@@ -188,17 +188,49 @@ RSpec.describe WorkPackages::Shared::WorkingDays doexpect(subject.soonest_working_day(nil)).to be_nilend+ context 'with delay' do+ it 'returns the soonest working day from the given day, after a configurable delay of working days' do+ expect(subject.soonest_working_day(sunday_2022_07_31, delay: nil)).to eq(sunday_2022_07_31)+ expect(subject.soonest_working_day(sunday_2022_07_31, delay: 0)).to eq(sunday_2022_07_31)+ expect(subject.soonest_working_day(sunday_2022_07_31, delay: 1)).to eq(monday_2022_08_01)+ end++ it 'works with big delay value like 100_000' do+ # First implementation was recursive and failed with SystemStackError: stack level too deep+ expect { subject.soonest_working_day(sunday_2022_07_31, delay: 100_000) }+ .not_to raise_error+ end+ end+context 'with weekend days (Saturday and Sunday)', :weekend_saturday_sunday doinclude_examples 'soonest working day', date: friday_2022_07_29, expected: friday_2022_07_29include_examples 'soonest working day', date: saturday_2022_07_30, expected: monday_2022_08_01include_examples 'soonest working day', date: sunday_2022_07_31, expected: monday_2022_08_01include_examples 'soonest working day', date: monday_2022_08_01, expected: monday_2022_08_01++ context 'with delay' do+ include_examples 'soonest working day with delay', date: friday_2022_07_29, delay: 0, expected: friday_2022_07_29+ include_examples 'soonest working day with delay', date: saturday_2022_07_30, delay: 0, expected: monday_2022_08_01+ include_examples 'soonest working day with delay', date: sunday_2022_07_31, delay: 0, expected: monday_2022_08_01+ include_examples 'soonest working day with delay', date: monday_2022_08_01, delay: 0, expected: monday_2022_08_01++ include_examples 'soonest working day with delay', date: friday_2022_07_29, delay: 1, expected: monday_2022_08_01+ include_examples 'soonest working day with delay', date: saturday_2022_07_30, delay: 1, expected: Date.new(2022, 8, 2)+ include_examples 'soonest working day with delay', date: sunday_2022_07_31, delay: 1, expected: Date.new(2022, 8, 2)+ include_examples 'soonest working day with delay', date: monday_2022_08_01, delay: 1, expected: Date.new(2022, 8, 2)++ include_examples 'soonest working day with delay', date: friday_2022_07_29, delay: 8, expected: Date.new(2022, 8, 10)+ endendcontext 'with some non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 doinclude_examples 'soonest working day', date: Date.new(2022, 12, 25), expected: Date.new(2022, 12, 26)include_examples 'soonest working day', date: Date.new(2022, 12, 31), expected: Date.new(2022, 12, 31)include_examples 'soonest working day', date: Date.new(2023, 1, 1), expected: Date.new(2023, 1, 2)++ context 'with delay' do+ include_examples 'soonest working day with delay', date: Date.new(2022, 12, 24), delay: 7, expected: Date.new(2023, 1, 2)+ endendcontext 'with no working days', :no_working_days dodiff --git a/spec/support/schedule_helpers/schedule.rb b/spec/support/schedule_helpers/schedule.rbindex 9bcf7566040..651ce6063b9 100644--- a/spec/support/schedule_helpers/schedule.rb+++ b/spec/support/schedule_helpers/schedule.rb@@ -44,6 +44,14 @@ module ScheduleHelpers@follows_relations[from:, to:]end+ def monday+ Date.current.next_occurring(:monday)+ end++ %i[tuesday wednesday thursday friday saturday sunday].each do |day_name|+ define_method(day_name) { monday.next_occurring(day_name) }+ end+privatedef normalize_name(name)