Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandr Stefurishin <[email protected]>
  • Loading branch information
Alexandr Stefurishin committed Dec 10, 2024
1 parent 10b6d59 commit c0dc9e9
Showing 1 changed file with 20 additions and 57 deletions.
77 changes: 20 additions & 57 deletions images/tlshd/src/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,28 @@ func (o *Opt) Parse() {
func main() {
opt.Parse()

// Create a base context
ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)

args := []string{"-s"}
if opt.TimeoutWait > 0 {
args = append(args, "-c", "/opt/deckhouse/csi/etc/tlshd.conf")

tctx, cancel := context.WithTimeout(ctx, time.Duration(opt.TimeoutWait)*time.Second)
defer cancel()
ctx = tctx

log.Printf("Timeout specified: %d seconds", opt.TimeoutWait)

} else {
args = append(args, "-c", "/etc/tlshd.conf")
log.Println("No timeout specified. Process will run indefinitely until it finishes or receives a termination signal.")
}
cmd := exec.Command("/opt/deckhouse/csi/bin/tlshd", args...)

cmd := exec.CommandContext(ctx, "/opt/deckhouse/csi/bin/tlshd", args...)

var stderrBuf bytes.Buffer
// MultiWriter to write to both os.Stderr and the buffer
//
// --- because there is no non-zero return code ---
// # ./src/tlshd/tlshd -s -c ./src/tlshd/tlshd.conf; echo $?
// tlshd[147529]: Built from ktls-utils 0.11 on Nov 20 2024 12:52:22
Expand All @@ -68,62 +79,14 @@ func main() {
cmd.Stderr = multiStderr
cmd.Stdout = os.Stdout

err := cmd.Start()
if err != nil {
log.Fatalf("Failed to start the command: %v", err)
}
log.Printf("Process started with PID: %d", cmd.Process.Pid)

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

// If a timeout is specified, wrap the context with a timeout
if opt.TimeoutWait > 0 {
ctx, cancel = context.WithTimeout(ctx, time.Duration(opt.TimeoutWait)*time.Second)
// defer cancel()
log.Printf("Timeout specified: %d seconds", opt.TimeoutWait)
} else {
log.Println("No timeout specified. Process will run indefinitely until it finishes or receives a termination signal.")
// run
if err := cmd.Run(); err != nil {
log.Fatalf("Failed to run process: %v", err)
}

// Channel to handle process completion
done := make(chan error, 1)

// Start a goroutine to monitor when the process finishes
go func() {
done <- cmd.Wait()
}()

// Channel to handle OS signals
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)

select {
case <-ctx.Done(): // Timeout or cancellation occurred
log.Println("Context done (timeout or cancellation), terminating the process...")
if err := cmd.Process.Kill(); err != nil {
cancel()
log.Fatalf("Failed to terminate the process: %v", err)
}
log.Println("Process has been terminated.")
case err := <-done: // Process finished on its own
if err != nil {
cancel()
log.Fatalf("Process exited with an error: %v", err)
}

r := regexp.MustCompile(`\s+is\s+not\s+available`)
if r.Match(stderrBuf.Bytes()) {
os.Exit(1)
}

log.Println("Process exited successfully.")
case sig := <-signalChan: // Received termination signal
log.Printf("Received signal: %v, terminating the process...", sig)
if err := cmd.Process.Kill(); err != nil {
log.Fatalf("Failed to terminate the process: %v", err)
}
log.Println("Process has been terminated.")
r := regexp.MustCompile(`\s+is\s+not\s+available`)
if r.Match(stderrBuf.Bytes()) {
os.Exit(1)
}
log.Printf("success")
}

0 comments on commit c0dc9e9

Please sign in to comment.