Skip to content

Commit cd77138

Browse files
committed
steven comments and test fix
1 parent 96f5e2e commit cd77138

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

cli/templateedit.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cli
33
import (
44
"fmt"
55
"net/http"
6+
"strings"
67
"time"
78

89
"golang.org/x/xerrors"
@@ -90,12 +91,6 @@ func (r *RootCmd) templateEdit() *clibase.Cmd {
9091
restartRequirementDaysOfWeek = []string{}
9192
}
9293

93-
// Check that the user didn't specify a value that is not allowed.
94-
_, err = codersdk.WeekdaysToBitmap(restartRequirementDaysOfWeek)
95-
if err != nil {
96-
return xerrors.Errorf("invalid restart requirement days of week: %w", err)
97-
}
98-
9994
// NOTE: coderd will ignore empty fields.
10095
req := codersdk.UpdateTemplateMeta{
10196
Name: name,
@@ -160,7 +155,17 @@ func (r *RootCmd) templateEdit() *clibase.Cmd {
160155
Description: "Edit the template restart requirement weekdays - workspaces created from this template must be restarted on the given weekdays. To unset this value for the template (and disable the restart requirement for the template), pass 'none'.",
161156
// TODO(@dean): unhide when we delete max_ttl
162157
Hidden: true,
163-
Value: clibase.StringArrayOf(&restartRequirementDaysOfWeek),
158+
Value: clibase.Validate(clibase.StringArrayOf(&restartRequirementDaysOfWeek), func(value *clibase.StringArray) error {
159+
v := value.GetSlice()
160+
if len(v) == 1 && v[0] == "none" {
161+
return nil
162+
}
163+
_, err := codersdk.WeekdaysToBitmap(v)
164+
if err != nil {
165+
return xerrors.Errorf("invalid restart requirement days of week %q: %w", strings.Join(v, ","), err)
166+
}
167+
return nil
168+
}),
164169
},
165170
{
166171
Flag: "restart-requirement-weeks",

coderd/schedule/autostop.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,27 @@ const (
1313
// restartRequirementLeeway is the duration of time before a restart
1414
// requirement where we skip the requirement and fall back to the next
1515
// scheduled restart. This avoids workspaces being restarted too soon.
16+
//
17+
// E.g. If the workspace is started within an hour of the quiet hours, we
18+
// will skip the restart requirement and use the next scheduled restart
19+
// requirement.
1620
restartRequirementLeeway = 1 * time.Hour
1721

1822
// restartRequirementBuffer is the duration of time we subtract from the
1923
// time when calculating the next scheduled restart time. This avoids issues
2024
// where autostart happens on the hour and the scheduled quiet hours are
2125
// also on the hour.
26+
//
27+
// E.g. If the workspace is started at 12am (perhaps due to scheduled
28+
// autostart) and the quiet hours is also 12am, the workspace will skip
29+
// the day it's supposed to stop and use the next day instead. This is
30+
// because getting the next cron schedule time will never include the
31+
// time fed to the calculation (i.e. it's not inclusive). This happens
32+
// because we always check for the next cron time by rounding down to
33+
// midnight.
34+
//
35+
// This resolves that problem by subtracting 15 minutes from midnight
36+
// when we check the next cron time.
2237
restartRequirementBuffer = -15 * time.Minute
2338
)
2439

coderd/schedule/autostop_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,11 +539,21 @@ func TestFindWeek(t *testing.T) {
539539
currentWeek, err := schedule.WeeksSinceEpoch(now)
540540
require.NoError(t, err)
541541

542-
currentWeekMondayExpected := now.AddDate(0, 0, -int(now.Weekday())+1)
542+
diffMonday := now.Weekday() - time.Monday
543+
if now.Weekday() == time.Sunday {
544+
// Sunday is 0, but Monday is the first day of the week in the
545+
// code.
546+
diffMonday = 6
547+
}
548+
currentWeekMondayExpected := now.AddDate(0, 0, -int(diffMonday))
549+
require.Equal(t, time.Monday, currentWeekMondayExpected.Weekday())
543550
y, m, d := currentWeekMondayExpected.Date()
551+
// Change to midnight.
544552
currentWeekMondayExpected = time.Date(y, m, d, 0, 0, 0, 0, loc)
553+
545554
currentWeekMonday, err := schedule.GetMondayOfWeek(now.Location(), currentWeek)
546555
require.NoError(t, err)
556+
require.Equal(t, time.Monday, currentWeekMonday.Weekday())
547557
require.Equal(t, currentWeekMondayExpected, currentWeekMonday)
548558

549559
t.Log("now", now)

0 commit comments

Comments
 (0)