storj/satellite/accounting/projectbwcleanup/chore.go
JT Olio da9ca0c650 testplanet/satellite: reduce the number of places default values need to be configured
Satellites set their configuration values to default values using
cfgstruct, however, it turns out our tests don't test these values
at all! Instead, they have a completely separate definition system
that is easy to forget about.

As is to be expected, these values have drifted, and it appears
in a few cases test planet is testing unreasonable values that we
won't see in production, or perhaps worse, features enabled in
production were missed and weren't enabled in testplanet.

This change makes it so all values are configured the same,
systematic way, so it's easy to see when test values are different
than dev values or release values, and it's less hard to forget
to enable features in testplanet.

In terms of reviewing, this change should be actually fairly
easy to review, considering private/testplanet/satellite.go keeps
the current config system and the new one and confirms that they
result in identical configurations, so you can be certain that
nothing was missed and the config is all correct.
You can also check the config lock to see what actual config
values changed.

Change-Id: I6715d0794887f577e21742afcf56fd2b9d12170e
2021-06-01 22:14:17 +00:00

80 lines
2.0 KiB
Go

// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package projectbwcleanup
import (
"context"
"time"
"github.com/spacemonkeygo/monkit/v3"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/common/sync2"
"storj.io/storj/satellite/accounting"
)
var mon = monkit.Package()
// Config is a configuration struct for the Chore.
type Config struct {
Interval time.Duration `help:"how often to remove unused project bandwidth rollups" default:"168h" testDefault:"$TESTINTERVAL"`
RetainMonths int `help:"number of months of project bandwidth rollups to retain, not including the current month" default:"2" testDefault:"1"`
}
// Chore to remove unused project bandwidth rollups.
//
// architecture: Chore
type Chore struct {
log *zap.Logger
db accounting.ProjectAccounting
config Config
Loop *sync2.Cycle
}
// NewChore creates new chore for removing unused project bandwidth rollups.
func NewChore(log *zap.Logger, db accounting.ProjectAccounting, config Config) *Chore {
return &Chore{
log: log,
db: db,
config: config,
Loop: sync2.NewCycle(config.Interval),
}
}
// Run starts the chore.
func (chore *Chore) Run(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
return chore.Loop.Run(ctx, func(ctx context.Context) error {
err := chore.RunOnce(ctx)
if err != nil {
chore.log.Error("error removing project bandwidth rollups", zap.Error(err))
}
return nil
})
}
// RunOnce removes unused project bandwidth rollups.
func (chore *Chore) RunOnce(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
if chore.config.RetainMonths < 0 {
return errs.New("retain months cannot be less than 0")
}
now := time.Now().UTC()
beforeMonth := time.Date(now.Year(), now.Month()-time.Month(chore.config.RetainMonths), 1, 0, 0, 0, 0, time.UTC)
return chore.db.DeleteProjectBandwidthBefore(ctx, beforeMonth)
}
// Close stops the chore.
func (chore *Chore) Close() error {
chore.Loop.Close()
return nil
}