Fix tests and issues

This commit is contained in:
Daniel
2024-11-08 14:39:58 +01:00
parent 2d0d711938
commit ddf7ba170e
10 changed files with 325 additions and 231 deletions

View File

@@ -130,6 +130,7 @@ func New(svcCfg *ServiceConfig) (*Instance, error) { //nolint:maintidx
dataDir: svcCfg.DataDir,
}
instance.ctx, instance.cancelCtx = context.WithCancel(context.Background())
instance.shutdownCtx, instance.cancelShutdownCtx = context.WithCancel(context.Background())
// Base modules
instance.base, err = base.New(instance)
@@ -651,24 +652,23 @@ func (i *Instance) shutdown(exitCode int) {
return
}
// Cancel main context.
i.cancelCtx()
// Set given exit code.
i.exitCode.Store(int32(exitCode))
// Cancel contexts.
i.cancelCtx()
defer i.cancelShutdownCtx()
// Start shutdown asynchronously in a separate manager.
m := mgr.New("instance")
m.Go("shutdown", func(w *mgr.WorkerCtx) error {
for {
if err := i.Stop(); err != nil {
w.Error("failed to shutdown", "err", err, "retry", "1s")
time.Sleep(1 * time.Second)
} else {
return nil
}
// Stop all modules.
if err := i.Stop(); err != nil {
w.Error("failed to shutdown", "err", err)
}
// Cancel shutdown process context.
i.cancelShutdownCtx()
return nil
})
}