diff --git a/internal/cli/main.go b/internal/cli/main.go index 63fc981..a03e46f 100644 --- a/internal/cli/main.go +++ b/internal/cli/main.go @@ -66,20 +66,20 @@ func Run(args []string) bool { gracefulStop := make(chan os.Signal, 1) signal.Notify(gracefulStop, syscall.SIGTERM, syscall.SIGINT) + wgch := make(chan struct{}) + go func() { + wg.Wait() + close(wgch) + }() + select { case <-ctx.Done(): cfg.Log().WithError(ctx.Err()).Info("Interrupt signal received") - case <-func() chan struct{} { - ch := make(chan struct{}) - go func() { - wg.Wait() - close(ch) - }() - return ch - }(): - cfg.Log().Warn("All services stopped") + stop() + <-wgch + case <-wgch: + cfg.Log().Warn("all services stopped") } - stop() return true } diff --git a/internal/cli/workers.go b/internal/cli/workers.go index 287162b..08d8ccc 100644 --- a/internal/cli/workers.go +++ b/internal/cli/workers.go @@ -51,7 +51,7 @@ func runServices(ctx context.Context, cfg config.Config, wg *sync.WaitGroup) { run(func() { leveljustice.Run(cfg, levelJustice) }) //service for cleaning daily question deadlines after day - run(func() { cleanquestiondeadlines.Run(cfg, cleanDQuestionDeadlines) }) + run(func() { cleanquestiondeadlines.Run(ctx, cfg, cleanDQuestionDeadlines) }) // service depends on all the workers for good UX <-expiryWatchSig diff --git a/internal/service/workers/cleanquestiondeadlines/main.go b/internal/service/workers/cleanquestiondeadlines/main.go index 925339d..084851d 100644 --- a/internal/service/workers/cleanquestiondeadlines/main.go +++ b/internal/service/workers/cleanquestiondeadlines/main.go @@ -1,12 +1,13 @@ package cleanquestiondeadlines import ( + "context" "time" "github.com/rarimo/geo-points-svc/internal/config" ) -func Run(cfg config.Config, sig chan struct{}) { +func Run(ctx context.Context, cfg config.Config, sig chan struct{}) { offset := cfg.DailyQuestions().Timezone for { @@ -30,6 +31,11 @@ func Run(cfg config.Config, sig chan struct{}) { cfg.Log().Info("Daily Question cleaning stop") timer.Stop() return + + case <-ctx.Done(): + cfg.Log().Info("Daily Question cleaning stop") + timer.Stop() + return } } }