Skip to content

Commit

Permalink
refactor(cli): refactor signal handling and context management in CLI
Browse files Browse the repository at this point in the history
- Introduced context management with cancellation in Run method.
- Refactored signal handling to properly clean up and stop goroutines.
- Added context cancellation to stop the readiness signaling to systemd.
  • Loading branch information
dyudin0821 committed Jun 27, 2024
1 parent d56853c commit 222ef7d
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package main

import (
"context"
"flag"
"fmt"
"io"
Expand Down Expand Up @@ -106,6 +107,10 @@ func (cli *CLI) Run(args []string) int {
return ExitCodeOK
}

// Create a context with cancellation
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Create a channel for signaling readiness to the systemd init system.
readyCh := make(chan struct{})

Expand All @@ -114,10 +119,15 @@ func (cli *CLI) Run(args []string) int {
// the application is ready by calling daemon.SdNotify with SdNotifyReady.
// If an error occurs during the notification, it logs a warning message.
go func() {
for range readyCh {
_, err := daemon.SdNotify(false, daemon.SdNotifyReady)
if err != nil {
log.Printf("[WARN] failed to signal readiness to systemd: %v", err)
for {
select {
case <-readyCh:
_, err := daemon.SdNotify(false, daemon.SdNotifyReady)
if err != nil {
log.Printf("[WARN] failed to signal readiness to systemd: %v", err)
}
case <-ctx.Done():
return
}
}
}()
Expand Down Expand Up @@ -156,6 +166,7 @@ func (cli *CLI) Run(args []string) int {
case <-service_os.Shutdown_Channel():
fmt.Fprintf(cli.errStream, "Cleaning up...\n")
runner.StopImmediately()
cancel() // Cancel the context to stop the readyCh goroutine
return ExitCodeInterrupt
case s := <-cli.signalCh:
log.Printf("[DEBUG] (cli) receiving signal %q", s)
Expand Down Expand Up @@ -187,12 +198,14 @@ func (cli *CLI) Run(args []string) int {
case *config.KillSignal:
fmt.Fprintf(cli.errStream, "Cleaning up...\n")
runner.StopImmediately()
cancel() // Cancel the context to stop the readyCh goroutine
return ExitCodeInterrupt
default:
// Propagate the signal to the child process
runner.Signal(s)
}
case <-cli.stopCh:
cancel() // Cancel the context to stop the readyCh goroutine
return ExitCodeOK
}
}
Expand Down

0 comments on commit 222ef7d

Please sign in to comment.