-
Notifications
You must be signed in to change notification settings - Fork 19
/
services_manager.go
163 lines (132 loc) · 3.98 KB
/
services_manager.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package api
import (
"context"
"errors"
"fmt"
"sync"
"time"
"code.vegaprotocol.io/vega/libs/ptr"
"code.vegaprotocol.io/vega/wallet/api/session"
)
var ErrNoServiceIsRunningForThisNetwork = errors.New("no service is running for this network")
type RunningService struct {
url string
shutdownSwitch *ServiceShutdownSwitch
sessions *session.Sessions
}
// ServicesManager keeps track of all running services. It is used by the
// admin API to control the lifecycle of the services and query their state.
type ServicesManager struct {
servicesByNetwork map[string]*RunningService
mu sync.Mutex
tokenStore TokenStore
walletStore WalletStore
}
func (ns *ServicesManager) RegisterService(network, url string, sessions *session.Sessions, shutdownSwitch *ServiceShutdownSwitch) error {
ns.mu.Lock()
defer ns.mu.Unlock()
if svc, ok := ns.servicesByNetwork[network]; ok {
return fmt.Errorf("a service is already running for this network at %q", svc.url)
}
tokens, err := ns.tokenStore.ListTokens()
if err != nil {
return fmt.Errorf("could not retrieve the list of registered tokens: %w", err)
}
for _, token := range tokens {
tokenInfo, err := ns.tokenStore.GetToken(token.Token)
if err != nil {
return fmt.Errorf("could not retrieve the information linked to the token %q: %w", token.Token, err)
}
w, err := ns.walletStore.GetWallet(context.Background(), tokenInfo.Wallet.Name, tokenInfo.Wallet.Passphrase)
if err != nil {
return fmt.Errorf("could not retrieve the wallet %q associated to the token %q: %w", tokenInfo.Wallet.Name, token.Token, err)
}
var expiry *time.Time
if tokenInfo.Expiry != nil {
expiry = ptr.From(time.Unix(*tokenInfo.Expiry, 0))
}
if err := sessions.ConnectWalletForLongLivingConnection(token.Token, w, time.Now(), expiry); err != nil {
return fmt.Errorf("could not connect the wallet %q: %w", tokenInfo.Wallet.Name, err)
}
}
ns.servicesByNetwork[network] = &RunningService{
url: url,
shutdownSwitch: shutdownSwitch,
sessions: sessions,
}
return nil
}
func (ns *ServicesManager) Sessions(network string) (*session.Sessions, error) {
ns.mu.Lock()
defer ns.mu.Unlock()
service, ok := ns.servicesByNetwork[network]
if !ok {
return nil, ErrNoServiceIsRunningForThisNetwork
}
return service.sessions, nil
}
func (ns *ServicesManager) AbortService(network string, err error) {
ns.mu.Lock()
defer ns.mu.Unlock()
service, ok := ns.servicesByNetwork[network]
if !ok {
return
}
service.shutdownSwitch.Flip(err)
service.sessions.DisconnectAllWallets()
delete(ns.servicesByNetwork, network)
}
func (ns *ServicesManager) StopService(network string) {
ns.AbortService(network, nil)
}
func NewServicesManager(tokenStore TokenStore, walletStore WalletStore) *ServicesManager {
return &ServicesManager{
servicesByNetwork: map[string]*RunningService{},
tokenStore: tokenStore,
walletStore: walletStore,
}
}
type ServiceShutdownSwitch struct {
ctx context.Context
cFunc context.CancelFunc
wg *sync.WaitGroup
onErrorFunc func(error)
beforeCancel func()
}
func (s *ServiceShutdownSwitch) Ctx() context.Context {
return s.ctx
}
func (s *ServiceShutdownSwitch) Flip(err error) {
if err != nil && s.onErrorFunc != nil {
s.onErrorFunc(err)
}
if s.beforeCancel != nil {
s.beforeCancel()
}
s.cFunc()
}
func (s *ServiceShutdownSwitch) Flipped() <-chan struct{} {
return s.ctx.Done()
}
func (s *ServiceShutdownSwitch) BindToProcess() ProcessStoppedNotifier {
s.wg.Add(1)
return func() {
s.wg.Done()
}
}
func (s *ServiceShutdownSwitch) WaitForShutdown() {
s.wg.Wait()
}
func (s *ServiceShutdownSwitch) BeforeCancelFunc(f func()) {
s.beforeCancel = f
}
func NewServiceShutdownSwitch(onErrorFunc func(error)) *ServiceShutdownSwitch {
ctx, cFunc := context.WithCancel(context.Background())
return &ServiceShutdownSwitch{
ctx: ctx,
cFunc: cFunc,
wg: &sync.WaitGroup{},
beforeCancel: nil,
onErrorFunc: onErrorFunc,
}
}