From 153108a67f51d22f5248497611ce3c627dde80a7 Mon Sep 17 00:00:00 2001 From: Dasha Komsa Date: Thu, 1 Feb 2024 01:09:20 -0500 Subject: [PATCH] let ansible-runner shut down gracefully on timeout (#299) Signed-off-by: Dasha Komsa --- internal/ansible/ansible.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/internal/ansible/ansible.go b/internal/ansible/ansible.go index 89770bf..cb9fbc3 100644 --- a/internal/ansible/ansible.go +++ b/internal/ansible/ansible.go @@ -27,6 +27,7 @@ import ( "os/exec" "os/user" "path/filepath" + "time" "github.com/apenella/go-ansible/pkg/stdoutcallback/results" "github.com/crossplane-contrib/provider-ansible/apis/v1alpha1" @@ -358,6 +359,14 @@ func (r *Runner) Run() (*exec.Cmd, io.Reader, error) { dc.Stdout = stdoutWriter dc.Stderr = stderrWriter + // let the command shut down gracefully + dc.Cancel = func() error { + return dc.Process.Signal(os.Interrupt) + } + // if it doesn't respond to the SIGINT within 10s, + // it's going to be forcefully shut down with SIGKILL + dc.WaitDelay = 10 * time.Second + err := dc.Start() if err != nil { return nil, nil, err