Skip to content

Commit f517579

Browse files
committed
use timeouts when executing remote execs
1 parent c6a8346 commit f517579

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

pkg/config/resources/exec/provider.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,17 @@ func (p *Provider) createRemoteExec(outputPath string) error {
205205
group = p.config.RunAs.Group
206206
}
207207

208-
_, err := p.container.ExecuteScript(targetID, script, envs, p.config.WorkingDirectory, user, group, 300, p.log.StandardWriter())
208+
if p.config.Timeout == "" {
209+
p.config.Timeout = "300s"
210+
}
211+
212+
timeout, err := time.ParseDuration(p.config.Timeout)
213+
if err != nil {
214+
p.log.Error("Unable to parse timeout duration", "ref", p.config.Meta.Name, "timeout", p.config.Timeout, "error", err)
215+
return fmt.Errorf("unable to parse timeout duration: %w", err)
216+
}
217+
218+
_, err = p.container.ExecuteScript(targetID, script, envs, p.config.WorkingDirectory, user, group, int(timeout.Seconds()), p.log.StandardWriter())
209219
if err != nil {
210220
p.log.Error("Unable to execute command", "ref", p.config.Meta.Name, "image", p.config.Image, "script", p.config.Script)
211221
return fmt.Errorf("unable to execute command: in remote container: %w", err)
@@ -304,19 +314,21 @@ func (p *Provider) createLocalExec(outputPath string) (int, error) {
304314
// create the folders for logs and pids
305315
logPath := filepath.Join(utils.LogsDir(), fmt.Sprintf("exec_%s.log", p.config.Meta.Name))
306316

307-
// do we have a duration to parse
308-
var d time.Duration
309-
if p.config.Timeout != "" {
310-
d, err = time.ParseDuration(p.config.Timeout)
311-
if err != nil {
312-
return 0, fmt.Errorf("unable to parse duration for timeout: %s", err)
313-
}
314-
317+
// do we have a duration to parse otherwise set default
318+
if p.config.Timeout == "" {
319+
p.config.Timeout = "300s"
320+
} else {
315321
if p.config.Daemon {
316322
p.log.Warn("Timeout will be ignored when exec is running in daemon mode")
317323
}
318324
}
319325

326+
timeout, err := time.ParseDuration(p.config.Timeout)
327+
if err != nil {
328+
p.log.Error("Unable to parse timeout duration", "ref", p.config.Meta.Name, "timeout", p.config.Timeout, "error", err)
329+
return 1, fmt.Errorf("unable to parse timeout duration: %w", err)
330+
}
331+
320332
// inject the output file into the environment
321333
envs = append(envs, fmt.Sprintf("EXEC_OUTPUT=%s", outputPath))
322334

@@ -327,7 +339,7 @@ func (p *Provider) createLocalExec(outputPath string) (int, error) {
327339
WorkingDirectory: p.config.WorkingDirectory,
328340
RunInBackground: p.config.Daemon,
329341
LogFilePath: logPath,
330-
Timeout: d,
342+
Timeout: timeout,
331343
}
332344

333345
pid, err := p.command.Execute(cc)

0 commit comments

Comments
 (0)