Skip to content

Commit

Permalink
feat(manager): log script exit status
Browse files Browse the repository at this point in the history
  • Loading branch information
tjhop committed Jun 13, 2024
1 parent d850e80 commit 7512840
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions internal/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,20 +218,28 @@ func Run(ctx context.Context, runID ulid.ULID, path, content string, allVars []s
if err := os.MkdirAll(logDir, 0750); err != nil && !os.IsExist(err) {
return fmt.Errorf("Failed to create directory for script logs: %v", err)
}

// log stdout from script
stdoutLog, err := os.OpenFile(filepath.Join(logDir, "stdout"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("Failed to open script log for stdout: %v", err)
}
defer stdoutLog.Close()

// log stderr from script
// `mango_$scriptID_timestamp_stderr.log
stderrLog, err := os.OpenFile(filepath.Join(logDir, "stderr"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("Failed to open script log for stderr: %v", err)
}
defer stderrLog.Close()

// log exit status from script
exitStatusLog, err := os.OpenFile(filepath.Join(logDir, "exit_status"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("Failed to open script log for exit status: %v", err)
}
defer exitStatusLog.Close()

// log script content itself for testing template rendering
if err := os.WriteFile(filepath.Join(logDir, "script.mango-rendered"), []byte(content), 0644); err != nil {
return fmt.Errorf("Failed to write rendered script to log file: %v", err)
Expand Down Expand Up @@ -260,8 +268,20 @@ func Run(ctx context.Context, runID ulid.ULID, path, content string, allVars []s
}

// run it!
if err = runner.Run(ctx, file); err != nil {
return fmt.Errorf("Failed to run script %s: %v", path, err)
var exitStatus uint8
err = runner.Run(ctx, file)
if err != nil {
status, ok := interp.IsExitStatus(err)
if !ok {
// Not an exit code, something else went wrong
return fmt.Errorf("Failed to run script %s: %v", path, err)
}

exitStatus = status
}

if _, err := exitStatusLog.WriteString(fmt.Sprintf("%d\n", exitStatus)); err != nil {
return fmt.Errorf("Failed to write exit status log for status code '%d': %v", exitStatus, err)
}

return nil
Expand Down

0 comments on commit 7512840

Please sign in to comment.