Skip to content

Commit

Permalink
fix: graceful shutdown in http and wails modes
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Jan 19, 2024
1 parent c1687ba commit c958084
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 33 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Go to `/frontend`

`unset GTK_PATH && wails dev -tags "wails"`

_If you get a blank screen the first load, close the window and start the app again_

#### Wails Production build

`wails build -tags "wails"`
Expand Down
21 changes: 17 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

// TODO: move to service.go
func NewService(wg *sync.WaitGroup) *Service {
func NewService(ctx context.Context) *Service {
// Load config from environment variables / .env file
godotenv.Load(".env")
cfg := &Config{}
Expand Down Expand Up @@ -118,13 +118,16 @@ func NewService(wg *sync.WaitGroup) *Service {
}

log.Infof("Starting nostr-wallet-connect. npub: %s hex: %s", npub, identityPubkey)
ctx := context.Background()
ctx, _ = signal.NotifyContext(ctx, os.Interrupt)

var wg sync.WaitGroup
wg.Add(1)

svc := &Service{
cfg: cfg,
db: db,
ctx: ctx,
wg: &wg,
}

err = svc.setupDbConfig()
Expand Down Expand Up @@ -156,7 +159,9 @@ func NewService(wg *sync.WaitGroup) *Service {

relay, err := nostr.RelayConnect(ctx, cfg.Relay, nostr.WithNoticeHandler(svc.noticeHandler))
if err != nil {
svc.Logger.Fatal(err)
svc.Logger.Errorf("Failed to connect to relay: %v", err)
wg.Done()
return
}

//publish event with NIP-47 info
Expand Down Expand Up @@ -186,11 +191,19 @@ func NewService(wg *sync.WaitGroup) *Service {
//err being nil means that the context was canceled and we should exit the program.
break
}
svc.Logger.Info("Disconnecting from relay...")
err = relay.Close()
if err != nil {
svc.Logger.Error(err)
}
svc.Logger.Info("Graceful shutdown completed. Goodbye.")
if svc.lnClient != nil {
svc.Logger.Info("Shutting down LN backend...")
err = svc.lnClient.Shutdown()
if err != nil {
svc.Logger.Error(err)
}
}
svc.Logger.Info("Relay subroutine ended")
wg.Done()
}()

Expand Down
28 changes: 13 additions & 15 deletions main_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"context"
"fmt"
"net/http"
"sync"
"time"

echologrus "github.com/davrux/echo-logrus/v4"
Expand All @@ -21,10 +20,8 @@ import (
// this function will only be executed if no wails tag is set
func main() {
log.Info("NWC Starting in HTTP mode")
var wg sync.WaitGroup
wg.Add(1)

svc := NewService(&wg)
ctx := context.Background()
svc := NewService(ctx)

if svc.cfg.CookieSecret == "" {
svc.Logger.Fatalf("required key COOKIE_SECRET missing value")
Expand All @@ -38,16 +35,17 @@ func main() {
//start Echo server
go func() {
if err := e.Start(fmt.Sprintf(":%v", svc.cfg.Port)); err != nil && err != http.ErrServerClosed {
e.Logger.Fatal("shutting down the server")
e.Logger.Fatalf("shutting down the server: %v", err)
}
//handle graceful shutdown
<-svc.ctx.Done()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
e.Shutdown(ctx)
svc.Logger.Info("Echo server exited")
wg.Done()
}()

wg.Wait()
//handle graceful shutdown
<-svc.ctx.Done()
svc.Logger.Infof("Shutting down echo server...")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
e.Shutdown(ctx)
svc.Logger.Info("Echo server exited")
svc.Logger.Info("Waiting for service to exit...")
svc.wg.Wait()
svc.Logger.Info("Service exited")
}
24 changes: 12 additions & 12 deletions main_wails.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package main

import (
"sync"
"context"

log "github.com/sirupsen/logrus"
)
Expand All @@ -13,17 +13,17 @@ import (
// this function will only be executed if the wails tag is set
func main() {
log.Info("NWC Starting in WAILS mode")
var wg sync.WaitGroup
wg.Add(1)
ctx, cancel := context.WithCancel(context.Background())
svc := NewService(ctx)

svc := NewService(&wg)
app := NewApp(svc)
LaunchWailsApp(app)
svc.Logger.Info("Wails app exited")

go func() {
app := NewApp(svc)
LaunchWailsApp(app)
wg.Done()
svc.Logger.Info("Wails app exited")
}()

wg.Wait()
svc.Logger.Info("Cancelling service context...")
// cancel the service context
cancel()
svc.Logger.Info("Waiting for service to exit...")
svc.wg.Wait()
svc.Logger.Info("Service exited")
}
5 changes: 3 additions & 2 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"strings"
"sync"
"time"

"github.com/labstack/echo-contrib/session"
Expand All @@ -23,6 +24,7 @@ type Service struct {
ReceivedEOS bool
Logger *logrus.Logger
ctx context.Context
wg *sync.WaitGroup
}

func (svc *Service) GetUser(c echo.Context) (user *User, err error) {
Expand Down Expand Up @@ -123,7 +125,6 @@ func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscripti
}
}(event)
}
svc.Logger.Info("Subscription ended")
}()

select {
Expand All @@ -135,7 +136,7 @@ func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscripti
svc.Logger.Errorf("Subscription error %v", ctx.Err())
return ctx.Err()
}
svc.Logger.Info("Exiting subscription.")
svc.Logger.Info("Exiting subscription...")
return nil
}
}
Expand Down

0 comments on commit c958084

Please sign in to comment.