Skip to content

Commit

Permalink
handle cancellation on facts commands
Browse files Browse the repository at this point in the history
  • Loading branch information
balanza committed Jan 8, 2025
1 parent be1cbcc commit 4243bf3
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion cmd/facts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package cmd

import (
"context"
"os"
"os/signal"
"syscall"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -108,7 +113,27 @@ func gather(cmd *cobra.Command, _ []string) {
},
}

value, err := g.Gather(cmd.Context(), factRequest)
ctx, cancel := context.WithCancel(cmd.Context())

signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
cancelled := false
go func() {
<-signals
log.Info("Caught signal!")
cancelled = true
cancel()

}()

value, err := g.Gather(ctx, factRequest)

if cancelled {
gatherers.CleanupPlugins()
log.Info("Gathering cancelled")
return
}

if err != nil {
log.Errorf("Error gathering fact \"%s\" with argument \"%s\"", gatherer, argument)
cleanupAndFatal(err)
Expand Down

0 comments on commit 4243bf3

Please sign in to comment.