Skip to content

Commit 26abb16

Browse files
committed
refactor(coderd/database): allow passing deletion threshold into DeleteOldWorkspaceAgentLogs
1 parent 4b5c45d commit 26abb16

File tree

9 files changed

+25
-26
lines changed

9 files changed

+25
-26
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,11 +1144,11 @@ func (q *querier) DeleteOldProvisionerDaemons(ctx context.Context) error {
11441144
return q.db.DeleteOldProvisionerDaemons(ctx)
11451145
}
11461146

1147-
func (q *querier) DeleteOldWorkspaceAgentLogs(ctx context.Context) error {
1147+
func (q *querier) DeleteOldWorkspaceAgentLogs(ctx context.Context, threshold time.Time) error {
11481148
if err := q.authorizeContext(ctx, policy.ActionDelete, rbac.ResourceSystem); err != nil {
11491149
return err
11501150
}
1151-
return q.db.DeleteOldWorkspaceAgentLogs(ctx)
1151+
return q.db.DeleteOldWorkspaceAgentLogs(ctx, threshold)
11521152
}
11531153

11541154
func (q *querier) DeleteOldWorkspaceAgentStats(ctx context.Context) error {

coderd/database/dbauthz/dbauthz_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2514,7 +2514,7 @@ func (s *MethodTestSuite) TestSystemFunctions() {
25142514
}).Asserts(rbac.ResourceSystem, policy.ActionCreate)
25152515
}))
25162516
s.Run("DeleteOldWorkspaceAgentLogs", s.Subtest(func(db database.Store, check *expects) {
2517-
check.Args().Asserts(rbac.ResourceSystem, policy.ActionDelete)
2517+
check.Args(time.Time{}).Asserts(rbac.ResourceSystem, policy.ActionDelete)
25182518
}))
25192519
s.Run("InsertWorkspaceAgentStats", s.Subtest(func(db database.Store, check *expects) {
25202520
check.Args(database.InsertWorkspaceAgentStatsParams{}).Asserts(rbac.ResourceSystem, policy.ActionCreate).Errors(errMatchAny)

coderd/database/dbmem/dbmem.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,19 +1706,15 @@ func (q *FakeQuerier) DeleteOldProvisionerDaemons(_ context.Context) error {
17061706
return nil
17071707
}
17081708

1709-
func (q *FakeQuerier) DeleteOldWorkspaceAgentLogs(_ context.Context) error {
1709+
func (q *FakeQuerier) DeleteOldWorkspaceAgentLogs(_ context.Context, threshold time.Time) error {
17101710
q.mutex.Lock()
17111711
defer q.mutex.Unlock()
17121712

1713-
now := dbtime.Now()
1714-
weekInterval := 7 * 24 * time.Hour
1715-
weekAgo := now.Add(-weekInterval)
1716-
17171713
var validLogs []database.WorkspaceAgentLog
17181714
for _, log := range q.workspaceAgentLogs {
17191715
var toBeDeleted bool
17201716
for _, agent := range q.workspaceAgents {
1721-
if agent.ID == log.AgentID && agent.LastConnectedAt.Valid && agent.LastConnectedAt.Time.Before(weekAgo) {
1717+
if agent.ID == log.AgentID && agent.LastConnectedAt.Valid && agent.LastConnectedAt.Time.Before(threshold) {
17221718
toBeDeleted = true
17231719
break
17241720
}

coderd/database/dbmetrics/dbmetrics.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbmock/dbmock.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbpurge/dbpurge.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import (
1414
)
1515

1616
const (
17-
delay = 10 * time.Minute
17+
delay = 10 * time.Minute
18+
maxAgentLogAge = 7 * 24 * time.Hour
1819
)
1920

2021
// New creates a new periodically purging database instance.
@@ -31,10 +32,9 @@ func New(ctx context.Context, logger slog.Logger, db database.Store) io.Closer {
3132
// Use time.Nanosecond to force an initial tick. It will be reset to the
3233
// correct duration after executing once.
3334
ticker := time.NewTicker(time.Nanosecond)
34-
doTick := func() {
35+
doTick := func(start time.Time) {
3536
defer ticker.Reset(delay)
3637

37-
start := time.Now()
3838
// Start a transaction to grab advisory lock, we don't want to run
3939
// multiple purges at the same time (multiple replicas).
4040
if err := db.InTx(func(tx database.Store) error {
@@ -49,7 +49,7 @@ func New(ctx context.Context, logger slog.Logger, db database.Store) io.Closer {
4949
return nil
5050
}
5151

52-
if err := tx.DeleteOldWorkspaceAgentLogs(ctx); err != nil {
52+
if err := tx.DeleteOldWorkspaceAgentLogs(ctx, start.Add(-maxAgentLogAge)); err != nil {
5353
return xerrors.Errorf("failed to delete old workspace agent logs: %w", err)
5454
}
5555
if err := tx.DeleteOldWorkspaceAgentStats(ctx); err != nil {
@@ -78,9 +78,12 @@ func New(ctx context.Context, logger slog.Logger, db database.Store) io.Closer {
7878
select {
7979
case <-ctx.Done():
8080
return
81-
case <-ticker.C:
81+
case now, ok := <-ticker.C:
82+
if !ok {
83+
return
84+
}
8285
ticker.Stop()
83-
doTick()
86+
doTick(now)
8487
}
8588
}
8689
}()

coderd/database/querier.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaceagents.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ SELECT * FROM workspace_agent_log_sources WHERE workspace_agent_id = ANY(@ids ::
192192
-- name: DeleteOldWorkspaceAgentLogs :exec
193193
DELETE FROM workspace_agent_logs WHERE agent_id IN
194194
(SELECT id FROM workspace_agents WHERE last_connected_at IS NOT NULL
195-
AND last_connected_at < NOW() - INTERVAL '7 day');
195+
AND last_connected_at < @threshold :: timestamptz);
196196

197197
-- name: GetWorkspaceAgentsInLatestBuildByWorkspaceID :many
198198
SELECT

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy