From 29487ddcef3584cb6fb90b36fc4f3869e0610cda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A0=20Serra=20Rigo?= Date: Mon, 22 Jun 2020 09:57:09 +0200 Subject: [PATCH] Command time to live can be configured now, if not defined the default is the same as before: 10m --- outputs/cmd/cmd.go | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/outputs/cmd/cmd.go b/outputs/cmd/cmd.go index c3b3d75..58b3302 100644 --- a/outputs/cmd/cmd.go +++ b/outputs/cmd/cmd.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "log" "os/exec" "regexp" "strconv" @@ -14,19 +15,20 @@ import ( "github.com/savaki/jq" ) -var ( - maximumCmdTimeLive = 10 * time.Minute +const ( + defaultMaximumCmdTimeLive = 10 * time.Minute ) // Cmd is the command struct that will be executed after recive the order // from the input plugins type Cmd struct { - r *lib.Reactor - cmd string - user string - environment []string - args []string - cond map[string]*regexp.Regexp + r *lib.Reactor + cmd string + user string + environment []string + args []string + cond map[string]*regexp.Regexp + maximumCmdTimeLive time.Duration } // NewOrGet create the command struct and fill the parameters needed from the @@ -59,9 +61,19 @@ func NewOrGet(r *lib.Reactor, c map[string]interface{}) (*Cmd, error) { } } + case strings.ToLower("maximumCmdTimeLive"): + var err error + o.maximumCmdTimeLive, err = time.ParseDuration(v.(string)) + if err != nil { + log.Print(err) + o.maximumCmdTimeLive = defaultMaximumCmdTimeLive + } } } + if o.maximumCmdTimeLive == 0 { + o.maximumCmdTimeLive = defaultMaximumCmdTimeLive + } return o, nil } @@ -154,7 +166,7 @@ func (o *Cmd) Run(rl *lib.ReactorLog, msg lib.Msg) error { rl.Label = o.findReplace(msg, o.r.Label) - ctx, cancel := context.WithTimeout(context.Background(), maximumCmdTimeLive) + ctx, cancel := context.WithTimeout(context.Background(), o.maximumCmdTimeLive) defer cancel() var c *exec.Cmd