2018-11-14 01:22:18 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package rollup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2018-12-14 14:27:21 +00:00
|
|
|
"github.com/zeebo/errs"
|
2018-11-14 01:22:18 +00:00
|
|
|
"go.uber.org/zap"
|
2018-12-14 14:27:21 +00:00
|
|
|
|
2018-11-26 21:49:55 +00:00
|
|
|
"storj.io/storj/pkg/accounting"
|
2018-11-14 01:22:18 +00:00
|
|
|
"storj.io/storj/pkg/provider"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config contains configurable values for rollup
|
|
|
|
type Config struct {
|
2019-01-17 19:39:32 +00:00
|
|
|
Interval time.Duration `help:"how frequently rollup should run" default:"120s"`
|
2018-11-14 01:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize a rollup struct
|
|
|
|
func (c Config) initialize(ctx context.Context) (Rollup, error) {
|
2018-12-14 14:27:21 +00:00
|
|
|
db, ok := ctx.Value("masterdb").(interface{ Accounting() accounting.DB })
|
|
|
|
if !ok {
|
|
|
|
return nil, Error.Wrap(errs.New("unable to get master db instance"))
|
2018-11-26 21:49:55 +00:00
|
|
|
}
|
2018-12-14 14:27:21 +00:00
|
|
|
return newRollup(zap.L(), db.Accounting(), c.Interval), nil
|
2018-11-14 01:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run runs the rollup with configured values
|
|
|
|
func (c Config) Run(ctx context.Context, server *provider.Provider) (err error) {
|
|
|
|
rollup, err := c.initialize(ctx)
|
|
|
|
if err != nil {
|
2018-12-12 13:15:34 +00:00
|
|
|
return Error.Wrap(err)
|
2018-11-14 01:22:18 +00:00
|
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := rollup.Run(ctx); err != nil {
|
|
|
|
defer cancel()
|
2019-01-16 19:30:33 +00:00
|
|
|
zap.L().Debug("Rollup is shutting down", zap.Error(err))
|
2018-11-14 01:22:18 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return server.Run(ctx)
|
|
|
|
}
|