From 95fe26f3e3080ef0ee7104940dce5a3a4dc009c8 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Thu, 9 Jul 2020 19:16:52 +0200 Subject: [PATCH] keep track of services errors So we can exit with the correct code if an error happen inside the services goroutines Fixes #143 --- cmd/serve.go | 4 +++- service/service.go | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/cmd/serve.go b/cmd/serve.go index 14dc6659..b16d780d 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -34,7 +34,9 @@ Please take a look at the usage below to customize the startup options`, } if err := service.Start(); err == nil { service.Wait() - os.Exit(0) + if service.Error == nil { + os.Exit(0) + } } os.Exit(1) }, diff --git a/service/service.go b/service/service.go index 264b95a0..3587216f 100644 --- a/service/service.go +++ b/service/service.go @@ -35,6 +35,7 @@ type Service struct { LogVerbose bool Profiler bool Shutdown chan bool + Error error } // Start initializes the service @@ -92,6 +93,7 @@ func (s *Service) Start() error { if err := sftpdConf.Initialize(s.ConfigDir); err != nil { logger.Error(logSender, "", "could not start SFTP server: %v", err) logger.ErrorToConsole("could not start SFTP server: %v", err) + s.Error = err } s.Shutdown <- true }() @@ -101,6 +103,7 @@ func (s *Service) Start() error { if err := httpdConf.Initialize(s.ConfigDir, s.Profiler); err != nil { logger.Error(logSender, "", "could not start HTTP server: %v", err) logger.ErrorToConsole("could not start HTTP server: %v", err) + s.Error = err } s.Shutdown <- true }()