Fix issue with under maintenance feature

This commit is contained in:
TwinProduction 2021-10-01 02:33:37 -04:00
parent dfd2f7943f
commit bf455fb7cc
2 changed files with 35 additions and 6 deletions

View File

@ -95,10 +95,15 @@ func (c Config) IsUnderMaintenance() bool {
return false return false
} }
now := time.Now().UTC() now := time.Now().UTC()
dayWhereMaintenancePeriodWouldStart := now.Add(-c.Duration).Truncate(24 * time.Hour) var dayWhereMaintenancePeriodWouldStart time.Time
if now.Hour() >= int(c.durationToStartFromMidnight.Hours()) {
dayWhereMaintenancePeriodWouldStart = now.Truncate(24 * time.Hour)
} else {
dayWhereMaintenancePeriodWouldStart = now.Add(-c.Duration).Truncate(24 * time.Hour)
}
hasMaintenanceEveryDay := len(c.Every) == 0 hasMaintenanceEveryDay := len(c.Every) == 0
hasMaintenancePeriodScheduledForThatWeekday := c.hasDay(dayWhereMaintenancePeriodWouldStart.Weekday().String()) hasMaintenancePeriodScheduledToStartOnThatWeekday := c.hasDay(dayWhereMaintenancePeriodWouldStart.Weekday().String())
if !hasMaintenanceEveryDay && !hasMaintenancePeriodScheduledForThatWeekday { if !hasMaintenanceEveryDay && !hasMaintenancePeriodScheduledToStartOnThatWeekday {
// The day when the maintenance period would start is not scheduled // The day when the maintenance period would start is not scheduled
// to have any maintenance, so we can just return false. // to have any maintenance, so we can just return false.
return false return false

View File

@ -153,7 +153,7 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
expected: true, expected: true,
}, },
{ {
name: "under-maintenance", name: "under-maintenance-starting-now-for-2h",
cfg: &Config{ cfg: &Config{
Start: fmt.Sprintf("%02d:00", now.Hour()), Start: fmt.Sprintf("%02d:00", now.Hour()),
Duration: 2 * time.Hour, Duration: 2 * time.Hour,
@ -161,9 +161,33 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
expected: true, expected: true,
}, },
{ {
name: "not-under-maintenance", name: "under-maintenance-starting-now-for-8h",
cfg: &Config{ cfg: &Config{
Start: fmt.Sprintf("%02d:00", now.Add(-5*time.Hour).Hour()), Start: fmt.Sprintf("%02d:00", now.Hour()),
Duration: 8 * time.Hour,
},
expected: true,
},
{
name: "under-maintenance-starting-4h-ago-for-8h",
cfg: &Config{
Start: fmt.Sprintf("%02d:00", now.Hour()-4),
Duration: 8 * time.Hour,
},
expected: true,
},
{
name: "under-maintenance-starting-4h-ago-for-3h",
cfg: &Config{
Start: fmt.Sprintf("%02d:00", now.Hour()-4),
Duration: 3 * time.Hour,
},
expected: false,
},
{
name: "under-maintenance-starting-5h-ago-for-1h",
cfg: &Config{
Start: fmt.Sprintf("%02d:00", now.Hour()-5),
Duration: time.Hour, Duration: time.Hour,
}, },
expected: false, expected: false,