Skip to content

Commit 25c5865

Browse files
committed
update template scheduler
1 parent bb90bb3 commit 25c5865

File tree

1 file changed

+51
-39
lines changed

1 file changed

+51
-39
lines changed

enterprise/coderd/templateschedule.go

+51-39
Original file line numberDiff line numberDiff line change
@@ -44,51 +44,63 @@ func (*EnterpriseTemplateScheduleStore) SetTemplateScheduleOptions(ctx context.C
4444
return tpl, nil
4545
}
4646

47-
template, err := db.UpdateTemplateScheduleByID(ctx, database.UpdateTemplateScheduleByIDParams{
48-
ID: tpl.ID,
49-
UpdatedAt: database.Now(),
50-
AllowUserAutostart: opts.UserAutostartEnabled,
51-
AllowUserAutostop: opts.UserAutostopEnabled,
52-
DefaultTTL: int64(opts.DefaultTTL),
53-
MaxTTL: int64(opts.MaxTTL),
54-
FailureTTL: int64(opts.FailureTTL),
55-
InactivityTTL: int64(opts.InactivityTTL),
56-
LockedTTL: int64(opts.LockedTTL),
57-
})
58-
if err != nil {
59-
return database.Template{}, xerrors.Errorf("update template schedule: %w", err)
60-
}
47+
var template database.Template
48+
err := db.InTx(func(db database.Store) error {
49+
err := db.UpdateTemplateScheduleByID(ctx, database.UpdateTemplateScheduleByIDParams{
50+
ID: tpl.ID,
51+
UpdatedAt: database.Now(),
52+
AllowUserAutostart: opts.UserAutostartEnabled,
53+
AllowUserAutostop: opts.UserAutostopEnabled,
54+
DefaultTTL: int64(opts.DefaultTTL),
55+
MaxTTL: int64(opts.MaxTTL),
56+
FailureTTL: int64(opts.FailureTTL),
57+
InactivityTTL: int64(opts.InactivityTTL),
58+
LockedTTL: int64(opts.LockedTTL),
59+
})
60+
if err != nil {
61+
return xerrors.Errorf("update template schedule: %w", err)
62+
}
6163

62-
// Update all workspaces using the template to set the user defined schedule
63-
// to be within the new bounds. This essentially does the following for each
64-
// workspace using the template.
65-
// if (template.ttl != NULL) {
66-
// workspace.ttl = min(workspace.ttl, template.ttl)
67-
// }
68-
//
69-
// NOTE: this does not apply to currently running workspaces as their
70-
// schedule information is committed to the workspace_build during start.
71-
// This limitation is displayed to the user while editing the template.
72-
if opts.MaxTTL > 0 {
73-
err = db.UpdateWorkspaceTTLToBeWithinTemplateMax(ctx, database.UpdateWorkspaceTTLToBeWithinTemplateMaxParams{
74-
TemplateID: template.ID,
75-
TemplateMaxTTL: int64(opts.MaxTTL),
64+
// Update all workspaces using the template to set the user defined schedule
65+
// to be within the new bounds. This essentially does the following for each
66+
// workspace using the template.
67+
// if (template.ttl != NULL) {
68+
// workspace.ttl = min(workspace.ttl, template.ttl)
69+
// }
70+
//
71+
// NOTE: this does not apply to currently running workspaces as their
72+
// schedule information is committed to the workspace_build during start.
73+
// This limitation is displayed to the user while editing the template.
74+
if opts.MaxTTL > 0 {
75+
err = db.UpdateWorkspaceTTLToBeWithinTemplateMax(ctx, database.UpdateWorkspaceTTLToBeWithinTemplateMaxParams{
76+
TemplateID: tpl.ID,
77+
TemplateMaxTTL: int64(opts.MaxTTL),
78+
})
79+
if err != nil {
80+
return xerrors.Errorf("update TTL of all workspaces on template to be within new template max TTL: %w", err)
81+
}
82+
}
83+
84+
// If we updated the locked_ttl we need to update all the workspaces deleting_at
85+
// to ensure workspaces are being cleaned up correctly. Similarly if we are
86+
// disabling it (by passing 0), then we want to delete nullify the deleting_at
87+
// fields of all the template workspaces.
88+
err = db.UpdateWorkspacesDeletingAtByTemplateID(ctx, database.UpdateWorkspacesDeletingAtByTemplateIDParams{
89+
TemplateID: tpl.ID,
90+
LockedTtlMs: opts.LockedTTL.Milliseconds(),
7691
})
7792
if err != nil {
78-
return database.Template{}, xerrors.Errorf("update TTL of all workspaces on template to be within new template max TTL: %w", err)
93+
return xerrors.Errorf("update deleting_at of all workspaces for new locked_ttl %q: %w", opts.LockedTTL, err)
7994
}
80-
}
8195

82-
// If we updated the locked_ttl we need to update all the workspaces deleting_at
83-
// to ensure workspaces are being cleaned up correctly. Similarly if we are
84-
// disabling it (by passing 0), then we want to delete nullify the deleting_at
85-
// fields of all the template workspaces.
86-
err = db.UpdateWorkspacesDeletingAtByTemplateID(ctx, database.UpdateWorkspacesDeletingAtByTemplateIDParams{
87-
TemplateID: template.ID,
88-
LockedTtlMs: opts.LockedTTL.Milliseconds(),
89-
})
96+
template, err = db.GetTemplateByID(ctx, tpl.ID)
97+
if err != nil {
98+
return xerrors.Errorf("get updated template schedule: %w", err)
99+
}
100+
return nil
101+
}, nil)
90102
if err != nil {
91-
return database.Template{}, xerrors.Errorf("update deleting_at of all workspaces for new locked_ttl %q: %w", opts.LockedTTL, err)
103+
return database.Template{}, xerrors.Errorf("in tx: %w", err)
92104
}
93105

94106
return template, nil

0 commit comments

Comments
 (0)