Skip to content

chore(coderd/database/dbpurge): replace usage of time.* with quartz #14480

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

Merged
merged 3 commits into from
Aug 30, 2024

Conversation

johnstcn
Copy link
Member

@johnstcn johnstcn commented Aug 29, 2024

Related to #10576

This PR introduces quartz to coderd/database/dbpurge and updates the following unit tests to make use of Quartz's functionality:

  • TestPurge
  • TestDeleteOldWorkspaceAgentLogs

Additionally, updates DeleteOldWorkspaceAgentLogs to replace the hard-coded interval with a parameter passed into the query. This aids in testing and brings us a step towards allowing operators to configure the cutoff interval for workspace agent logs.

@johnstcn johnstcn self-assigned this Aug 29, 2024
@johnstcn johnstcn changed the title Cj/dbpurge quartz chore(coderd/database/dbpurge): replace usage of time.Now() with quartz Aug 29, 2024
@johnstcn johnstcn changed the title chore(coderd/database/dbpurge): replace usage of time.Now() with quartz chore(coderd/database/dbpurge): replace usage of time.* with quartz Aug 29, 2024
@johnstcn johnstcn marked this pull request as ready for review August 29, 2024 16:06
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

agent := mustCreateAgentWithLogs(ctx, t, db, user, org, tmpl, tv, now.Add(-8*24*time.Hour), t.Name())
// After dbpurge completes, the ticker is reset. Trap this call.
trapReset := clk.Trap().TickerReset()
defer trapReset.Close()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems shared between all tests, how about refactoring to a helper like below, or consider only testing it in one test and assuming it applies to the rest?

	t.Helper()

	// After dbpurge completes, the ticker is reset. Trap this call.
	trapReset := clk.Trap().TickerReset()
	defer trapReset.Close()

	closer := do(clk)
	
	// Wait for the initial nanosecond tick.
	clk.Advance(time.Nanosecond).MustWait(ctx)

	trapReset.MustWait(ctx).Release() // Wait for ticker.Reset()

	return closer

Perhaps this example can even be taken further, just here as food for thought.

Copy link
Member Author

@johnstcn johnstcn Aug 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I modify this test in the follow-up PR #14460 so the sharing between tests won't be an issue.

But I do like the idea of extracting this to a test helper!

Copy link
Contributor

@dannykopping dannykopping left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

}

//nolint:paralleltest // It uses LockIDDBPurge.
func TestDeleteOldWorkspaceAgentStats(t *testing.T) {
db, _ := dbtestutil.NewDB(t)
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: testutil.Context is a nice convenience for this:

func Context(t *testing.T, dur time.Duration) context.Context {
	ctx, cancel := context.WithTimeout(context.Background(), dur)
	t.Cleanup(cancel)
	return ctx
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will address in follow-up 👍

@johnstcn johnstcn merged commit a74273f into main Aug 30, 2024
27 checks passed
@johnstcn johnstcn deleted the cj/dbpurge-quartz branch August 30, 2024 10:55
@github-actions github-actions bot locked and limited conversation to collaborators Aug 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants