Skip to content

Commit

Permalink
fix: exit main process if a command fails
Browse files Browse the repository at this point in the history
  • Loading branch information
brunomacf committed Jul 4, 2021
1 parent b2be3b5 commit 0769516
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
6 changes: 6 additions & 0 deletions examples/base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,11 @@ acts:
- act: act2
- act: act1 -test


error:
cmds:
- echo "hello"
- exit 1

child:
include: child/actfile.yml
34 changes: 30 additions & 4 deletions run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func ActDetachExec(cmd *actfile.Cmd, ctx *ActRunCtx, wg *sync.WaitGroup) {
shCmd.Env = envars
shCmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}


// Set logging
if !ctx.RunCtx.Quiet && !ctx.Act.Quiet && !cmd.Quiet {
l := NewLogWriter(ctx)
Expand Down Expand Up @@ -218,13 +219,14 @@ func CmdExec(cmd *actfile.Cmd, ctx *ActRunCtx, wg *sync.WaitGroup) {
* Set the command to run (script or command line).
*/
var shArgs []string
var cmdLine string

if cmd.Script != "" {
cmdLine := utils.CompileTemplate(cmd.Script, vars)
cmdLine = utils.CompileTemplate(cmd.Script, vars)

shArgs = append([]string{cmdLine}, ctx.Args...)
} else {
cmdLine := utils.CompileTemplate(cmd.Cmd, vars)
cmdLine = utils.CompileTemplate(cmd.Cmd, vars)

shArgs = []string{"-c", cmdLine, "--"}
shArgs = append(shArgs, ctx.Args...)
Expand All @@ -248,6 +250,10 @@ func CmdExec(cmd *actfile.Cmd, ctx *ActRunCtx, wg *sync.WaitGroup) {
// Command to spawn.
shCmd := exec.Command(shell, shArgs...)

if shCmd.SysProcAttr == nil {
shCmd.SysProcAttr = &syscall.SysProcAttr{}
}

/**
* We going to run the scrip relative to the folder which contains
* the actfile where we actually matched the act to run.
Expand Down Expand Up @@ -303,6 +309,9 @@ func CmdExec(cmd *actfile.Cmd, ctx *ActRunCtx, wg *sync.WaitGroup) {
}

if !ctx.RunCtx.IsDaemon && logMode == "raw" {
shCmd.SysProcAttr.Setctty = true
shCmd.SysProcAttr.Setsid = true

shCmd.Stdout = os.Stdout
shCmd.Stderr = os.Stderr
shCmd.Stdin = os.Stdin
Expand Down Expand Up @@ -358,8 +367,25 @@ func CmdExec(cmd *actfile.Cmd, ctx *ActRunCtx, wg *sync.WaitGroup) {
// Save to run context info file
ctx.RunCtx.Info.AddChildPgid(pgid)

// Wait finalization
shCmd.Wait()
// Wait finalization and get error code
if err := shCmd.Wait(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
errMsg := fmt.Sprintf("command '%s' failed", cmdLine)

// The program has exited with an exit code != 0

// This works on both Unix and Windows. Although package
// syscall is generally platform dependent, WaitStatus is
// defined for both Unix and Windows and in both cases has
// an ExitStatus() method with the same signature.
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
utils.FatalErrorWithCode(status.ExitStatus(), errMsg, err)
//os.Exit(status.ExitStatus())
} else {
utils.FatalError(errMsg, err)
}
}
}

// Remove pgid now
if !ctx.RunCtx.IsKilling {
Expand Down
8 changes: 8 additions & 0 deletions utils/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ func FatalError(args ...interface{}) {
os.Exit(1)
}

/**
* This function going to handle fatal error with code.
*/
func FatalErrorWithCode(code int, args ...interface{}) {
LogError(args...)
os.Exit(code)
}

//############################################################
// Lifecycle Functions
//############################################################
Expand Down

0 comments on commit 0769516

Please sign in to comment.