Skip to content

Commit e25d8ab

Browse files
committed
first stab at query
1 parent b3c9839 commit e25d8ab

File tree

8 files changed

+147
-56
lines changed

8 files changed

+147
-56
lines changed

coderd/activitybump.go

+60-56
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package coderd
22

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

97
"github.com/google/uuid"
@@ -23,67 +21,73 @@ func activityBumpWorkspace(ctx context.Context, log slog.Logger, db database.Sto
2321
defer cancel()
2422

2523
err := db.InTx(func(s database.Store) error {
26-
build, err := s.GetLatestWorkspaceBuildByWorkspaceID(ctx, workspaceID)
27-
if errors.Is(err, sql.ErrNoRows) {
28-
return nil
29-
} else if err != nil {
30-
return xerrors.Errorf("get latest workspace build: %w", err)
24+
if err := s.ActivityBumpWorkspace(ctx, database.ActivityBumpWorkspaceParams{
25+
WorkspaceID: workspaceID,
26+
UpdatedAt: dbtime.Now(),
27+
}); err != nil {
28+
return xerrors.Errorf("activity bump workspace: %w", err)
3129
}
30+
// build, err := s.GetLatestWorkspaceBuildByWorkspaceID(ctx, workspaceID)
31+
// if errors.Is(err, sql.ErrNoRows) {
32+
// return nil
33+
// } else if err != nil {
34+
// return xerrors.Errorf("get latest workspace build: %w", err)
35+
// }
3236

33-
job, err := s.GetProvisionerJobByID(ctx, build.JobID)
34-
if err != nil {
35-
return xerrors.Errorf("get provisioner job: %w", err)
36-
}
37+
// job, err := s.GetProvisionerJobByID(ctx, build.JobID)
38+
// if err != nil {
39+
// return xerrors.Errorf("get provisioner job: %w", err)
40+
// }
3741

38-
if build.Transition != database.WorkspaceTransitionStart || !job.CompletedAt.Valid {
39-
return nil
40-
}
42+
// if build.Transition != database.WorkspaceTransitionStart || !job.CompletedAt.Valid {
43+
// return nil
44+
// }
4145

42-
if build.Deadline.IsZero() {
43-
// Workspace shutdown is manual
44-
return nil
45-
}
46+
// if build.Deadline.IsZero() {
47+
// // Workspace shutdown is manual
48+
// return nil
49+
// }
4650

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

65-
if timeSinceLastBump < bumpEvery {
66-
return nil
67-
}
56+
// var (
57+
// We bump by the original TTL to prevent counter-intuitive behavior
58+
// as the TTL wraps. For example, if I set the TTL to 12 hours, sign off
59+
// work at midnight, come back at 10am, I would want another full day
60+
// of uptime. In the prior implementation, the workspace would enter
61+
// a state of always expiring 1 hour in the future
62+
// bumpAmount = time.Duration(workspace.Ttl.Int64)
63+
// DB writes are expensive so we only bump when 5% of the deadline
64+
// has elapsed.
65+
// bumpEvery = bumpAmount / 20
66+
// timeSinceLastBump = bumpAmount - time.Until(build.Deadline)
67+
// )
6868

69-
if bumpAmount == 0 {
70-
return nil
71-
}
72-
73-
newDeadline := dbtime.Now().Add(bumpAmount)
74-
if !build.MaxDeadline.IsZero() && newDeadline.After(build.MaxDeadline) {
75-
newDeadline = build.MaxDeadline
76-
}
77-
78-
if err := s.UpdateWorkspaceBuildByID(ctx, database.UpdateWorkspaceBuildByIDParams{
79-
ID: build.ID,
80-
UpdatedAt: dbtime.Now(),
81-
ProvisionerState: build.ProvisionerState,
82-
Deadline: newDeadline,
83-
MaxDeadline: build.MaxDeadline,
84-
}); err != nil {
85-
return xerrors.Errorf("update workspace build: %w", err)
86-
}
69+
// if timeSinceLastBump < bumpEvery {
70+
// return nil
71+
// }
72+
//
73+
// if bumpAmount == 0 {
74+
// return nil
75+
// }
76+
//
77+
// newDeadline := dbtime.Now().Add(bumpAmount)
78+
// if !build.MaxDeadline.IsZero() && newDeadline.After(build.MaxDeadline) {
79+
// newDeadline = build.MaxDeadline
80+
// }
81+
//
82+
// if err := s.UpdateWorkspaceBuildByID(ctx, database.UpdateWorkspaceBuildByIDParams{
83+
// ID: build.ID,
84+
// UpdatedAt: dbtime.Now(),
85+
// ProvisionerState: build.ProvisionerState,
86+
// Deadline: newDeadline,
87+
// MaxDeadline: build.MaxDeadline,
88+
// }); err != nil {
89+
// return xerrors.Errorf("update workspace build: %w", err)
90+
// }
8791
return nil
8892
}, nil)
8993
if err != nil {

coderd/database/dbauthz/dbauthz.go

+4
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,10 @@ func (q *querier) AcquireProvisionerJob(ctx context.Context, arg database.Acquir
657657
return q.db.AcquireProvisionerJob(ctx, arg)
658658
}
659659

660+
func (q *querier) ActivityBumpWorkspace(ctx context.Context, arg database.ActivityBumpWorkspaceParams) error {
661+
panic("not implemented")
662+
}
663+
660664
func (q *querier) CleanTailnetCoordinators(ctx context.Context) error {
661665
if err := q.authorizeContext(ctx, rbac.ActionDelete, rbac.ResourceTailnetCoordinator); err != nil {
662666
return err

coderd/database/dbfake/dbfake.go

+9
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,15 @@ func (q *FakeQuerier) AcquireProvisionerJob(_ context.Context, arg database.Acqu
744744
return database.ProvisionerJob{}, sql.ErrNoRows
745745
}
746746

747+
func (q *FakeQuerier) ActivityBumpWorkspace(ctx context.Context, arg database.ActivityBumpWorkspaceParams) error {
748+
err := validateDatabaseType(arg)
749+
if err != nil {
750+
return err
751+
}
752+
753+
panic("not implemented")
754+
}
755+
747756
func (*FakeQuerier) CleanTailnetCoordinators(_ context.Context) error {
748757
return ErrUnimplemented
749758
}

coderd/database/dbmetrics/dbmetrics.go

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

coderd/database/dbmock/dbmock.go

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

coderd/database/querier.go

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

coderd/database/queries.sql.go

+32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- name: ActivityBumpWorkspace :exec
2+
UPDATE
3+
workspace_builds
4+
SET
5+
updated_at = $2,
6+
deadline = LEAST(workspace_builds.deadline + workspace.ttl, workspace_builds.max_deadline)
7+
WHERE
8+
workspace_builds.id IN (
9+
SELECT wb.id
10+
FROM workspace_builds wb
11+
JOIN provisioner_jobs pj
12+
ON pj.id = wb.job_id
13+
WHERE wb.workspace_id = $1
14+
AND wb.transition == 'start'
15+
AND wb.deadline > $2
16+
AND wb.deadline != wb.max_deadline
17+
AND pj.completed_at IS NOT NULL
18+
ORDER BY wb.build_number ASC
19+
LIMIT 1
20+
);

0 commit comments

Comments
 (0)