Skip to content

Commit

Permalink
fix: migrate node (#485)
Browse files Browse the repository at this point in the history
Fixes #472

We weren't shutting down the DB correctly which didn't save the full
write-ahead log to the DB. This was causing issues when importing - it
was using the old -shm and -wal files which were causing the imported
nwc.db to be discarded.

- ensure full wal checkpoint is executed on shutdown
- correctly shutdown DB on normal exit
- shutdown service before exiting on backup restore
- remove JWT middleware from backup endpoint
- delete old nwc db files before backup restore to ensure restored files
are used

Tests:
- [x] Http Mode - Backup
- [x] Http Mode - Restore
- [x] Wails mode - Backup
- [x] Wails mode - Restore
  • Loading branch information
im-adithya authored Aug 14, 2024
2 parents 65ab5fe + 9cbeeb3 commit f232eba
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
9 changes: 9 additions & 0 deletions api/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,15 @@ func (api *api) RestoreBackup(unlockPassword string, r io.Reader) error {

go func() {
logger.Logger.Info("Backup restored. Shutting down Alby Hub...")
api.svc.Shutdown()
// ensure no -shm or -wal files exist as they will stop the restore
for _, filename := range []string{"nwc.db", "nwc.db-shm", "nwc.db-wal"} {
err = os.Remove(filepath.Join(workDir, filename))
if err != nil {
logger.Logger.WithError(err).WithField("filename", filename).Error("failed to remove old nwc db file before restore")
}
}

// schedule node shutdown after a few seconds to ensure frontend updates
time.Sleep(5 * time.Second)
os.Exit(0)
Expand Down
5 changes: 5 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ func Stop(db *gorm.DB) error {
return fmt.Errorf("failed to get database connection: %w", err)
}

err = db.Exec("PRAGMA wal_checkpoint(FULL)", nil).Error
if err != nil {
logger.Logger.WithError(err).Error("Failed to execute wal endpoint")
}

err = sqlDB.Close()
if err != nil {
return fmt.Errorf("failed to close database connection: %w", err)
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/screens/BackupNodeSuccess.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export function BackupNodeSuccess() {
<PowerOff className="w-6 h-6" />
</div>
<span>
Alby Hub has now shut down.{" "}
This Alby Hub has is now in a halted state to prevent further
changes.{" "}
<b>
Do not restart it otherwise your backup will be invalidated.
</b>
Expand Down
2 changes: 1 addition & 1 deletion http/http_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func (httpSvc *HttpService) RegisterSharedRoutes(e *echo.Echo) {
e.POST("/api/start", httpSvc.startHandler, unlockRateLimiter)
e.POST("/api/unlock", httpSvc.unlockHandler, unlockRateLimiter)
e.PATCH("/api/unlock-password", httpSvc.changeUnlockPasswordHandler, unlockRateLimiter)
e.POST("/api/backup", httpSvc.createBackupHandler, unlockRateLimiter)

frontend.RegisterHandlers(e)

Expand Down Expand Up @@ -146,7 +147,6 @@ func (httpSvc *HttpService) RegisterSharedRoutes(e *echo.Echo) {
restrictedGroup.POST("/api/send-payment-probes", httpSvc.sendPaymentProbesHandler)
restrictedGroup.POST("/api/send-spontaneous-payment-probes", httpSvc.sendSpontaneousPaymentProbesHandler)
restrictedGroup.GET("/api/log/:type", httpSvc.getLogOutputHandler)
restrictedGroup.POST("/api/backup", httpSvc.createBackupHandler)

httpSvc.albyHttpSvc.RegisterSharedRoutes(restrictedGroup, e)
}
Expand Down
1 change: 1 addition & 0 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ func (svc *service) Shutdown() {
svc.eventPublisher.Publish(&events.Event{
Event: "nwc_stopped",
})
db.Stop(svc.db)
// wait for any remaining events
time.Sleep(1 * time.Second)
}
Expand Down

0 comments on commit f232eba

Please sign in to comment.