Skip to content

Commit 0d09d4c

Browse files
committed
feat: template controls which days can autostart
1 parent 7c66878 commit 0d09d4c

17 files changed

+349
-158
lines changed

coderd/autobuild/lifecycle_executor.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,17 @@ func isEligibleForAutostart(ws database.Workspace, build database.WorkspaceBuild
354354
// Truncate is probably not necessary here, but doing it anyway to be sure.
355355
nextTransition := sched.Next(build.CreatedAt).Truncate(time.Minute)
356356

357-
return !currentTick.Before(nextTransition)
357+
// The nextTransition is when the auto start should kick off. If it lands on a
358+
// forbidden day, do not allow the auto start. We use the time location of the
359+
// schedule to determine the weekday. So if "Saturday" is disallowed, the
360+
// definition of "Saturday" depends on the location of the schedule.
361+
zonedTransition := nextTransition.In(sched.Location())
362+
allowed := templateSchedule.AutostartRequirement.DaysMap()[zonedTransition.Weekday()]
363+
if !allowed {
364+
return false
365+
}
366+
367+
return currentTick.After(nextTransition)
358368
}
359369

360370
// isEligibleForAutostart returns true if the workspace should be autostopped.

coderd/database/dbfake/dbfake.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5700,6 +5700,7 @@ func (q *FakeQuerier) UpdateTemplateScheduleByID(_ context.Context, arg database
57005700
tpl.MaxTTL = arg.MaxTTL
57015701
tpl.AutostopRequirementDaysOfWeek = arg.AutostopRequirementDaysOfWeek
57025702
tpl.AutostopRequirementWeeks = arg.AutostopRequirementWeeks
5703+
tpl.AutostartBlockDaysOfWeek = arg.AutostartBlockDaysOfWeek
57035704
tpl.FailureTTL = arg.FailureTTL
57045705
tpl.TimeTilDormant = arg.TimeTilDormant
57055706
tpl.TimeTilDormantAutoDelete = arg.TimeTilDormantAutoDelete

coderd/database/dump.sql

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
BEGIN;
2+
3+
DROP VIEW template_with_users;
4+
5+
ALTER TABLE templates
6+
DROP COLUMN autostart_block_days_of_week;
7+
8+
-- Recreate view
9+
CREATE VIEW
10+
template_with_users
11+
AS
12+
SELECT
13+
templates.*,
14+
coalesce(visible_users.avatar_url, '') AS created_by_avatar_url,
15+
coalesce(visible_users.username, '') AS created_by_username
16+
FROM
17+
templates
18+
LEFT JOIN
19+
visible_users
20+
ON
21+
templates.created_by = visible_users.id;
22+
23+
COMMENT ON VIEW template_with_users IS 'Joins in the username + avatar url of the created by user.';
24+
25+
COMMIT;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
BEGIN;
2+
3+
DROP VIEW template_with_users;
4+
5+
ALTER TABLE templates
6+
ADD COLUMN autostart_block_days_of_week smallint NOT NULL DEFAULT 0;
7+
8+
COMMENT ON COLUMN templates.autostart_block_days_of_week IS 'A bitmap of days of week that autostart of a workspace is not allowed. Default allows all days. This is intended as a cost savings measure to prevent auto start on weekends (for example).';
9+
10+
-- Recreate view
11+
CREATE VIEW
12+
template_with_users
13+
AS
14+
SELECT
15+
templates.*,
16+
coalesce(visible_users.avatar_url, '') AS created_by_avatar_url,
17+
coalesce(visible_users.username, '') AS created_by_username
18+
FROM
19+
templates
20+
LEFT JOIN
21+
visible_users
22+
ON
23+
templates.created_by = visible_users.id;
24+
25+
COMMENT ON VIEW template_with_users IS 'Joins in the username + avatar url of the created by user.';
26+
27+
COMMIT;

coderd/database/modelmethods.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ func (t Template) DeepCopy() Template {
124124
return cpy
125125
}
126126

127+
// AutostartAllowedDays returns the inverse of 'AutostartBlockDaysOfWeek'.
128+
// It is more useful to have the days that are allowed to autostart from a UX
129+
// POV. The database prefers the 0 value being 'all days allowed'.
130+
func (t Template) AutostartAllowedDays() uint8 {
131+
// Just flip the binary 0s to 1s and vice versa.
132+
// There is an extra day with the 8th bit that needs to be zeroed.
133+
return ^uint8(t.AutostartBlockDaysOfWeek) & 0b01111111
134+
}
135+
127136
func (TemplateVersion) RBACObject(template Template) rbac.Object {
128137
// Just use the parent template resource for controlling versions
129138
return template.RBACObject()

coderd/database/modelqueries.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func (q *sqlQuerier) GetAuthorizedTemplates(ctx context.Context, arg GetTemplate
8585
&i.TimeTilDormantAutoDelete,
8686
&i.AutostopRequirementDaysOfWeek,
8787
&i.AutostopRequirementWeeks,
88+
&i.AutostartBlockDaysOfWeek,
8889
&i.CreatedByAvatarURL,
8990
&i.CreatedByUsername,
9091
); err != nil {

coderd/database/models.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)