storj/pkg/accounting/config.go

48 lines
1.1 KiB
Go
Raw Normal View History

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package accounting
import (
"context"
"time"
"go.uber.org/zap"
"storj.io/storj/pkg/kademlia"
"storj.io/storj/pkg/overlay"
"storj.io/storj/pkg/pointerdb"
"storj.io/storj/pkg/provider"
)
// Config contains configurable values accounting
type Config struct {
Interval time.Duration `help:"how frequently checker should audit segments" default:"30s"`
}
// Initialize a Accounting tally struct
func (c Config) initialize(ctx context.Context) (Tally, error) {
pointerdb := pointerdb.LoadFromContext(ctx)
overlay := overlay.LoadServerFromContext(ctx)
kademlia := kademlia.LoadFromContext(ctx)
return newTally(pointerdb, overlay, kademlia, 0, zap.L(), c.Interval), nil
}
// 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)
}