-
Notifications
You must be signed in to change notification settings - Fork 887
feat(coderd/database/dbpurge): retain most recent agent build logs #14460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9451962
0c14a3e
0af051e
dd97358
2017434
725eeb0
591dd91
0f8ab25
5c77b5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import ( | |
|
||
"github.com/coder/coder/v2/coderd/database" | ||
"github.com/coder/coder/v2/coderd/database/dbauthz" | ||
"github.com/coder/coder/v2/coderd/database/dbtime" | ||
"github.com/coder/quartz" | ||
) | ||
|
||
|
@@ -30,7 +31,8 @@ func New(ctx context.Context, logger slog.Logger, db database.Store, clk quartz. | |
//nolint:gocritic // The system purges old db records without user input. | ||
ctx = dbauthz.AsSystemRestricted(ctx) | ||
|
||
ticker := clk.NewTicker(time.Nanosecond) | ||
// Start the ticker with the initial delay. | ||
ticker := clk.NewTicker(delay) | ||
Comment on lines
+34
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Review: I found this to be the source of a race condition while testing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What was the race condition? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may actually be the same race condition you mentioned below, just moved around slightly. |
||
doTick := func(start time.Time) { | ||
defer ticker.Reset(delay) | ||
// Start a transaction to grab advisory lock, we don't want to run | ||
|
@@ -47,7 +49,8 @@ func New(ctx context.Context, logger slog.Logger, db database.Store, clk quartz. | |
return nil | ||
} | ||
|
||
if err := tx.DeleteOldWorkspaceAgentLogs(ctx, start.Add(-maxAgentLogAge)); err != nil { | ||
deleteOldWorkspaceAgentLogsBefore := start.Add(-maxAgentLogAge) | ||
if err := tx.DeleteOldWorkspaceAgentLogs(ctx, deleteOldWorkspaceAgentLogsBefore); err != nil { | ||
return xerrors.Errorf("failed to delete old workspace agent logs: %w", err) | ||
} | ||
if err := tx.DeleteOldWorkspaceAgentStats(ctx); err != nil { | ||
|
@@ -72,13 +75,15 @@ func New(ctx context.Context, logger slog.Logger, db database.Store, clk quartz. | |
go func() { | ||
defer close(closed) | ||
defer ticker.Stop() | ||
// Force an initial tick. | ||
doTick(dbtime.Time(clk.Now()).UTC()) | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case tick := <-ticker.C: | ||
ticker.Stop() | ||
doTick(tick) | ||
doTick(dbtime.Time(tick).UTC()) | ||
} | ||
} | ||
}() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really nice!
I'm concerned it'll go stale...
Although it could be argued all the business logic duplicated in this file is stale the second it's written.
We really need to get rid of dbmem.