Skip to content

Commit 1829a25

Browse files
committed
fix bugged query
1 parent 3e583d9 commit 1829a25

File tree

6 files changed

+23
-84
lines changed

6 files changed

+23
-84
lines changed

coderd/activitybump.go

+1-78
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ package coderd
22

33
import (
44
"context"
5-
"database/sql"
6-
"errors"
75
"time"
86

97
"github.com/google/uuid"
108
"golang.org/x/xerrors"
119

1210
"cdr.dev/slog"
1311
"github.com/coder/coder/v2/coderd/database"
14-
"github.com/coder/coder/v2/coderd/database/dbtime"
1512
)
1613

1714
// activityBumpWorkspace automatically bumps the workspace's auto-off timer
@@ -21,81 +18,7 @@ func activityBumpWorkspace(ctx context.Context, log slog.Logger, db database.Sto
2118
// low priority operations fail first.
2219
ctx, cancel := context.WithTimeout(ctx, time.Second*15)
2320
defer cancel()
24-
// if err := db.ActivityBumpWorkspace(ctx, workspaceID); err != nil {
25-
// if !xerrors.Is(err, context.Canceled) && !database.IsQueryCanceledError(err) {
26-
// // Bump will fail if the context is canceled, but this is ok.
27-
// log.Error(ctx, "bump failed", slog.Error(err),
28-
// slog.F("workspace_id", workspaceID),
29-
// )
30-
// }
31-
// return
32-
// }
33-
34-
err := db.InTx(func(s database.Store) error {
35-
build, err := s.GetLatestWorkspaceBuildByWorkspaceID(ctx, workspaceID)
36-
if errors.Is(err, sql.ErrNoRows) {
37-
return nil
38-
} else if err != nil {
39-
return xerrors.Errorf("get latest workspace build: %w", err)
40-
}
41-
42-
job, err := s.GetProvisionerJobByID(ctx, build.JobID)
43-
if err != nil {
44-
return xerrors.Errorf("get provisioner job: %w", err)
45-
}
46-
47-
if build.Transition != database.WorkspaceTransitionStart || !job.CompletedAt.Valid {
48-
return nil
49-
}
50-
51-
if build.Deadline.IsZero() {
52-
// Workspace shutdown is manual
53-
return nil
54-
}
55-
56-
workspace, err := s.GetWorkspaceByID(ctx, workspaceID)
57-
if err != nil {
58-
return xerrors.Errorf("get workspace: %w", err)
59-
}
60-
61-
var (
62-
// We bump by the original TTL to prevent counter-intuitive behavior
63-
// as the TTL wraps. For example, if I set the TTL to 12 hours, sign off
64-
// work at midnight, come back at 10am, I would want another full day
65-
// of uptime. In the prior implementation, the workspace would enter
66-
// a state of always expiring 1 hour in the future
67-
bumpAmount = time.Duration(workspace.Ttl.Int64)
68-
// DB writes are expensive so we only bump when 5% of the deadline
69-
// has elapsed.
70-
bumpEvery = bumpAmount / 20
71-
timeSinceLastBump = bumpAmount - time.Until(build.Deadline)
72-
)
73-
74-
if timeSinceLastBump < bumpEvery {
75-
return nil
76-
}
77-
78-
if bumpAmount == 0 {
79-
return nil
80-
}
81-
82-
newDeadline := dbtime.Now().Add(bumpAmount)
83-
if !build.MaxDeadline.IsZero() && newDeadline.After(build.MaxDeadline) {
84-
newDeadline = build.MaxDeadline
85-
}
86-
87-
if err := s.UpdateWorkspaceBuildByID(ctx, database.UpdateWorkspaceBuildByIDParams{
88-
ID: build.ID,
89-
UpdatedAt: dbtime.Now(),
90-
ProvisionerState: build.ProvisionerState,
91-
Deadline: newDeadline,
92-
MaxDeadline: build.MaxDeadline,
93-
}); err != nil {
94-
return xerrors.Errorf("update workspace build: %w", err)
95-
}
96-
return nil
97-
}, nil)
98-
if err != nil {
21+
if err := db.ActivityBumpWorkspace(ctx, workspaceID); err != nil {
9922
if !xerrors.Is(err, context.Canceled) && !database.IsQueryCanceledError(err) {
10023
// Bump will fail if the context is canceled, but this is ok.
10124
log.Error(ctx, "bump failed", slog.Error(err),

coderd/activitybump_internal_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func Test_ActivityBumpWorkspace(t *testing.T) {
166166
require.Equal(t, bld.UpdatedAt.UTC(), updatedBuild.UpdatedAt.UTC(), "should not have bumped updated_at")
167167
require.Equal(t, bld.Deadline.UTC(), updatedBuild.Deadline.UTC(), "should not have bumped deadline")
168168
} else {
169-
require.WithinDuration(t, dbtime.Now(), updatedBuild.UpdatedAt, 15*time.Second, "unexpected updated at time after bump")
169+
require.NotEqual(t, bld.UpdatedAt.UTC(), updatedBuild.UpdatedAt.UTC(), "should have bumped updated_at")
170170
expectedDeadline := approxBumpTime.Add(tt.expectedBump).UTC()
171171
require.WithinDuration(t, expectedDeadline, updatedBuild.Deadline.UTC(), 15*time.Second, "unexpected deadline after bump")
172172
}

coderd/database/dbmetrics/dbmetrics.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

+9-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/activitybump.sql

+9-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,20 @@ SET
2323
updated_at = NOW(),
2424
deadline = CASE
2525
WHEN l.build_max_deadline = '0001-01-01 00:00:00'
26+
-- We bump by the original TTL to prevent counter-intuitive behavior
27+
-- as the TTL wraps. For example, if I set the TTL to 12 hours, sign off
28+
-- work at midnight, come back at 10am, I would want another full day
29+
-- of uptime. In the prior implementation, the workspace would enter
30+
-- a state of always expiring 1 hour in the future.
2631
THEN l.now_utc + l.ttl_interval
2732
ELSE LEAST(l.now_utc + l.ttl_interval, l.build_max_deadline)
2833
END
2934
FROM latest l
3035
WHERE wb.id = l.build_id
36+
AND l.job_completed_at IS NOT NULL
3137
AND l.build_transition = 'start'
38+
-- Workspace shutdown is manual.
3239
AND l.build_deadline != '0001-01-01 00:00:00'
33-
AND l.job_completed_at IS NOT NULL
34-
AND l.build_deadline + (l.ttl_interval * 0.05) < NOW()
40+
-- We only bump when 5% of the deadline has elapsed.
41+
AND l.build_deadline - (l.ttl_interval * 0.95) < NOW()
3542
;

0 commit comments

Comments
 (0)