-
Notifications
You must be signed in to change notification settings - Fork 1
/
lock_cleaner.go
80 lines (65 loc) · 1.73 KB
/
lock_cleaner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package repository
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/ttab/elephant-repository/postgres"
"github.com/ttab/elephantine"
"github.com/ttab/elephantine/pg"
)
func (s *PGDocStore) RunCleaner(ctx context.Context, period time.Duration) {
for {
select {
case <-time.After(period):
case <-ctx.Done():
return
}
jobLock, err := pg.NewJobLock(s.pool, s.logger, "cleaner", pg.JobLockOptions{
PingInterval: 10 * time.Second,
StaleAfter: 1 * time.Minute,
CheckInterval: 20 * time.Second,
Timeout: 5 * time.Second,
})
if err != nil {
s.logger.ErrorContext(ctx, "failed to create job lock",
elephantine.LogKeyError, err)
continue
}
err = jobLock.RunWithContext(ctx, s.removeExpiredLocks)
if err != nil {
s.logger.ErrorContext(
ctx, "lock cleaner error",
elephantine.LogKeyError, err,
)
}
}
}
func (s *PGDocStore) removeExpiredLocks(ctx context.Context) error {
s.logger.Debug("removing expired document locks")
cutoff := pg.Time(time.Now().Add(5 * time.Minute))
err := s.withTX(ctx, "lock cleaner", func(tx pgx.Tx) error {
q := postgres.New(tx)
expired, err := q.GetExpiredDocumentLocks(ctx, cutoff)
if err != nil {
return fmt.Errorf("could not get expired locks: %w", err)
}
uuids := make([]uuid.UUID, len(expired))
for i := 0; i < len(expired); i++ {
uuids[i] = expired[i].UUID
}
err = q.DeleteExpiredDocumentLock(ctx, postgres.DeleteExpiredDocumentLockParams{
Cutoff: cutoff,
Uuids: uuids,
})
if err != nil {
return fmt.Errorf("could not remove expired locks: %w", err)
}
return nil
})
if err != nil {
return fmt.Errorf("could not remove expired locks: %w", err)
}
return nil
}