Skip to content

Commit

Permalink
feat: Add graceful shutdown handling in notification (argoproj#19368)
Browse files Browse the repository at this point in the history
Signed-off-by: LeeSeolHui <lsh81550@gmail.com>
  • Loading branch information
D0ri123 authored Sep 12, 2024
1 parent 022c4fd commit c4709fb
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions cmd/argocd-notification/commands/controller.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package commands

import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"

"github.com/argoproj/argo-cd/v2/common"
"github.com/argoproj/argo-cd/v2/reposerver/apiclient"
Expand Down Expand Up @@ -62,7 +66,8 @@ func NewCommand() *cobra.Command {
Use: "controller",
Short: "Starts Argo CD Notifications controller",
RunE: func(c *cobra.Command, args []string) error {
ctx := c.Context()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

vers := common.GetVersion()
namespace, _, err := clientConfig.Namespace()
Expand Down Expand Up @@ -146,8 +151,18 @@ func NewCommand() *cobra.Command {
return fmt.Errorf("failed to initialize controller: %w", err)
}

sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
s := <-sigCh
log.Printf("got signal %v, attempting graceful shutdown", s)
cancel()
}()

go ctrl.Run(ctx, processorsCount)
<-ctx.Done()
return nil
},
}
Expand Down

0 comments on commit c4709fb

Please sign in to comment.