2020-01-10 18:53:42 +00:00
|
|
|
// 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 {
|
2020-01-17 22:55:53 +00:00
|
|
|
log *zap.Logger
|
|
|
|
rollupsWriteCache *RollupsWriteCache
|
|
|
|
Loop *sync2.Cycle
|
2020-01-10 18:53:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewChore creates new chore for flushing the orders write cache to the database.
|
2020-01-17 22:55:53 +00:00
|
|
|
func NewChore(log *zap.Logger, rollupsWriteCache *RollupsWriteCache, config Config) *Chore {
|
2020-01-10 18:53:42 +00:00
|
|
|
return &Chore{
|
2020-01-17 22:55:53 +00:00
|
|
|
log: log,
|
|
|
|
rollupsWriteCache: rollupsWriteCache,
|
|
|
|
Loop: sync2.NewCycle(config.FlushInterval),
|
2020-01-10 18:53:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2020-01-17 22:55:53 +00:00
|
|
|
chore.rollupsWriteCache.Flush(ctx)
|
2020-01-10 18:53:42 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close stops the orders write cache chore.
|
|
|
|
func (chore *Chore) Close() error {
|
|
|
|
chore.Loop.Close()
|
|
|
|
return nil
|
|
|
|
}
|