From 30844f08043500e372b02ee93e037ca633964c02 Mon Sep 17 00:00:00 2001 From: Scott Leggett Date: Fri, 19 Apr 2024 20:25:57 +0800 Subject: [PATCH] fix: exit gracefully on SIGTERM --- cmd/lagoon-opensearch-sync/sync.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/cmd/lagoon-opensearch-sync/sync.go b/cmd/lagoon-opensearch-sync/sync.go index fb47e08..4451cd6 100644 --- a/cmd/lagoon-opensearch-sync/sync.go +++ b/cmd/lagoon-opensearch-sync/sync.go @@ -86,13 +86,17 @@ func (cmd *SyncCmd) Run(log *zap.Logger) error { return nil } // continue running in a loop - tick := time.NewTicker(cmd.Period) - for range tick.C { - err = sync.Sync(ctx, log, l, k, o, d, cmd.DryRun, cmd.Objects, - cmd.LegacyIndexPatternDelimiter) - if err != nil { - return err + ticker := time.NewTicker(cmd.Period) + for { + select { + case <-ctx.Done(): + return nil + case <-ticker.C: + err = sync.Sync(ctx, log, l, k, o, d, cmd.DryRun, cmd.Objects, + cmd.LegacyIndexPatternDelimiter) + if err != nil { + return err + } } } - return nil }