Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a1fec34

Browse files
committed
chore: codersdk: refactor time.Duration to millisecond timestamp
1 parent 51c420c commit a1fec34

15 files changed

+141
-106
lines changed

cli/autostart.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ func autostartShow() *cobra.Command {
5151
return err
5252
}
5353

54-
if workspace.AutostartSchedule == "" {
54+
if workspace.AutostartSchedule == nil || *workspace.AutostartSchedule == "" {
5555
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "not enabled\n")
5656
return nil
5757
}
5858

59-
validSchedule, err := schedule.Weekly(workspace.AutostartSchedule)
59+
validSchedule, err := schedule.Weekly(*workspace.AutostartSchedule)
6060
if err != nil {
6161
// This should never happen.
62-
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "invalid autostart schedule %q for workspace %s: %s\n", workspace.AutostartSchedule, workspace.Name, err.Error())
62+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "invalid autostart schedule %q for workspace %s: %s\n", *workspace.AutostartSchedule, workspace.Name, err.Error())
6363
return nil
6464
}
6565

@@ -110,7 +110,7 @@ func autostartEnable() *cobra.Command {
110110
}
111111

112112
err = client.UpdateWorkspaceAutostart(cmd.Context(), workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
113-
Schedule: validSchedule.String(),
113+
Schedule: &spec,
114114
})
115115
if err != nil {
116116
return err
@@ -153,7 +153,7 @@ func autostartDisable() *cobra.Command {
153153
}
154154

155155
err = client.UpdateWorkspaceAutostart(cmd.Context(), workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
156-
Schedule: "",
156+
Schedule: nil,
157157
})
158158
if err != nil {
159159
return err

cli/autostart_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestAutostart(t *testing.T) {
3434
)
3535

3636
err := client.UpdateWorkspaceAutostart(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
37-
Schedule: sched,
37+
Schedule: ptr(sched),
3838
})
3939
require.NoError(t, err)
4040

@@ -76,7 +76,7 @@ func TestAutostart(t *testing.T) {
7676
// Ensure autostart schedule updated
7777
updated, err := client.Workspace(ctx, workspace.ID)
7878
require.NoError(t, err, "fetch updated workspace")
79-
require.Equal(t, sched, updated.AutostartSchedule, "expected autostart schedule to be set")
79+
require.Equal(t, sched, *updated.AutostartSchedule, "expected autostart schedule to be set")
8080

8181
// Disable schedule
8282
cmd, root = clitest.New(t, "autostart", "disable", workspace.Name)
@@ -90,7 +90,7 @@ func TestAutostart(t *testing.T) {
9090
// Ensure autostart schedule updated
9191
updated, err = client.Workspace(ctx, workspace.ID)
9292
require.NoError(t, err, "fetch updated workspace")
93-
require.Empty(t, updated.AutostartSchedule, "expected autostart schedule to not be set")
93+
require.Nil(t, updated.AutostartSchedule, "expected autostart schedule to not be set")
9494
})
9595

9696
t.Run("Enable_NotFound", func(t *testing.T) {
@@ -155,6 +155,6 @@ func TestAutostart(t *testing.T) {
155155
// Ensure nothing happened
156156
updated, err := client.Workspace(ctx, workspace.ID)
157157
require.NoError(t, err, "fetch updated workspace")
158-
require.Equal(t, expectedSchedule, updated.AutostartSchedule, "expected default autostart schedule")
158+
require.Equal(t, expectedSchedule, *updated.AutostartSchedule, "expected default autostart schedule")
159159
})
160160
}

cli/bump_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ func TestBump(t *testing.T) {
4040
expectedDeadline := workspace.LatestBuild.Deadline.Add(90 * time.Minute)
4141

4242
// Assert test invariant: workspace build has a deadline set equal to now plus ttl
43-
require.WithinDuration(t, workspace.LatestBuild.Deadline, time.Now().Add(*workspace.TTL), time.Minute)
44-
require.NoError(t, err)
43+
initDeadline := time.Now().Add(time.Duration(*workspace.TTLMillis) * time.Millisecond)
44+
require.WithinDuration(t, initDeadline, workspace.LatestBuild.Deadline, time.Minute)
4545

4646
cmd, root := clitest.New(t, cmdArgs...)
4747
clitest.SetupConfig(t, client, root)
@@ -81,8 +81,8 @@ func TestBump(t *testing.T) {
8181
expectedDeadline := workspace.LatestBuild.Deadline.Add(30 * time.Minute)
8282

8383
// Assert test invariant: workspace build has a deadline set equal to now plus ttl
84-
require.WithinDuration(t, workspace.LatestBuild.Deadline, time.Now().Add(*workspace.TTL), time.Minute)
85-
require.NoError(t, err)
84+
initDeadline := time.Now().Add(time.Duration(*workspace.TTLMillis) * time.Millisecond)
85+
require.WithinDuration(t, initDeadline, workspace.LatestBuild.Deadline, time.Minute)
8686

8787
cmd, root := clitest.New(t, cmdArgs...)
8888
clitest.SetupConfig(t, client, root)
@@ -121,8 +121,8 @@ func TestBump(t *testing.T) {
121121
require.NoError(t, err)
122122

123123
// Assert test invariant: workspace build has a deadline set equal to now plus ttl
124-
require.WithinDuration(t, workspace.LatestBuild.Deadline, time.Now().Add(*workspace.TTL), time.Minute)
125-
require.NoError(t, err)
124+
initDeadline := time.Now().Add(time.Duration(*workspace.TTLMillis) * time.Millisecond)
125+
require.WithinDuration(t, initDeadline, workspace.LatestBuild.Deadline, time.Minute)
126126

127127
cmd, root := clitest.New(t, cmdArgs...)
128128
clitest.SetupConfig(t, client, root)
@@ -147,7 +147,7 @@ func TestBump(t *testing.T) {
147147
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
148148
project = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
149149
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, project.ID, func(cwr *codersdk.CreateWorkspaceRequest) {
150-
cwr.TTL = nil
150+
cwr.TTLMillis = nil
151151
})
152152
cmdArgs = []string{"bump", workspace.Name}
153153
stdoutBuf = &bytes.Buffer{}
@@ -199,8 +199,8 @@ func TestBump(t *testing.T) {
199199
require.NoError(t, err)
200200

201201
// Assert test invariant: workspace build has a deadline set equal to now plus ttl
202-
require.WithinDuration(t, workspace.LatestBuild.Deadline, time.Now().Add(*workspace.TTL), time.Minute)
203-
require.NoError(t, err)
202+
initDeadline := time.Now().Add(time.Duration(*workspace.TTLMillis) * time.Millisecond)
203+
require.WithinDuration(t, initDeadline, workspace.LatestBuild.Deadline, time.Minute)
204204

205205
cmd, root := clitest.New(t, cmdArgs...)
206206
clitest.SetupConfig(t, client, root)

cli/create.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,12 @@ func create() *cobra.Command {
222222
return err
223223
}
224224

225+
ttlMillis := ttl.Milliseconds()
225226
workspace, err := client.CreateWorkspace(cmd.Context(), organization.ID, codersdk.CreateWorkspaceRequest{
226227
TemplateID: template.ID,
227228
Name: workspaceName,
228229
AutostartSchedule: &schedSpec,
229-
TTL: &ttl,
230+
TTLMillis: &ttlMillis,
230231
ParameterValues: parameters,
231232
})
232233
if err != nil {

cli/list.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,16 @@ func list() *cobra.Command {
8787

8888
duration := time.Now().UTC().Sub(workspace.LatestBuild.Job.CreatedAt).Truncate(time.Second)
8989
autostartDisplay := "-"
90-
if workspace.AutostartSchedule != "" {
91-
if sched, err := schedule.Weekly(workspace.AutostartSchedule); err == nil {
90+
if workspace.AutostartSchedule != nil && *workspace.AutostartSchedule != "" {
91+
if sched, err := schedule.Weekly(*workspace.AutostartSchedule); err == nil {
9292
autostartDisplay = sched.Cron()
9393
}
9494
}
9595

9696
autostopDisplay := "-"
97-
if workspace.TTL != nil {
98-
autostopDisplay = durationDisplay(*workspace.TTL)
97+
if workspace.TTLMillis != nil && *workspace.TTLMillis > 0 {
98+
dur := time.Duration(*workspace.TTLMillis) * time.Millisecond
99+
autostopDisplay = durationDisplay(dur)
99100
if has, ext := hasExtension(workspace); has {
100101
autostopDisplay += fmt.Sprintf(" (+%s)", durationDisplay(ext.Round(time.Minute)))
101102
}
@@ -128,10 +129,11 @@ func hasExtension(ws codersdk.Workspace) (bool, time.Duration) {
128129
if ws.LatestBuild.Deadline.IsZero() {
129130
return false, 0
130131
}
131-
if ws.TTL == nil {
132+
if ws.TTLMillis == nil {
132133
return false, 0
133134
}
134-
delta := ws.LatestBuild.Deadline.Add(-*ws.TTL).Sub(ws.LatestBuild.CreatedAt)
135+
ttl := time.Duration(*ws.TTLMillis) * time.Millisecond
136+
delta := ws.LatestBuild.Deadline.Add(-ttl).Sub(ws.LatestBuild.CreatedAt)
135137
if delta < time.Minute {
136138
return false, 0
137139
}

cli/root_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ func TestRoot(t *testing.T) {
2020
require.Contains(t, errStr, "Run 'coder delete --help' for usage.")
2121
})
2222
}
23+
24+
func ptr[T any](v T) *T {
25+
return &v
26+
}

cli/ssh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ func notifyCondition(ctx context.Context, client *codersdk.Client, workspaceID u
290290
return time.Time{}, nil
291291
}
292292

293-
if ws.TTL == nil || *ws.TTL == 0 {
293+
if ws.TTLMillis == nil || *ws.TTLMillis == 0 {
294294
return time.Time{}, nil
295295
}
296296

cli/ttl.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,13 @@ func ttlShow() *cobra.Command {
4949
return xerrors.Errorf("get workspace: %w", err)
5050
}
5151

52-
if workspace.TTL == nil {
52+
if workspace.TTLMillis == nil || *workspace.TTLMillis == 0 {
5353
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "not set\n")
5454
return nil
5555
}
5656

57-
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\n", workspace.TTL)
57+
dur := time.Duration(*workspace.TTLMillis) * time.Millisecond
58+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\n", dur)
5859

5960
return nil
6061
},
@@ -96,10 +97,10 @@ func ttlset() *cobra.Command {
9697
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "warning: ttl rounded down to %s\n", truncated)
9798
}
9899

99-
err = client.UpdateWorkspaceTTL(cmd.Context(), workspace.ID, codersdk.UpdateWorkspaceTTLRequest{
100-
TTL: &truncated,
101-
})
102-
if err != nil {
100+
millis := truncated.Milliseconds()
101+
if err = client.UpdateWorkspaceTTL(cmd.Context(), workspace.ID, codersdk.UpdateWorkspaceTTLRequest{
102+
TTLMillis: &millis,
103+
}); err != nil {
103104
return xerrors.Errorf("update workspace ttl: %w", err)
104105
}
105106

@@ -130,7 +131,7 @@ func ttlunset() *cobra.Command {
130131
}
131132

132133
err = client.UpdateWorkspaceTTL(cmd.Context(), workspace.ID, codersdk.UpdateWorkspaceTTLRequest{
133-
TTL: nil,
134+
TTLMillis: nil,
134135
})
135136
if err != nil {
136137
return xerrors.Errorf("update workspace ttl: %w", err)

cli/ttl_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestTTL(t *testing.T) {
3434
)
3535

3636
err := client.UpdateWorkspaceTTL(ctx, workspace.ID, codersdk.UpdateWorkspaceTTLRequest{
37-
TTL: &ttl,
37+
TTLMillis: ptr(ttl.Milliseconds()),
3838
})
3939
require.NoError(t, err)
4040

@@ -73,7 +73,7 @@ func TestTTL(t *testing.T) {
7373
// Ensure ttl updated
7474
updated, err := client.Workspace(ctx, workspace.ID)
7575
require.NoError(t, err, "fetch updated workspace")
76-
require.Equal(t, ttl.Truncate(time.Minute), *updated.TTL)
76+
require.Equal(t, ttl.Truncate(time.Minute), time.Duration(*updated.TTLMillis)*time.Millisecond)
7777
require.Contains(t, stdoutBuf.String(), "warning: ttl rounded down")
7878

7979
// unset schedule
@@ -87,7 +87,7 @@ func TestTTL(t *testing.T) {
8787
// Ensure ttl updated
8888
updated, err = client.Workspace(ctx, workspace.ID)
8989
require.NoError(t, err, "fetch updated workspace")
90-
require.Nil(t, updated.TTL, "expected ttl to not be set")
90+
require.Nil(t, updated.TTLMillis, "expected ttl to not be set")
9191
})
9292

9393
t.Run("ZeroInvalid", func(t *testing.T) {
@@ -116,7 +116,7 @@ func TestTTL(t *testing.T) {
116116
// Ensure ttl updated
117117
updated, err := client.Workspace(ctx, workspace.ID)
118118
require.NoError(t, err, "fetch updated workspace")
119-
require.Equal(t, ttl.Truncate(time.Minute), *updated.TTL)
119+
require.Equal(t, ttl.Truncate(time.Minute), time.Duration(*updated.TTLMillis)*time.Millisecond)
120120
require.Contains(t, stdoutBuf.String(), "warning: ttl rounded down")
121121

122122
// A TTL of zero is not considered valid.
@@ -131,7 +131,7 @@ func TestTTL(t *testing.T) {
131131
// Ensure ttl remains as before
132132
updated, err = client.Workspace(ctx, workspace.ID)
133133
require.NoError(t, err, "fetch updated workspace")
134-
require.Equal(t, ttl.Truncate(time.Minute), *updated.TTL)
134+
require.Equal(t, ttl.Truncate(time.Minute), time.Duration(*updated.TTLMillis)*time.Millisecond)
135135
})
136136

137137
t.Run("Set_NotFound", func(t *testing.T) {

coderd/autobuild/executor/lifecycle_executor_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestExecutorAutostartOK(t *testing.T) {
4444
sched, err := schedule.Weekly("* * * * *")
4545
require.NoError(t, err)
4646
require.NoError(t, client.UpdateWorkspaceAutostart(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
47-
Schedule: sched.String(),
47+
Schedule: ptr(sched.String()),
4848
}))
4949

5050
// When: the autobuild executor ticks
@@ -95,7 +95,7 @@ func TestExecutorAutostartTemplateUpdated(t *testing.T) {
9595
sched, err := schedule.Weekly("* * * * *")
9696
require.NoError(t, err)
9797
require.NoError(t, client.UpdateWorkspaceAutostart(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
98-
Schedule: sched.String(),
98+
Schedule: ptr(sched.String()),
9999
}))
100100

101101
// When: the autobuild executor ticks
@@ -138,7 +138,7 @@ func TestExecutorAutostartAlreadyRunning(t *testing.T) {
138138
sched, err := schedule.Weekly("* * * * *")
139139
require.NoError(t, err)
140140
require.NoError(t, client.UpdateWorkspaceAutostart(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
141-
Schedule: sched.String(),
141+
Schedule: ptr(sched.String()),
142142
}))
143143

144144
// When: the autobuild executor ticks
@@ -316,12 +316,12 @@ func TestExecutorAutostopNotEnabled(t *testing.T) {
316316
})
317317
// Given: we have a user with a workspace that has no TTL set
318318
workspace = mustProvisionWorkspace(t, client, func(cwr *codersdk.CreateWorkspaceRequest) {
319-
cwr.TTL = nil
319+
cwr.TTLMillis = nil
320320
})
321321
)
322322

323323
// Given: workspace has no TTL set
324-
require.Nil(t, workspace.TTL)
324+
require.Nil(t, workspace.TTLMillis)
325325

326326
// Given: workspace is running
327327
require.Equal(t, codersdk.WorkspaceTransitionStart, workspace.LatestBuild.Transition)
@@ -359,7 +359,7 @@ func TestExecutorWorkspaceDeleted(t *testing.T) {
359359
sched, err := schedule.Weekly("* * * * *")
360360
require.NoError(t, err)
361361
require.NoError(t, client.UpdateWorkspaceAutostart(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
362-
Schedule: sched.String(),
362+
Schedule: ptr(sched.String()),
363363
}))
364364

365365
// Given: workspace is deleted
@@ -402,7 +402,7 @@ func TestExecutorWorkspaceAutostartTooEarly(t *testing.T) {
402402
sched, err := schedule.Weekly(futureTimeCron)
403403
require.NoError(t, err)
404404
require.NoError(t, client.UpdateWorkspaceAutostart(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
405-
Schedule: sched.String(),
405+
Schedule: ptr(sched.String()),
406406
}))
407407

408408
// When: the autobuild executor ticks
@@ -461,7 +461,7 @@ func TestExecutorWorkspaceAutostopNoWaitChangedMyMind(t *testing.T) {
461461
)
462462

463463
// Given: the user changes their mind and decides their workspace should not auto-stop
464-
err := client.UpdateWorkspaceTTL(ctx, workspace.ID, codersdk.UpdateWorkspaceTTLRequest{TTL: nil})
464+
err := client.UpdateWorkspaceTTL(ctx, workspace.ID, codersdk.UpdateWorkspaceTTLRequest{TTLMillis: nil})
465465
require.NoError(t, err)
466466

467467
// When: the autobuild executor ticks after the deadline
@@ -572,6 +572,10 @@ func mustWorkspace(t *testing.T, client *codersdk.Client, workspaceID uuid.UUID)
572572
return ws
573573
}
574574

575+
func ptr[T any](v T) *T {
576+
return &v
577+
}
578+
575579
func TestMain(m *testing.M) {
576580
goleak.VerifyTestMain(m)
577581
}

coderd/coderdtest/coderdtest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ func CreateWorkspace(t *testing.T, client *codersdk.Client, organization uuid.UU
400400
TemplateID: templateID,
401401
Name: randomUsername(),
402402
AutostartSchedule: ptr("CRON_TZ=US/Central * * * * *"),
403-
TTL: ptr(8 * time.Hour),
403+
TTLMillis: ptr((8 * time.Hour).Milliseconds()),
404404
}
405405
for _, mutator := range mutators {
406406
mutator(&req)

0 commit comments

Comments
 (0)