feat: automatically detach from p logs -f on job end
CI / Check, test, lint (push) Successful in 36s
CI / Check, test, lint (push) Successful in 36s
This commit is contained in:
+23
-2
@@ -20,8 +20,29 @@ pub fn execute(job_id: &str, follow: bool) -> Result<()> {
|
|||||||
);
|
);
|
||||||
print_log(worker, &log)?;
|
print_log(worker, &log)?;
|
||||||
} else {
|
} else {
|
||||||
// Stream live output. Ctrl+C kills ssh; the job keeps running.
|
// Stream live output and exit automatically when the job finishes.
|
||||||
ssh::run_output(worker, &format!("tail -n +1 -f {}", log))?;
|
//
|
||||||
|
// The script backgrounds `tail -f` (so its stdout still flows to the
|
||||||
|
// client through the SSH pipe), then polls for the exitcode file that
|
||||||
|
// run.sh writes the moment the job exits. A 1-second sleep after
|
||||||
|
// detecting the exitcode file gives tail time to drain any bytes that
|
||||||
|
// were written to the log just before exitcode appeared. `kill` +
|
||||||
|
// `wait` let tail flush its buffer before the SSH connection closes.
|
||||||
|
//
|
||||||
|
// Ctrl+C still works: it kills ssh, which sends SIGHUP to the remote
|
||||||
|
// shell, terminating the whole script including the tail background job.
|
||||||
|
let exitcode_path = format!("~/.p/jobs/{}/exitcode", job.id);
|
||||||
|
let follow_cmd = format!(
|
||||||
|
"tail -n +1 -f {log} & \
|
||||||
|
TAIL_PID=$!; \
|
||||||
|
while ! [ -f {exitcode} ]; do sleep 1; done; \
|
||||||
|
sleep 1; \
|
||||||
|
kill \"$TAIL_PID\" 2>/dev/null; \
|
||||||
|
wait \"$TAIL_PID\" 2>/dev/null",
|
||||||
|
log = log,
|
||||||
|
exitcode = exitcode_path,
|
||||||
|
);
|
||||||
|
ssh::run_output(worker, &follow_cmd)?;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
print_log(worker, &log)?;
|
print_log(worker, &log)?;
|
||||||
|
|||||||
Reference in New Issue
Block a user