@@ -44,51 +44,63 @@ func (*EnterpriseTemplateScheduleStore) SetTemplateScheduleOptions(ctx context.C
44
44
return tpl , nil
45
45
}
46
46
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
+ }
61
63
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 (),
76
91
})
77
92
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 )
79
94
}
80
- }
81
95
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 )
90
102
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 )
92
104
}
93
105
94
106
return template , nil
0 commit comments