Skip to content

Commit

Permalink
fix: stop nostr when app is shutdown and use context to stop lnclient (
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz authored Jul 13, 2024
1 parent 831d7bf commit 7b9e4d5
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 53 deletions.
10 changes: 3 additions & 7 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,15 +314,11 @@ func (api *api) Stop() error {
return errors.New("LNClient not started")
}

// TODO: this should stop everything related to the lnclient
// stop the lnclient
// stop the lnclient, nostr relay etc.
// The user will be forced to re-enter their unlock password to restart the node
err := api.svc.StopLNClient()
if err != nil {
logger.Logger.WithError(err).Error("Failed to stop LNClient")
}
api.svc.StopApp()

return err
return nil
}

func (api *api) GetNodeConnectionInfo(ctx context.Context) (*lnclient.NodeConnectionInfo, error) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ func main() {
defer cancel()
e.Shutdown(ctx)
logger.Logger.Info("Echo server exited")
svc.WaitShutdown()
svc.Shutdown()
logger.Logger.Info("Service exited")
}
2 changes: 1 addition & 1 deletion main_wails.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ func main() {
logger.Logger.Info("Cancelling service context...")
// cancel the service context
cancel()
svc.WaitShutdown()
svc.Shutdown()
logger.Logger.Info("Service exited")
}
3 changes: 1 addition & 2 deletions service/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
type Service interface {
StartApp(encryptionKey string) error
StopApp()
StopLNClient() error
WaitShutdown()
Shutdown()

// TODO: remove getters (currently used by http / wails services)
GetAlbyOAuthSvc() alby.AlbyOAuthService
Expand Down
14 changes: 1 addition & 13 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,21 +203,14 @@ func finishRestoreNode(workDir string) {
}

func (svc *service) Shutdown() {
svc.StopLNClient()
svc.StopApp()
svc.eventPublisher.Publish(&events.Event{
Event: "nwc_stopped",
})
// wait for any remaining events
time.Sleep(1 * time.Second)
}

func (svc *service) StopApp() {
if svc.appCancelFn != nil {
svc.appCancelFn()
svc.wg.Wait()
}
}

func (svc *service) GetDB() *gorm.DB {
return svc.db
}
Expand Down Expand Up @@ -245,8 +238,3 @@ func (svc *service) GetLNClient() lnclient.LNClient {
func (svc *service) GetKeys() keys.Keys {
return svc.keys
}

func (svc *service) WaitShutdown() {
logger.Logger.Info("Waiting for service to exit...")
svc.wg.Wait()
}
33 changes: 24 additions & 9 deletions service/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/getAlby/hub/logger"
)

func (svc *service) StartNostr(ctx context.Context, encryptionKey string) error {
func (svc *service) startNostr(ctx context.Context, encryptionKey string) error {

relayUrl := svc.cfg.GetRelayUrl()

Expand All @@ -40,8 +40,10 @@ func (svc *service) StartNostr(ctx context.Context, encryptionKey string) error
"npub": npub,
"hex": svc.keys.GetNostrPublicKey(),
}).Info("Starting Alby Hub")
svc.wg.Add(1)
go func() {
svc.wg.Add(1)
// ensure the relay is properly disconnected before exiting
defer svc.wg.Done()
//Start infinite loop which will be only broken by canceling ctx (SIGINT)
var relay *nostr.Relay

Expand Down Expand Up @@ -95,9 +97,7 @@ func (svc *service) StartNostr(ctx context.Context, encryptionKey string) error
break
}
closeRelay(relay)
svc.Shutdown()
logger.Logger.Info("Relay subroutine ended")
svc.wg.Done()
}()
return nil
}
Expand All @@ -123,24 +123,38 @@ func (svc *service) StartApp(encryptionKey string) error {
return err
}

svc.StartNostr(ctx, encryptionKey)
err = svc.startNostr(ctx, encryptionKey)
if err != nil {
cancelFn()
return err
}

svc.appCancelFn = cancelFn

return nil
}

func (svc *service) launchLNBackend(ctx context.Context, encryptionKey string) error {
err := svc.StopLNClient()
if err != nil {
return err
if svc.lnClient != nil {
logger.Logger.Error("LNClient already started")
return errors.New("LNClient already started")
}

go func() {
// ensure the LNClient is stopped properly before exiting
svc.wg.Add(1)
<-ctx.Done()
svc.stopLNClient()
}()

lnBackend, _ := svc.cfg.Get("LNBackendType", "")
if lnBackend == "" {
return errors.New("no LNBackendType specified")
}

logger.Logger.Infof("Launching LN Backend: %s", lnBackend)
var lnClient lnclient.LNClient
var err error
switch lnBackend {
case config.LNDBackendType:
LNDAddress, _ := svc.cfg.Get("LNDAddress", encryptionKey)
Expand Down Expand Up @@ -183,6 +197,7 @@ func (svc *service) launchLNBackend(ctx context.Context, encryptionKey string) e
return err
}

svc.lnClient = lnClient
info, err := lnClient.GetInfo(ctx)
if err != nil {
logger.Logger.WithError(err).Error("Failed to fetch node info")
Expand All @@ -197,7 +212,7 @@ func (svc *service) launchLNBackend(ctx context.Context, encryptionKey string) e
"node_type": lnBackend,
},
})
svc.lnClient = lnClient

return nil
}

Expand Down
48 changes: 28 additions & 20 deletions service/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,36 @@ import (
"github.com/getAlby/hub/logger"
)

// TODO: this should happen on ctx.Done() rather than having to call manually
// see svc.appCancelFn and how svc.StartNostr works
func (svc *service) StopLNClient() error {
if svc.lnClient != nil {
logger.Logger.Info("Shutting down LN client")
err := svc.lnClient.Shutdown()
if err != nil {
logger.Logger.WithError(err).Error("Failed to stop LN client")
svc.eventPublisher.Publish(&events.Event{
Event: "nwc_node_stop_failed",
Properties: map[string]interface{}{
"error": fmt.Sprintf("%v", err),
},
})
return err
}
logger.Logger.Info("Publishing node shutdown event")
svc.lnClient = nil
func (svc *service) StopApp() {
if svc.appCancelFn != nil {
logger.Logger.Info("Stopping app...")
svc.appCancelFn()
svc.wg.Wait()
logger.Logger.Info("app stopped")
}
}

func (svc *service) stopLNClient() {
defer svc.wg.Done()
if svc.lnClient == nil {
return
}
logger.Logger.Info("Shutting down LN client")
err := svc.lnClient.Shutdown()
if err != nil {
logger.Logger.WithError(err).Error("Failed to stop LN client")
svc.eventPublisher.Publish(&events.Event{
Event: "nwc_node_stopped",
Event: "nwc_node_stop_failed",
Properties: map[string]interface{}{
"error": fmt.Sprintf("%v", err),
},
})
return
}
logger.Logger.Info("Publishing node shutdown event")
svc.lnClient = nil
svc.eventPublisher.Publish(&events.Event{
Event: "nwc_node_stopped",
})
logger.Logger.Info("LNClient stopped successfully")
return nil
}

0 comments on commit 7b9e4d5

Please sign in to comment.