Skip to content

Commit 3769467

Browse files
committed
subservers+terminal: integrate status server
Pass the status manager to the sub-server manager so that the sub-server manager can inform the status manager of any sub-servers that have started or failed to start.
1 parent 4e2ec08 commit 3769467

File tree

3 files changed

+44
-31
lines changed

3 files changed

+44
-31
lines changed

subservers/manager.go

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
restProxy "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
1111
"github.com/lightninglabs/lightning-terminal/perms"
12+
"github.com/lightninglabs/lightning-terminal/status"
1213
"github.com/lightninglabs/lndclient"
1314
"github.com/lightningnetwork/lnd/lncfg"
1415
"github.com/lightningnetwork/lnd/lnrpc"
@@ -32,20 +33,27 @@ var (
3233

3334
// Manager manages a set of subServer objects.
3435
type Manager struct {
35-
servers []*subServerWrapper
36-
permsMgr *perms.Manager
37-
mu sync.RWMutex
36+
servers []*subServerWrapper
37+
permsMgr *perms.Manager
38+
statusServer *status.Manager
39+
mu sync.RWMutex
3840
}
3941

40-
// NewManager constructs a new subServerMgr.
41-
func NewManager(permsMgr *perms.Manager) *Manager {
42+
// NewManager constructs a new Manager.
43+
func NewManager(permsMgr *perms.Manager,
44+
statusServer *status.Manager) *Manager {
45+
4246
return &Manager{
43-
permsMgr: permsMgr,
47+
permsMgr: permsMgr,
48+
statusServer: statusServer,
4449
}
4550
}
4651

4752
// AddServer adds a new subServer to the manager's set.
4853
func (s *Manager) AddServer(ss SubServer, enable bool) {
54+
// Register all sub-servers with the status server.
55+
s.statusServer.RegisterSubServer(ss.Name())
56+
4957
// If the sub-server has explicitly been disabled, then we don't add it
5058
// to the set of servers tracked by the Manager.
5159
if !enable {
@@ -65,12 +73,15 @@ func (s *Manager) AddServer(ss SubServer, enable bool) {
6573
s.permsMgr.RegisterSubServer(
6674
ss.Name(), ss.Permissions(), ss.WhiteListedURLs(),
6775
)
76+
77+
// Mark the sub-server as enabled with the status manager.
78+
s.statusServer.SetEnabled(ss.Name())
6879
}
6980

7081
// StartIntegratedServers starts all the manager's sub-servers that should be
7182
// started in integrated mode.
7283
func (s *Manager) StartIntegratedServers(lndClient lnrpc.LightningClient,
73-
lndGrpc *lndclient.GrpcLndServices, withMacaroonService bool) error {
84+
lndGrpc *lndclient.GrpcLndServices, withMacaroonService bool) {
7485

7586
s.mu.Lock()
7687
defer s.mu.Unlock()
@@ -82,19 +93,24 @@ func (s *Manager) StartIntegratedServers(lndClient lnrpc.LightningClient,
8293

8394
err := ss.startIntegrated(
8495
lndClient, lndGrpc, withMacaroonService,
96+
func(err error) {
97+
s.statusServer.SetErrored(
98+
ss.Name(), err.Error(),
99+
)
100+
},
85101
)
86102
if err != nil {
87-
return fmt.Errorf("unable to start %v in integrated "+
88-
"mode: %v", ss.Name(), err)
103+
s.statusServer.SetErrored(ss.Name(), err.Error())
104+
continue
89105
}
90-
}
91106

92-
return nil
107+
s.statusServer.SetRunning(ss.Name())
108+
}
93109
}
94110

95111
// ConnectRemoteSubServers creates connections to all the manager's sub-servers
96112
// that are running remotely.
97-
func (s *Manager) ConnectRemoteSubServers() error {
113+
func (s *Manager) ConnectRemoteSubServers() {
98114
s.mu.Lock()
99115
defer s.mu.Unlock()
100116

@@ -105,12 +121,12 @@ func (s *Manager) ConnectRemoteSubServers() error {
105121

106122
err := ss.connectRemote()
107123
if err != nil {
108-
return fmt.Errorf("failed to connect to remote %s: %v",
109-
ss.Name(), err)
124+
s.statusServer.SetErrored(ss.Name(), err.Error())
125+
continue
110126
}
111-
}
112127

113-
return nil
128+
s.statusServer.SetRunning(ss.Name())
129+
}
114130
}
115131

116132
// RegisterRPCServices registers all the manager's sub-servers with the given
@@ -301,6 +317,8 @@ func (s *Manager) Stop() error {
301317
log.Errorf("Error stopping %s: %v", ss.Name(), err)
302318
returnErr = err
303319
}
320+
321+
s.statusServer.SetStopped(ss.Name())
304322
}
305323

306324
return returnErr

subservers/subserver.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ func (s *subServerWrapper) stop() error {
9999

100100
// startIntegrated starts the subServer in integrated mode.
101101
func (s *subServerWrapper) startIntegrated(lndClient lnrpc.LightningClient,
102-
lndGrpc *lndclient.GrpcLndServices, withMacaroonService bool) error {
102+
lndGrpc *lndclient.GrpcLndServices, withMacaroonService bool,
103+
onError func(error)) error {
103104

104105
err := s.Start(lndClient, lndGrpc, withMacaroonService)
105106
if err != nil {
@@ -121,11 +122,11 @@ func (s *subServerWrapper) startIntegrated(lndClient lnrpc.LightningClient,
121122
// happens. We don't need to try to stop it again.
122123
s.setStarted(false)
123124

124-
err = fmt.Errorf("received critical error from "+
125-
"sub-server (%s), shutting down: %v",
126-
s.Name(), err)
127-
128-
log.Error(err)
125+
onError(
126+
fmt.Errorf("received critical error from "+
127+
"sub-server (%s), shutting down: %v",
128+
s.Name(), err),
129+
)
129130

130131
case <-s.quit:
131132
}

terminal.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func (g *LightningTerminal) Run() error {
234234

235235
// Create the instances of our subservers now so we can hook them up to
236236
// lnd once it's fully started.
237-
g.subServerMgr = subservers.NewManager(g.permsMgr)
237+
g.subServerMgr = subservers.NewManager(g.permsMgr, g.statusMgr)
238238

239239
// Register our sub-servers. This must be done before the REST proxy is
240240
// set up so that the correct REST handlers are registered.
@@ -511,10 +511,7 @@ func (g *LightningTerminal) start() error {
511511

512512
// Initialise any connections to sub-servers that we are running in
513513
// remote mode.
514-
if err := g.subServerMgr.ConnectRemoteSubServers(); err != nil {
515-
return fmt.Errorf("error connecting to remote sub-servers: %v",
516-
err)
517-
}
514+
g.subServerMgr.ConnectRemoteSubServers()
518515

519516
// bakeSuperMac is a closure that can be used to bake a new super
520517
// macaroon that contains all active permissions.
@@ -615,12 +612,9 @@ func (g *LightningTerminal) start() error {
615612

616613
// Both connection types are ready now, let's start our sub-servers if
617614
// they should be started locally as an integrated service.
618-
err = g.subServerMgr.StartIntegratedServers(
615+
g.subServerMgr.StartIntegratedServers(
619616
g.basicClient, g.lndClient, createDefaultMacaroons,
620617
)
621-
if err != nil {
622-
return fmt.Errorf("could not start subservers: %v", err)
623-
}
624618

625619
err = g.startInternalSubServers(createDefaultMacaroons)
626620
if err != nil {

0 commit comments

Comments
 (0)