-
Notifications
You must be signed in to change notification settings - Fork 390
/
version.go
60 lines (47 loc) · 1.36 KB
/
version.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package stripecoinpayments
import (
"context"
"time"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/common/sync2"
)
// ErrVersion defines version service error.
var ErrVersion = errs.Class("version service error")
// VersionService updates conversion rates in a loop.
//
// architecture: Service
type VersionService struct {
log *zap.Logger
service *Service
Cycle sync2.Cycle
}
// NewVersionService creates new instance of VersionService.
func NewVersionService(log *zap.Logger, service *Service, interval time.Duration) *VersionService {
return &VersionService{
log: log,
service: service,
Cycle: *sync2.NewCycle(interval),
}
}
// Run runs loop which updates conversion rates for service.
func (version *VersionService) Run(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
return ErrVersion.Wrap(version.Cycle.Run(ctx,
func(ctx context.Context) error {
version.log.Debug("running conversion rates update cycle")
if err := version.service.UpdateRates(ctx); err != nil {
version.log.Error("conversion rates update cycle failed", zap.Error(ErrChore.Wrap(err)))
}
return nil
},
))
}
// Close closes underlying cycle.
func (version *VersionService) Close() (err error) {
defer mon.Task()(nil)(&err)
version.Cycle.Close()
return nil
}