2019-07-29 15:07:52 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
// Package bandwidth implements bandwidth usage rollup loop.
|
|
|
|
package bandwidth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2019-11-08 20:40:39 +00:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2019-07-29 15:07:52 +01:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/sync2"
|
2019-07-29 15:07:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var mon = monkit.Package()
|
|
|
|
|
|
|
|
// Config defines parameters for storage node Collector.
|
|
|
|
type Config struct {
|
|
|
|
Interval time.Duration `help:"how frequently bandwidth usage rollups are calculated" default:"1h0m0s"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Service implements
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Chore
|
2019-07-29 15:07:52 +01:00
|
|
|
type Service struct {
|
|
|
|
log *zap.Logger
|
|
|
|
db DB
|
2020-01-29 15:37:50 +00:00
|
|
|
Loop *sync2.Cycle
|
2019-07-29 15:07:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService creates a new bandwidth service.
|
|
|
|
func NewService(log *zap.Logger, db DB, config Config) *Service {
|
|
|
|
return &Service{
|
|
|
|
log: log,
|
|
|
|
db: db,
|
2020-01-29 15:37:50 +00:00
|
|
|
Loop: sync2.NewCycle(config.Interval),
|
2019-07-29 15:07:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts the background process for rollups of bandwidth usage
|
|
|
|
func (service *Service) Run(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return service.Loop.Run(ctx, service.Rollup)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rollup calls bandwidth DB Rollup method and logs any errors
|
|
|
|
func (service *Service) Rollup(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
service.log.Info("Performing bandwidth usage rollups")
|
|
|
|
err = service.db.Rollup(ctx)
|
|
|
|
if err != nil {
|
|
|
|
service.log.Error("Could not rollup bandwidth usage", zap.Error(err))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close stops the background process for rollups of bandwidth usage
|
|
|
|
func (service *Service) Close() (err error) {
|
|
|
|
service.Loop.Close()
|
|
|
|
return nil
|
|
|
|
}
|