storj/satellite/orders/rollups_chore.go
Isaac Hess 40a890639d satellite/orders: Flush all pending bandwidth rollup writes on shutdown
Currently we risk losing pending bandwidth rollup writes even on a clean
shutdown. This change ensures that all pending writes are actually
written to the db when shutting down the satellite.

Change-Id: Ideab62fa9808937d3dce9585c52405d8c8a0e703
2020-01-23 08:12:41 -07:00

46 lines
1.0 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package orders
import (
"context"
"go.uber.org/zap"
"storj.io/common/sync2"
)
// Chore for flushing orders write cache to the database.
//
// architecture: Chore
type Chore struct {
log *zap.Logger
rollupsWriteCache *RollupsWriteCache
Loop *sync2.Cycle
}
// NewChore creates new chore for flushing the orders write cache to the database.
func NewChore(log *zap.Logger, rollupsWriteCache *RollupsWriteCache, config Config) *Chore {
return &Chore{
log: log,
rollupsWriteCache: rollupsWriteCache,
Loop: sync2.NewCycle(config.FlushInterval),
}
}
// Run starts the orders write cache 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 {
chore.rollupsWriteCache.Flush(ctx)
return nil
})
}
// Close stops the orders write cache chore.
func (chore *Chore) Close() error {
chore.Loop.Close()
return nil
}