diff options
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 43 |
1 files changed, 17 insertions, 26 deletions
@@ -10,7 +10,6 @@ import ( "os" "os/signal" "syscall" - "time" log "github.com/sirupsen/logrus" "gopkg.in/yaml.v2" @@ -176,44 +175,36 @@ func main() { reg_file := readRegistration(config.Registration) registration = ®_file - // Start appservice and web management interface + // Create context and handlers for errors and signals + ctx, stop_all := context.WithCancel(context.Background()) errch := make(chan error) sigch := make(chan os.Signal) signal.Notify(sigch, os.Interrupt, syscall.SIGTERM) + defer func() { + signal.Stop(sigch) + stop_all() + }() - as_server, err := StartAppService(errch) + // Start appservice and web server + _, err = StartAppService(errch, ctx) if err != nil { log.Fatal(err) } - web_server := StartWeb(errch) + _ = StartWeb(errch, ctx) // Wait for an error somewhere or interrupt signal select { case err = <-errch: - if err != nil { - log.Error(err) - } + log.Error(err) + stop_all() case sig := <-sigch: - log.Warnf("Got signal %s", sig.String()) + log.Warnf("Got signal: %s", sig.String()) + stop_all() + case <-ctx.Done(): } - // Shut down, hopefully this is not a too bad way to do it - log.Warn("Shuttind down") - delay := 2 * time.Second - - ctx1, _ := context.WithTimeout(context.TODO(), delay) - go as_server.Shutdown(ctx1) - - ctx2, _ := context.WithTimeout(context.TODO(), delay) - go web_server.Shutdown(ctx2) - - time.Sleep(delay) - CloseAllAcountsForShutdown() - - if err != nil { - os.Exit(1) - } else { - os.Exit(0) - } + log.Info("Closing all account connections...") + CloseAllAccountsForShutdown() + log.Info("Exiting.") } |