-
Notifications
You must be signed in to change notification settings - Fork 0
/
housekeeper.go
49 lines (41 loc) · 972 Bytes
/
housekeeper.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
package main
import (
"context"
"time"
"github.com/0x4c6565/p.lee.io/pkg/storage"
"github.com/rs/zerolog/log"
)
type Housekeeper struct {
storage storage.Storage
}
func NewHousekeeper(storage storage.Storage) *Housekeeper {
return &Housekeeper{
storage: storage,
}
}
func (h *Housekeeper) Start(ctx context.Context) error {
log.Info().Msg("Starting housekeeper")
for {
select {
case <-ctx.Done():
log.Info().Msg("Housekeeper stopped")
return ctx.Err()
case <-time.After(60 * time.Second):
h.work(ctx)
}
}
}
func (h Housekeeper) work(ctx context.Context) {
expiredPastes, err := h.storage.GetExpired(ctx)
if err != nil {
log.Err(err).Msg("Failed to retrieve expired pastes")
}
log.Trace().Msg("Retrieving expired pastes")
for _, paste := range expiredPastes {
log.Debug().Msgf("Removing paste %s", paste.ID)
err = h.storage.Delete(ctx, paste.ID)
if err != nil {
log.Err(err).Msg("Failed to burn expired paste")
}
}
}