Skip to content

Commit 4420985

Browse files
authored
feat(coderd): activity bump for full TTL instead of 1h (#5732)
1 parent e558a25 commit 4420985

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

coderd/activitybump.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,29 @@ func activityBumpWorkspace(log slog.Logger, db database.Store, workspaceID uuid.
4343
return nil
4444
}
4545

46-
// We sent bumpThreshold slightly under bumpAmount to minimize DB writes.
47-
const (
48-
bumpAmount = time.Hour
49-
bumpThreshold = time.Hour - (time.Minute * 10)
46+
workspace, err := s.GetWorkspaceByID(ctx, workspaceID)
47+
if err != nil {
48+
return xerrors.Errorf("get workspace: %w", err)
49+
}
50+
51+
var (
52+
// We bump by the original TTL to prevent counter-intuitive behavior
53+
// as the TTL wraps. For example, if I set the TTL to 12 hours, sign off
54+
// work at midnight, come back at 10am, I would want another full day
55+
// of uptime. In the prior implementation, the workspace would enter
56+
// a state of always expiring 1 hour in the future
57+
bumpAmount = time.Duration(workspace.Ttl.Int64)
58+
// DB writes are expensive so we only bump when 5% of the deadline
59+
// has elapsed.
60+
bumpEvery = bumpAmount / 20
61+
timeSinceLastBump = bumpAmount - time.Until(build.Deadline)
5062
)
5163

52-
if !build.Deadline.Before(time.Now().Add(bumpThreshold)) {
64+
if timeSinceLastBump < bumpEvery {
65+
return nil
66+
}
67+
68+
if bumpAmount == 0 {
5369
return nil
5470
}
5571

coderd/activitybump_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@ func TestWorkspaceActivityBump(t *testing.T) {
1919

2020
ctx := context.Background()
2121

22+
const ttl = time.Minute
23+
2224
setupActivityTest := func(t *testing.T) (client *codersdk.Client, workspace codersdk.Workspace, assertBumped func(want bool)) {
23-
var ttlMillis int64 = 60 * 1000
25+
ttlMillis := int64(ttl / time.Millisecond)
2426

2527
client = coderdtest.New(t, &coderdtest.Options{
26-
AppHostname: proxyTestSubdomainRaw,
27-
IncludeProvisionerDaemon: true,
28-
AgentStatsRefreshInterval: time.Millisecond * 100,
29-
MetricsCacheRefreshInterval: time.Millisecond * 100,
28+
AppHostname: proxyTestSubdomainRaw,
29+
IncludeProvisionerDaemon: true,
30+
// Agent stats trigger the activity bump, so we want to report
31+
// very frequently in tests.
32+
AgentStatsRefreshInterval: time.Millisecond * 100,
3033
})
3134
user := coderdtest.CreateFirstUser(t, client)
3235

@@ -67,11 +70,11 @@ func TestWorkspaceActivityBump(t *testing.T) {
6770
require.NoError(t, err)
6871
return workspace.LatestBuild.Deadline.Time != firstDeadline
6972
},
70-
testutil.WaitShort, testutil.IntervalFast,
73+
testutil.WaitLong, testutil.IntervalFast,
7174
"deadline %v never updated", firstDeadline,
7275
)
7376

74-
require.WithinDuration(t, database.Now().Add(time.Hour), workspace.LatestBuild.Deadline.Time, 3*time.Second)
77+
require.WithinDuration(t, database.Now().Add(ttl), workspace.LatestBuild.Deadline.Time, 3*time.Second)
7578
}
7679
}
7780

@@ -87,6 +90,8 @@ func TestWorkspaceActivityBump(t *testing.T) {
8790
require.NoError(t, err)
8891
defer conn.Close()
8992

93+
// Must send network traffic after a few seconds to surpass bump threshold.
94+
time.Sleep(time.Second * 3)
9095
sshConn, err := conn.SSHClient(ctx)
9196
require.NoError(t, err)
9297
_ = sshConn.Close()

site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export const Language = {
6262
ttlLabel: "Time until shutdown (hours)",
6363
ttlCausesShutdownHelperText: "Your workspace will shut down",
6464
ttlCausesShutdownAfterStart:
65-
"after its next start. We delay shutdown by an hour whenever we detect activity",
65+
"after its next start. We delay shutdown by this time whenever we detect activity",
6666
ttlCausesNoShutdownHelperText:
6767
"Your workspace will not automatically shut down.",
6868
formTitle: "Workspace schedule",

0 commit comments

Comments
 (0)