fix: kill log tailing properly on ctrl+c

This commit is contained in:
Jose Diaz-Gonzalez
2024-01-17 23:28:10 -05:00
parent 57b62f51d3
commit 3137c457fa

View File

@@ -392,13 +392,14 @@ func TriggerSchedulerEnter(scheduler string, appName string, processType string,
return fmt.Errorf("Error creating kubernetes client: %w", err) return fmt.Errorf("Error creating kubernetes client: %w", err)
} }
ctx := context.Background() ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(ctx) signals := make(chan os.Signal, 1)
cSignal := make(chan os.Signal, 2) signal.Notify(signals, os.Interrupt, syscall.SIGHUP,
signal.Notify(cSignal, os.Interrupt, syscall.SIGTERM) syscall.SIGINT,
syscall.SIGQUIT,
syscall.SIGTERM)
go func() { go func() {
<-cSignal <-signals
common.LogWarn("Exiting pod")
cancel() cancel()
}() }()
@@ -530,12 +531,14 @@ func TriggerSchedulerLogs(scheduler string, appName string, processType string,
return fmt.Errorf("Error creating kubernetes client: %w", err) return fmt.Errorf("Error creating kubernetes client: %w", err)
} }
ctx := context.Background() ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(ctx) signals := make(chan os.Signal, 1)
cSignal := make(chan os.Signal, 2) signal.Notify(signals, os.Interrupt, syscall.SIGHUP,
signal.Notify(cSignal, os.Interrupt, syscall.SIGTERM) syscall.SIGINT,
syscall.SIGQUIT,
syscall.SIGTERM)
go func() { go func() {
<-cSignal <-signals
cancel() cancel()
}() }()
@@ -567,6 +570,10 @@ func TriggerSchedulerLogs(scheduler string, appName string, processType string,
ch := make(chan bool) ch := make(chan bool)
podItems := podList.Items podItems := podList.Items
if os.Getenv("FORCE_COLOR") == "1" {
color.NoColor = false
}
colors := []color.Attribute{ colors := []color.Attribute{
color.FgRed, color.FgRed,
color.FgYellow, color.FgYellow,
@@ -596,30 +603,36 @@ func TriggerSchedulerLogs(scheduler string, appName string, processType string,
return err return err
} }
buffer := bufio.NewReader(podLogs) buffer := bufio.NewReader(podLogs)
go func(buffer *bufio.Reader, prettyText func(a ...interface{}) string, ch chan bool) { go func(ctx context.Context, buffer *bufio.Reader, prettyText func(a ...interface{}) string, ch chan bool) {
defer func() { defer func() {
ch <- true ch <- true
}() }()
for { for {
str, readErr := buffer.ReadString('\n') select {
if readErr == io.EOF { case <-ctx.Done(): // if cancel() execute
break ch <- true
}
if str == "" {
continue
}
if !quiet {
str = fmt.Sprintf("%s %s", dynoText(fmt.Sprintf("app[%s]:", podName)), str)
}
_, err := fmt.Print(str)
if err != nil {
return return
default:
str, readErr := buffer.ReadString('\n')
if readErr == io.EOF {
break
}
if str == "" {
continue
}
if !quiet {
str = fmt.Sprintf("%s %s", dynoText(fmt.Sprintf("app[%s]:", podName)), str)
}
_, err := fmt.Print(str)
if err != nil {
return
}
} }
} }
}(buffer, dynoText, ch) }(ctx, buffer, dynoText, ch)
} }
<-ch <-ch