Skip to content

Commit 52f68e6

Browse files
committed
The point at which I realized I'm doing this wrong
1 parent c43c2ff commit 52f68e6

File tree

1 file changed

+57
-46
lines changed

1 file changed

+57
-46
lines changed

coderd/httpmw/workspaceactivitybump.go

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26,62 +26,73 @@ func ActivityBumpWorkspace(log slog.Logger, db database.Store) func(h http.Handl
2626
// We run the bump logic asynchronously since the result doesn't
2727
// affect the response.
2828
go func() {
29-
// We cannot use the Request context since the goroutine
30-
// may be around after the request terminates.
31-
// We set a short timeout so if the app is under load, these
32-
// low priority operations fail first.
33-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
34-
defer cancel()
29+
bump := func() {
30+
// We cannot use the Request context since the goroutine
31+
// may be around after the request terminates.
32+
// We set a short timeout so if the app is under load, these
33+
// low priority operations fail first.
34+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
35+
defer cancel()
3536

36-
err := db.InTx(func(s database.Store) error {
37-
build, err := s.GetLatestWorkspaceBuildByWorkspaceID(ctx, workspace.ID)
38-
log.Debug(ctx, "build", slog.F("build", build))
39-
if errors.Is(err, sql.ErrNoRows) {
40-
return nil
41-
} else if err != nil {
42-
return xerrors.Errorf("get latest workspace build: %w", err)
43-
}
37+
err := db.InTx(func(s database.Store) error {
38+
build, err := s.GetLatestWorkspaceBuildByWorkspaceID(ctx, workspace.ID)
39+
log.Debug(ctx, "build", slog.F("build", build))
40+
if errors.Is(err, sql.ErrNoRows) {
41+
return nil
42+
} else if err != nil {
43+
return xerrors.Errorf("get latest workspace build: %w", err)
44+
}
4445

45-
job, err := s.GetProvisionerJobByID(ctx, build.JobID)
46-
if err != nil {
47-
return xerrors.Errorf("get provisioner job: %w", err)
48-
}
46+
job, err := s.GetProvisionerJobByID(ctx, build.JobID)
47+
if err != nil {
48+
return xerrors.Errorf("get provisioner job: %w", err)
49+
}
4950

50-
if build.Transition != database.WorkspaceTransitionStart || !job.CompletedAt.Valid {
51-
return nil
52-
}
51+
if build.Transition != database.WorkspaceTransitionStart || !job.CompletedAt.Valid {
52+
return nil
53+
}
5354

54-
if build.Deadline.IsZero() {
55-
// Workspace shutdown is manual
56-
return nil
57-
}
55+
if build.Deadline.IsZero() {
56+
// Workspace shutdown is manual
57+
return nil
58+
}
5859

59-
// We sent bumpThreshold slightly under bumpAmount to minimize DB writes.
60-
const (
61-
bumpAmount = time.Hour
62-
bumpThreshold = time.Hour - (time.Minute * 10)
63-
)
60+
// We sent bumpThreshold slightly under bumpAmount to minimize DB writes.
61+
const (
62+
bumpAmount = time.Hour
63+
bumpThreshold = time.Hour - (time.Minute * 10)
64+
)
6465

65-
if !build.Deadline.Before(time.Now().Add(bumpThreshold)) {
66-
return nil
67-
}
66+
if !build.Deadline.Before(time.Now().Add(bumpThreshold)) {
67+
return nil
68+
}
6869

69-
newDeadline := time.Now().Add(bumpAmount)
70+
newDeadline := time.Now().Add(bumpAmount)
7071

71-
if err := s.UpdateWorkspaceBuildByID(ctx, database.UpdateWorkspaceBuildByIDParams{
72-
ID: build.ID,
73-
UpdatedAt: build.UpdatedAt,
74-
ProvisionerState: build.ProvisionerState,
75-
Deadline: newDeadline,
76-
}); err != nil {
77-
return xerrors.Errorf("update workspace build: %w", err)
78-
}
79-
return nil
80-
})
72+
if err := s.UpdateWorkspaceBuildByID(ctx, database.UpdateWorkspaceBuildByIDParams{
73+
ID: build.ID,
74+
UpdatedAt: build.UpdatedAt,
75+
ProvisionerState: build.ProvisionerState,
76+
Deadline: newDeadline,
77+
}); err != nil {
78+
return xerrors.Errorf("update workspace build: %w", err)
79+
}
80+
return nil
81+
})
8182

82-
if err != nil {
83-
log.Error(ctx, "bump failed", slog.Error(err))
83+
if err != nil {
84+
log.Error(ctx, "bump failed", slog.Error(err))
85+
} else {
86+
log.Debug(
87+
ctx, "bumped deadline from activity",
88+
slog.F("workspace_id", workspace.ID),
89+
)
90+
}
8491
}
92+
// For long running connections (e.g. web terminal), we need
93+
// to bump periodically
94+
// ticker := time.NewTicker(time.Minute)
95+
bump()
8596
}()
8697
next.ServeHTTP(w, r)
8798
})

0 commit comments

Comments
 (0)