Skip to content

Commit e86539d

Browse files
authored
feat: Allow user to cancel workspace jobs (coder#5115)
* Add database column allow_user_cancel_workspace_jobs * Adjust API * site: typesGenerated.ts * Expose template.allow_ in Workspaces API * Fix: site tests * Fix: make fmt/prettier * Fix: enterprise * Database tests * Add CLI tests * Add checkbox * i18n * Logic: block cancelling * Unit tests for conditional cancel * Fix: message * Address PR comment * Address PR comments * Fix: make
1 parent 5fa3fde commit e86539d

23 files changed

+336
-162
lines changed

cli/templateedit.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ import (
1313

1414
func templateEdit() *cobra.Command {
1515
var (
16-
name string
17-
displayName string
18-
description string
19-
icon string
20-
defaultTTL time.Duration
16+
name string
17+
displayName string
18+
description string
19+
icon string
20+
defaultTTL time.Duration
21+
allowUserCancelWorkspaceJobs bool
2122
)
2223

2324
cmd := &cobra.Command{
@@ -40,11 +41,12 @@ func templateEdit() *cobra.Command {
4041

4142
// NOTE: coderd will ignore empty fields.
4243
req := codersdk.UpdateTemplateMeta{
43-
Name: name,
44-
DisplayName: displayName,
45-
Description: description,
46-
Icon: icon,
47-
DefaultTTLMillis: defaultTTL.Milliseconds(),
44+
Name: name,
45+
DisplayName: displayName,
46+
Description: description,
47+
Icon: icon,
48+
DefaultTTLMillis: defaultTTL.Milliseconds(),
49+
AllowUserCancelWorkspaceJobs: allowUserCancelWorkspaceJobs,
4850
}
4951

5052
_, err = client.UpdateTemplateMeta(cmd.Context(), template.ID, req)
@@ -61,6 +63,7 @@ func templateEdit() *cobra.Command {
6163
cmd.Flags().StringVarP(&description, "description", "", "", "Edit the template description")
6264
cmd.Flags().StringVarP(&icon, "icon", "", "", "Edit the template icon path")
6365
cmd.Flags().DurationVarP(&defaultTTL, "default-ttl", "", 0, "Edit the template default time before shutdown - workspaces created from this template to this value.")
66+
cmd.Flags().BoolVarP(&allowUserCancelWorkspaceJobs, "allow-user-cancel-workspace-jobs", "", true, "Allow users to cancel in-progress workspace jobs.")
6467
cliui.AllowSkipPrompt(cmd)
6568

6669
return cmd

cli/templateedit_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cli_test
22

33
import (
44
"context"
5+
"strconv"
56
"testing"
67
"time"
78

@@ -31,6 +32,8 @@ func TestTemplateEdit(t *testing.T) {
3132
desc := "lorem ipsum dolor sit amet et cetera"
3233
icon := "/icons/new-icon.png"
3334
defaultTTL := 12 * time.Hour
35+
allowUserCancelWorkspaceJobs := false
36+
3437
cmdArgs := []string{
3538
"templates",
3639
"edit",
@@ -40,6 +43,7 @@ func TestTemplateEdit(t *testing.T) {
4043
"--description", desc,
4144
"--icon", icon,
4245
"--default-ttl", defaultTTL.String(),
46+
"--allow-user-cancel-workspace-jobs=" + strconv.FormatBool(allowUserCancelWorkspaceJobs),
4347
}
4448
cmd, root := clitest.New(t, cmdArgs...)
4549
clitest.SetupConfig(t, client, root)
@@ -57,6 +61,7 @@ func TestTemplateEdit(t *testing.T) {
5761
assert.Equal(t, desc, updated.Description)
5862
assert.Equal(t, icon, updated.Icon)
5963
assert.Equal(t, defaultTTL.Milliseconds(), updated.DefaultTTLMillis)
64+
assert.Equal(t, allowUserCancelWorkspaceJobs, updated.AllowUserCancelWorkspaceJobs)
6065
})
6166
t.Run("FirstEmptyThenNotModified", func(t *testing.T) {
6267
t.Parallel()
@@ -75,6 +80,7 @@ func TestTemplateEdit(t *testing.T) {
7580
"--description", template.Description,
7681
"--icon", template.Icon,
7782
"--default-ttl", (time.Duration(template.DefaultTTLMillis) * time.Millisecond).String(),
83+
"--allow-user-cancel-workspace-jobs=" + strconv.FormatBool(template.AllowUserCancelWorkspaceJobs),
7884
}
7985
cmd, root := clitest.New(t, cmdArgs...)
8086
clitest.SetupConfig(t, client, root)
@@ -91,6 +97,7 @@ func TestTemplateEdit(t *testing.T) {
9197
assert.Equal(t, template.Description, updated.Description)
9298
assert.Equal(t, template.Icon, updated.Icon)
9399
assert.Equal(t, template.DefaultTTLMillis, updated.DefaultTTLMillis)
100+
assert.Equal(t, template.AllowUserCancelWorkspaceJobs, updated.AllowUserCancelWorkspaceJobs)
94101
})
95102
t.Run("InvalidDisplayName", func(t *testing.T) {
96103
t.Parallel()

coderd/database/dump.sql

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE templates DROP COLUMN allow_user_cancel_workspace_jobs;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ALTER TABLE templates ADD COLUMN allow_user_cancel_workspace_jobs boolean NOT NULL DEFAULT true;
2+
3+
COMMENT ON COLUMN templates.allow_user_cancel_workspace_jobs
4+
IS 'Allow users to cancel in-progress workspace jobs.';

coderd/database/models.go

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

coderd/database/queries.sql.go

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

coderd/database/queries/templates.sql

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ INSERT INTO
7070
icon,
7171
user_acl,
7272
group_acl,
73-
display_name
73+
display_name,
74+
allow_user_cancel_workspace_jobs
7475
)
7576
VALUES
76-
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) RETURNING *;
77+
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15) RETURNING *;
7778

7879
-- name: UpdateTemplateActiveVersionByID :exec
7980
UPDATE
@@ -102,7 +103,8 @@ SET
102103
default_ttl = $4,
103104
name = $5,
104105
icon = $6,
105-
display_name = $7
106+
display_name = $7,
107+
allow_user_cancel_workspace_jobs = $8
106108
WHERE
107109
id = $1
108110
RETURNING

0 commit comments

Comments
 (0)