-
Notifications
You must be signed in to change notification settings - Fork 402
/
service.go
64 lines (52 loc) · 1.53 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
// Package bandwidth implements bandwidth usage rollup loop.
package bandwidth
import (
"context"
"time"
"github.com/spacemonkeygo/monkit/v3"
"go.uber.org/zap"
"storj.io/common/sync2"
)
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 the bandwidth usage rollup service.
//
// architecture: Chore
type Service struct {
log *zap.Logger
db DB
Loop *sync2.Cycle
}
// NewService creates a new bandwidth service.
func NewService(log *zap.Logger, db DB, config Config) *Service {
return &Service{
log: log,
db: db,
Loop: sync2.NewCycle(config.Interval),
}
}
// 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
}