@@ -2,11 +2,10 @@ package dbpurge
2
2
3
3
import (
4
4
"context"
5
- "errors"
6
5
"io"
7
6
"time"
8
7
9
- "golang.org/x/sync/errgroup "
8
+ "golang.org/x/xerrors "
10
9
11
10
"cdr.dev/slog"
12
11
@@ -35,22 +34,38 @@ func New(ctx context.Context, logger slog.Logger, db database.Store) io.Closer {
35
34
doTick := func () {
36
35
defer ticker .Reset (delay )
37
36
38
- var eg errgroup.Group
39
- eg .Go (func () error {
40
- return db .DeleteOldWorkspaceAgentLogs (ctx )
41
- })
42
- eg .Go (func () error {
43
- return db .DeleteOldWorkspaceAgentStats (ctx )
44
- })
45
- eg .Go (func () error {
46
- return db .DeleteOldProvisionerDaemons (ctx )
47
- })
48
- err := eg .Wait ()
49
- if err != nil {
50
- if errors .Is (err , context .Canceled ) {
51
- return
37
+ start := time .Now ()
38
+ // Start a transaction to grab advisory lock, we don't want to run
39
+ // multiple purges at the same time (multiple replicas). The purge
40
+ // queries themselves are run outside of the transaction.
41
+ if err := db .InTx (func (tx database.Store ) error {
42
+ // Acquire a lock to ensure that only one instance of
43
+ // the rollup is running at a time.
44
+ ok , err := tx .TryAcquireLock (ctx , database .LockIDDBPurge )
45
+ if err != nil {
46
+ return err
47
+ }
48
+ if ! ok {
49
+ logger .Debug (ctx , "unable to acquire lock for purging old database entries, skipping" )
50
+ return nil
51
+ }
52
+
53
+ if err := tx .DeleteOldWorkspaceAgentLogs (ctx ); err != nil {
54
+ return xerrors .Errorf ("failed to delete old workspace agent logs: %w" , err )
52
55
}
56
+ if err := tx .DeleteOldWorkspaceAgentStats (ctx ); err != nil {
57
+ return xerrors .Errorf ("failed to delete old workspace agent stats: %w" , err )
58
+ }
59
+ if err := tx .DeleteOldProvisionerDaemons (ctx ); err != nil {
60
+ return xerrors .Errorf ("failed to delete old provisioner daemons: %w" , err )
61
+ }
62
+
63
+ logger .Info (ctx , "purged old database entries" , slog .F ("duration" , time .Since (start )))
64
+
65
+ return nil
66
+ }, nil ); err != nil {
53
67
logger .Error (ctx , "failed to purge old database entries" , slog .Error (err ))
68
+ return
54
69
}
55
70
}
56
71
0 commit comments