Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ExitCode to the "Exited" message #174

Merged
merged 6 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion start/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type process struct {
dead bool
interrupted bool
restart bool
paneID string

tmux *tmuxClient

Expand Down Expand Up @@ -175,7 +176,8 @@ func (p *process) observe() {
if !p.Running() {
if !p.keepingAlive {
p.out.Close()
p.output.WriteBoldLine(p, []byte("Exited"))

p.reportExitCode()
p.keepingAlive = true
}

Expand Down Expand Up @@ -209,3 +211,10 @@ func (p *process) respawn() {
p.waitPid()
p.output.WriteBoldLinef(p, "Restarted with pid %v...", p.pid)
}

func (p *process) reportExitCode() {
exitCode := p.tmux.PaneExitCode(p.paneID)
message := fmt.Sprintf("Exited with code %d", exitCode)

p.output.WriteBoldLine(p, []byte(message))
}
13 changes: 13 additions & 0 deletions start/tmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func (t *tmuxClient) mapProcess(pane, name, pid string) {
}

t.processesByPane[pane] = p
p.paneID = pane // save the tmux paneID in the process

if ipid, err := strconv.Atoi(pid); err == nil {
p.pid = ipid
Expand Down Expand Up @@ -235,3 +236,15 @@ func (t *tmuxClient) Shutdown() {
case <-time.After(5 * time.Second):
}
}

func (t *tmuxClient) PaneExitCode(paneID string) int {
cmd := exec.Command("tmux", "-L", t.Socket, "display-message", "-p", "-t", "%"+paneID, "#{pane_dead_status}")
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = os.Stderr
cmd.Run()

status, _ := strconv.Atoi(strings.TrimSpace(out.String()))

return status
}