2018-11-08 16:18:28 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-11-14 01:22:18 +00:00
|
|
|
package tally
|
2018-11-08 16:18:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2018-11-29 18:39:27 +00:00
|
|
|
|
2018-11-26 21:49:55 +00:00
|
|
|
"storj.io/storj/pkg/accounting"
|
2018-11-08 16:18:28 +00:00
|
|
|
"storj.io/storj/pkg/kademlia"
|
|
|
|
"storj.io/storj/pkg/overlay"
|
|
|
|
"storj.io/storj/pkg/pointerdb"
|
|
|
|
"storj.io/storj/pkg/provider"
|
|
|
|
)
|
|
|
|
|
2018-11-14 01:22:18 +00:00
|
|
|
// Config contains configurable values for tally
|
2018-11-08 16:18:28 +00:00
|
|
|
type Config struct {
|
2018-11-26 21:49:55 +00:00
|
|
|
Interval time.Duration `help:"how frequently tally should run" default:"30s"`
|
|
|
|
DatabaseURL string `help:"the database connection string to use" default:"sqlite3://$CONFDIR/stats.db"`
|
2018-11-08 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 01:22:18 +00:00
|
|
|
// Initialize a tally struct
|
2018-11-08 16:18:28 +00:00
|
|
|
func (c Config) initialize(ctx context.Context) (Tally, error) {
|
|
|
|
pointerdb := pointerdb.LoadFromContext(ctx)
|
|
|
|
overlay := overlay.LoadServerFromContext(ctx)
|
|
|
|
kademlia := kademlia.LoadFromContext(ctx)
|
2018-11-26 21:49:55 +00:00
|
|
|
db, err := accounting.NewDb(c.DatabaseURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newTally(zap.L(), db, pointerdb, overlay, kademlia, 0, c.Interval)
|
2018-11-08 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run runs the tally with configured values
|
|
|
|
func (c Config) Run(ctx context.Context, server *provider.Provider) (err error) {
|
|
|
|
tally, err := c.initialize(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := tally.Run(ctx); err != nil {
|
|
|
|
defer cancel()
|
|
|
|
zap.L().Error("Error running tally", zap.Error(err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return server.Run(ctx)
|
|
|
|
}
|