2019-08-27 18:12:38 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package dbcleanup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2019-11-08 20:40:39 +00:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2019-08-27 18:12:38 +01:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/sync2"
|
2019-08-27 18:12:38 +01:00
|
|
|
"storj.io/storj/satellite/orders"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Error the default dbcleanup errs class.
|
|
|
|
Error = errs.Class("dbcleanup error")
|
|
|
|
|
|
|
|
mon = monkit.Package()
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config defines configuration struct for dbcleanup chore.
|
|
|
|
type Config struct {
|
|
|
|
SerialsInterval time.Duration `help:"how often to delete expired serial numbers" default:"24h"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chore for deleting DB entries that are no longer needed.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Chore
|
2019-08-27 18:12:38 +01:00
|
|
|
type Chore struct {
|
|
|
|
log *zap.Logger
|
|
|
|
orders orders.DB
|
|
|
|
|
2020-01-30 13:06:43 +00:00
|
|
|
Serials *sync2.Cycle
|
2019-08-27 18:12:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewChore creates new chore for deleting DB entries.
|
|
|
|
func NewChore(log *zap.Logger, orders orders.DB, config Config) *Chore {
|
|
|
|
return &Chore{
|
|
|
|
log: log,
|
|
|
|
orders: orders,
|
|
|
|
|
2020-01-30 13:06:43 +00:00
|
|
|
Serials: sync2.NewCycle(config.SerialsInterval),
|
2019-08-27 18:12:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts the db cleanup chore.
|
|
|
|
func (chore *Chore) Run(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return chore.Serials.Run(ctx, chore.deleteExpiredSerials)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (chore *Chore) deleteExpiredSerials(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
chore.log.Debug("deleting expired serial numbers")
|
|
|
|
|
|
|
|
deleted, err := chore.orders.DeleteExpiredSerials(ctx, time.Now().UTC())
|
|
|
|
if err != nil {
|
|
|
|
chore.log.Error("deleting expired serial numbers", zap.Error(err))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
chore.log.Debug("expired serials deleted", zap.Int("items deleted", deleted))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close stops the dbcleanup chore.
|
|
|
|
func (chore *Chore) Close() error {
|
|
|
|
chore.Serials.Close()
|
|
|
|
return nil
|
|
|
|
}
|