use std::collections::HashMap;
use std::fmt::Write as _;
use std::path::PathBuf;
use std::time::Instant;
use async_channel::{Receiver, Sender};
use futures_lite::io::AsyncWriteExt;
use crate::permission::TerminalMode;
use crate::process_tree::{JobMarker, JobTree, KillTreeError, ProcessScanner};
#[derive(Debug, Clone)]
pub struct JobInfo {
pub pid: u32,
pub task_id: String,
pub execution_key: String,
pub script: String,
pub mode: TerminalMode,
pub started_at: Instant,
pub status: JobStatus,
pub output_path: Option<PathBuf>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum JobStatus {
Running,
Completed {
exit_code: i32,
},
Failed {
error: String,
},
Killed,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TerminalDelta {
pub task_id: String,
pub cursor: usize,
pub total_bytes: usize,
pub bytes: Vec<u8>,
pub status: JobStatus,
}
pub type TerminalInputSender = Sender<Vec<u8>>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TerminalStream {
Stdout,
Stderr,
}
#[derive(Debug)]
struct JobState {
info: JobInfo,
terminal_buffer: Vec<u8>,
stdout_buffer: Vec<u8>,
stderr_buffer: Vec<u8>,
stdout_closed: bool,
stderr_closed: bool,
input_tx: Option<TerminalInputSender>,
kill_switch: Option<Sender<()>>,
process_tree: Option<JobTree>,
kills_in_flight: usize,
deferred_end: Option<Ending>,
output_redirect: Option<async_fs::File>,
finished_at: Option<Instant>,
}
enum StateCommand {
Register {
pid: u32,
task_id: String,
execution_key: String,
script: String,
mode: TerminalMode,
output_path: Option<PathBuf>,
process_tree: Option<JobTree>,
},
AttachTerminalInput {
pid: u32,
input_tx: TerminalInputSender,
},
AttachKillSwitch {
pid: u32,
kill_tx: Sender<()>,
},
AppendTerminalOutput {
pid: u32,
stream: TerminalStream,
chunk: Vec<u8>,
},
CloseTerminalStream {
pid: u32,
stream: TerminalStream,
},
Complete {
pid: u32,
exit_code: i32,
output_path: Option<PathBuf>,
},
Fail {
pid: u32,
error: String,
output_path: Option<PathBuf>,
},
RecordFileReads {
reads: Vec<(PathBuf, FileFingerprint)>,
},
}
enum JobRequest {
List {
reply: Sender<Vec<JobInfo>>,
},
Get {
pid: u32,
reply: Sender<Option<JobInfo>>,
},
GetByTaskId {
task_id: String,
reply: Sender<Option<JobInfo>>,
},
BeginKill {
pid: u32,
reply: Sender<KillPlan>,
},
BeginKillByTaskId {
task_id: String,
reply: Sender<Option<(u32, KillPlan)>>,
},
BeginKillByExecutionKey {
execution_key: String,
reply: Sender<Vec<(u32, KillPlan)>>,
},
EndKill {
pid: u32,
killed: bool,
reply: Sender<bool>,
},
InputTerminal {
task_id: String,
bytes: Vec<u8>,
reply: Sender<Result<(), String>>,
},
StartOutputRedirect {
task_id: String,
output_path: PathBuf,
reply: Sender<Result<Vec<u8>, String>>,
},
TerminalOutput {
pid: u32,
reply: Sender<Option<(Vec<u8>, Vec<u8>)>>,
},
ReadTerminalDelta {
task_id: String,
cursor: usize,
max_bytes: usize,
tracked: bool,
reply: Sender<Result<TerminalDelta, String>>,
},
FileReadsUnchanged {
reads: Vec<(PathBuf, FileFingerprint)>,
reply: Sender<bool>,
},
TerminalStreamsClosed {
pid: u32,
reply: Sender<bool>,
},
FormatRunning {
reply: Sender<String>,
},
HasRunning {
reply: Sender<bool>,
},
}
pub type FileFingerprint = (u64, Option<std::time::SystemTime>);
enum KillPlan {
Done(bool),
Tree(JobTree),
Group(u32),
}
#[derive(Debug)]
enum Ending {
Complete {
exit_code: i32,
output_path: Option<PathBuf>,
},
Fail {
error: String,
output_path: Option<PathBuf>,
},
}
enum JobCommand {
State(StateCommand),
Request(JobRequest),
}
impl From<StateCommand> for JobCommand {
fn from(command: StateCommand) -> Self {
Self::State(command)
}
}
impl From<JobRequest> for JobCommand {
fn from(request: JobRequest) -> Self {
Self::Request(request)
}
}
#[derive(Clone, Debug)]
pub struct JobRegistry {
tx: Sender<JobCommand>,
#[cfg_attr(target_os = "linux", allow(dead_code))]
scanner: ProcessScanner,
}
#[derive(Debug)]
pub struct JobRegistryService {
rx: Receiver<JobCommand>,
}
#[must_use]
pub fn job_registry_channel() -> (JobRegistry, JobRegistryService) {
let (tx, rx) = async_channel::unbounded();
(
JobRegistry {
tx,
scanner: ProcessScanner::default(),
},
JobRegistryService { rx },
)
}
impl JobRegistry {
async fn send_command(&self, command: impl Into<JobCommand>) {
self.tx
.send(command.into())
.await
.expect("job registry service unavailable");
}
async fn recv_reply<T>(reply_rx: Receiver<T>) -> T {
reply_rx
.recv()
.await
.expect("job registry response dropped")
}
pub async fn register(
&self,
pid: u32,
task_id: &str,
execution_key: &str,
script: &str,
mode: TerminalMode,
output_path: Option<PathBuf>,
) {
self.register_with_tree(pid, task_id, execution_key, script, mode, output_path, None)
.await;
}
#[allow(clippy::too_many_arguments)]
pub(crate) async fn register_with_tree(
&self,
pid: u32,
task_id: &str,
execution_key: &str,
script: &str,
mode: TerminalMode,
output_path: Option<PathBuf>,
process_tree: Option<JobTree>,
) {
self.send_command(StateCommand::Register {
pid,
task_id: task_id.to_string(),
execution_key: execution_key.to_string(),
script: script.to_string(),
mode,
output_path,
process_tree,
})
.await;
}
pub async fn attach_terminal_input(&self, pid: u32, input_tx: TerminalInputSender) {
self.send_command(StateCommand::AttachTerminalInput { pid, input_tx })
.await;
}
pub async fn attach_kill_switch(&self, pid: u32, kill_tx: Sender<()>) {
self.send_command(StateCommand::AttachKillSwitch { pid, kill_tx })
.await;
}
#[cfg_attr(target_os = "linux", allow(dead_code))]
pub(crate) fn start_process_tree<E: executor_core::Executor>(
&self,
executor: &E,
pid: u32,
marker: &JobMarker,
) -> JobTree {
JobTree::spawn(executor, self.scanner.clone(), pid, marker)
}
pub async fn append_stdout(&self, pid: u32, chunk: Vec<u8>) {
self.send_command(StateCommand::AppendTerminalOutput {
pid,
stream: TerminalStream::Stdout,
chunk,
})
.await;
}
pub async fn append_stderr(&self, pid: u32, chunk: Vec<u8>) {
self.send_command(StateCommand::AppendTerminalOutput {
pid,
stream: TerminalStream::Stderr,
chunk,
})
.await;
}
pub async fn close_stdout(&self, pid: u32) {
self.send_command(StateCommand::CloseTerminalStream {
pid,
stream: TerminalStream::Stdout,
})
.await;
}
pub async fn close_stderr(&self, pid: u32) {
self.send_command(StateCommand::CloseTerminalStream {
pid,
stream: TerminalStream::Stderr,
})
.await;
}
pub async fn terminal_input(&self, task_id: &str, bytes: Vec<u8>) -> Result<(), String> {
let (reply_tx, reply_rx) = async_channel::bounded(1);
self.send_command(JobRequest::InputTerminal {
task_id: task_id.to_string(),
bytes,
reply: reply_tx,
})
.await;
Self::recv_reply(reply_rx).await
}
pub async fn start_output_redirect(
&self,
task_id: &str,
output_path: PathBuf,
) -> Result<Vec<u8>, String> {
let (reply_tx, reply_rx) = async_channel::bounded(1);
self.send_command(JobRequest::StartOutputRedirect {
task_id: task_id.to_string(),
output_path,
reply: reply_tx,
})
.await;
Self::recv_reply(reply_rx).await
}
pub async fn terminal_streams_closed(&self, pid: u32) -> bool {
let (reply_tx, reply_rx) = async_channel::bounded(1);
self.send_command(JobRequest::TerminalStreamsClosed {
pid,
reply: reply_tx,
})
.await;
Self::recv_reply(reply_rx).await
}
pub async fn terminal_output(&self, pid: u32) -> Option<(Vec<u8>, Vec<u8>)> {
let (reply_tx, reply_rx) = async_channel::bounded(1);
self.send_command(JobRequest::TerminalOutput {
pid,
reply: reply_tx,
})
.await;
Self::recv_reply(reply_rx).await
}
pub async fn terminal_read(
&self,
task_id: &str,
cursor: usize,
max_bytes: usize,
) -> Result<TerminalDelta, String> {
let (reply_tx, reply_rx) = async_channel::bounded(1);
self.send_command(JobRequest::ReadTerminalDelta {
task_id: task_id.to_string(),
cursor,
max_bytes,
tracked: true,
reply: reply_tx,
})
.await;
Self::recv_reply(reply_rx).await
}
pub async fn monitor_read(
&self,
task_id: &str,
cursor: usize,
max_bytes: usize,
) -> Result<TerminalDelta, String> {
let (reply_tx, reply_rx) = async_channel::bounded(1);
self.send_command(JobRequest::ReadTerminalDelta {
task_id: task_id.to_string(),
cursor,
max_bytes,
tracked: false,
reply: reply_tx,
})
.await;
Self::recv_reply(reply_rx).await
}
pub async fn file_reads_unchanged(&self, reads: Vec<(PathBuf, FileFingerprint)>) -> bool {
let (reply_tx, reply_rx) = async_channel::bounded(1);
self.send_command(JobRequest::FileReadsUnchanged {
reads,
reply: reply_tx,
})
.await;
Self::recv_reply(reply_rx).await
}
pub async fn record_file_reads(&self, reads: Vec<(PathBuf, FileFingerprint)>) {
self.send_command(StateCommand::RecordFileReads { reads })
.await;
}
pub async fn complete(&self, pid: u32, exit_code: i32, output_path: Option<PathBuf>) {
self.send_command(StateCommand::Complete {
pid,
exit_code,
output_path,
})
.await;
}
pub async fn fail(&self, pid: u32, error: &str, output_path: Option<PathBuf>) {
self.send_command(StateCommand::Fail {
pid,
error: error.to_string(),
output_path,
})
.await;
}
pub async fn list(&self) -> Vec<JobInfo> {
let (reply_tx, reply_rx) = async_channel::bounded(1);
self.send_command(JobRequest::List { reply: reply_tx })
.await;
Self::recv_reply(reply_rx).await
}
pub async fn get(&self, pid: u32) -> Option<JobInfo> {
let (reply_tx, reply_rx) = async_channel::bounded(1);
self.send_command(JobRequest::Get {
pid,
reply: reply_tx,
})
.await;
Self::recv_reply(reply_rx).await
}
pub async fn get_by_task_id(&self, task_id: &str) -> Option<JobInfo> {
let (reply_tx, reply_rx) = async_channel::bounded(1);
self.send_command(JobRequest::GetByTaskId {
task_id: task_id.to_string(),
reply: reply_tx,
})
.await;
Self::recv_reply(reply_rx).await
}
pub async fn kill(&self, pid: u32) -> bool {
let (reply_tx, reply_rx) = async_channel::bounded(1);
self.send_command(JobRequest::BeginKill {
pid,
reply: reply_tx,
})
.await;
let plan = Self::recv_reply(reply_rx).await;
self.finish_kill(pid, plan).await
}
pub async fn kill_by_task_id(&self, task_id: &str) -> bool {
let (reply_tx, reply_rx) = async_channel::bounded(1);
self.send_command(JobRequest::BeginKillByTaskId {
task_id: task_id.to_string(),
reply: reply_tx,
})
.await;
match Self::recv_reply(reply_rx).await {
Some((pid, plan)) => self.finish_kill(pid, plan).await,
None => false,
}
}
pub async fn kill_running(&self) -> usize {
let jobs = self.list().await;
futures::future::join_all(
jobs.into_iter()
.filter(|job| job.status == JobStatus::Running)
.map(|job| self.kill(job.pid)),
)
.await
.into_iter()
.filter(|killed| *killed)
.count()
}
pub async fn kill_by_execution_key(&self, execution_key: &str) -> usize {
let (reply_tx, reply_rx) = async_channel::bounded(1);
self.send_command(JobRequest::BeginKillByExecutionKey {
execution_key: execution_key.to_string(),
reply: reply_tx,
})
.await;
let plans = Self::recv_reply(reply_rx).await;
futures::future::join_all(
plans
.into_iter()
.map(|(pid, plan)| self.finish_kill(pid, plan)),
)
.await
.into_iter()
.filter(|killed| *killed)
.count()
}
async fn finish_kill(&self, pid: u32, plan: KillPlan) -> bool {
let killed = match plan {
KillPlan::Done(killed) => return killed,
KillPlan::Tree(tree) => match tree.kill().await {
Ok(()) => true,
#[cfg(unix)]
Err(KillTreeError::Ended) => {
tracing::debug!(pid = %pid, "the job ended before the kill reached it");
false
}
Err(error) => {
tracing::warn!(pid = %pid, error = %error, "failed to kill job");
false
}
},
KillPlan::Group(group) => match crate::process_tree::kill_process_tree(group).await {
Ok(()) => true,
Err(error) => {
tracing::warn!(pid = %pid, error = %error, "failed to kill job");
false
}
},
};
let (reply_tx, reply_rx) = async_channel::bounded(1);
self.send_command(JobRequest::EndKill {
pid,
killed,
reply: reply_tx,
})
.await;
Self::recv_reply(reply_rx).await
}
pub async fn format_running_jobs(&self) -> String {
let (reply_tx, reply_rx) = async_channel::bounded(1);
self.send_command(JobRequest::FormatRunning { reply: reply_tx })
.await;
Self::recv_reply(reply_rx).await
}
pub async fn has_running(&self) -> bool {
let (reply_tx, reply_rx) = async_channel::bounded(1);
self.send_command(JobRequest::HasRunning { reply: reply_tx })
.await;
Self::recv_reply(reply_rx).await
}
}
#[derive(Default)]
struct ReadTracker {
delta_reads: HashMap<String, (usize, usize, JobStatus)>,
file_reads: HashMap<PathBuf, FileFingerprint>,
}
impl JobRegistryService {
pub async fn serve(self) {
let mut jobs: HashMap<u32, JobState> = HashMap::new();
let mut reads = ReadTracker::default();
while let Ok(command) = self.rx.recv().await {
match command {
JobCommand::State(command) => {
handle_state_command(&mut jobs, &mut reads, command).await;
}
JobCommand::Request(request) => {
handle_job_request(&mut jobs, &mut reads, request).await;
}
}
}
}
}
async fn handle_state_command(
jobs: &mut HashMap<u32, JobState>,
reads: &mut ReadTracker,
command: StateCommand,
) {
match command {
StateCommand::Register {
pid,
task_id,
execution_key,
script,
mode,
output_path,
process_tree,
} => {
register_job(jobs, pid, task_id, execution_key, script, mode, output_path);
if let Some(job) = jobs.get_mut(&pid) {
job.process_tree = process_tree;
}
}
StateCommand::AttachTerminalInput { pid, input_tx } => {
if let Some(job) = jobs.get_mut(&pid) {
job.input_tx = Some(input_tx);
}
}
StateCommand::AttachKillSwitch { pid, kill_tx } => {
if let Some(job) = jobs.get_mut(&pid) {
job.kill_switch = Some(kill_tx);
}
}
StateCommand::AppendTerminalOutput { pid, stream, chunk } => {
append_terminal_output(jobs, pid, stream, chunk).await;
}
StateCommand::CloseTerminalStream { pid, stream } => {
close_terminal_stream(jobs, pid, stream);
}
StateCommand::Complete {
pid,
exit_code,
output_path,
} => complete_job(jobs, pid, exit_code, output_path).await,
StateCommand::Fail {
pid,
error,
output_path,
} => fail_job(jobs, pid, error, output_path).await,
StateCommand::RecordFileReads { reads: new_reads } => {
reads.file_reads.extend(new_reads);
}
}
}
async fn handle_job_request(
jobs: &mut HashMap<u32, JobState>,
reads: &mut ReadTracker,
request: JobRequest,
) {
match request {
JobRequest::List { reply } => {
let items = jobs.values().map(|state| state.info.clone()).collect();
let _ = reply.send(items).await;
}
JobRequest::Get { pid, reply } => {
let item = jobs.get(&pid).map(|state| state.info.clone());
let _ = reply.send(item).await;
}
JobRequest::GetByTaskId { task_id, reply } => {
let item = find_pid_by_task_id(jobs, &task_id)
.and_then(|pid| jobs.get(&pid))
.map(|state| state.info.clone());
let _ = reply.send(item).await;
}
JobRequest::BeginKill { pid, reply } => {
let plan = begin_kill(jobs, pid).await;
let _ = reply.send(plan).await;
}
JobRequest::BeginKillByTaskId { task_id, reply } => {
let plan = match find_pid_by_task_id(jobs, &task_id) {
Some(pid) => Some((pid, begin_kill(jobs, pid).await)),
None => None,
};
let _ = reply.send(plan).await;
}
JobRequest::BeginKillByExecutionKey {
execution_key,
reply,
} => {
let plans = begin_kill_by_execution_key(jobs, &execution_key).await;
let _ = reply.send(plans).await;
}
JobRequest::EndKill { pid, killed, reply } => {
let result = end_kill(jobs, pid, killed).await;
let _ = reply.send(result).await;
}
JobRequest::InputTerminal {
task_id,
bytes,
reply,
} => {
let result = input_terminal(jobs, &task_id, bytes).await;
let _ = reply.send(result).await;
}
JobRequest::StartOutputRedirect {
task_id,
output_path,
reply,
} => {
let result = redirect_task_output(jobs, &task_id, output_path).await;
let _ = reply.send(result).await;
}
JobRequest::TerminalOutput { pid, reply } => {
send_terminal_output(jobs, pid, reply).await;
}
JobRequest::ReadTerminalDelta {
task_id,
cursor,
max_bytes,
tracked,
reply,
} => {
let result = read_terminal_delta(jobs, &task_id, cursor, max_bytes)
.and_then(|delta| tracked_delta(reads, &task_id, delta, tracked));
let _ = reply.send(result).await;
}
JobRequest::FileReadsUnchanged {
reads: paths,
reply,
} => {
let unchanged = !paths.is_empty()
&& paths
.iter()
.all(|(path, stamp)| reads.file_reads.get(path) == Some(stamp));
let _ = reply.send(unchanged).await;
}
JobRequest::TerminalStreamsClosed { pid, reply } => {
let closed = jobs
.get(&pid)
.is_some_and(|job| job.stdout_closed && job.stderr_closed);
let _ = reply.send(closed).await;
}
JobRequest::FormatRunning { reply } => {
let _ = reply.send(format_running_jobs(jobs)).await;
}
JobRequest::HasRunning { reply } => {
let _ = reply.send(has_running_job(jobs)).await;
}
}
}
async fn send_terminal_output(
jobs: &HashMap<u32, JobState>,
pid: u32,
reply: Sender<Option<(Vec<u8>, Vec<u8>)>>,
) {
let output = jobs
.get(&pid)
.map(|job| (job.stdout_buffer.clone(), job.stderr_buffer.clone()));
let _ = reply.send(output).await;
}
fn has_running_job(jobs: &HashMap<u32, JobState>) -> bool {
jobs.values()
.any(|job| matches!(job.info.status, JobStatus::Running))
}
fn register_job(
jobs: &mut HashMap<u32, JobState>,
pid: u32,
task_id: String,
execution_key: String,
script: String,
mode: TerminalMode,
output_path: Option<PathBuf>,
) {
let info = JobInfo {
pid,
task_id,
execution_key,
script,
mode,
started_at: Instant::now(),
status: JobStatus::Running,
output_path,
};
jobs.insert(pid, JobState::new(info));
tracing::debug!(pid = %pid, "registered background job");
}
impl JobState {
const fn new(info: JobInfo) -> Self {
Self {
info,
terminal_buffer: Vec::new(),
stdout_buffer: Vec::new(),
stderr_buffer: Vec::new(),
stdout_closed: false,
stderr_closed: false,
input_tx: None,
kill_switch: None,
process_tree: None,
kills_in_flight: 0,
deferred_end: None,
output_redirect: None,
finished_at: None,
}
}
}
async fn append_terminal_output(
jobs: &mut HashMap<u32, JobState>,
pid: u32,
stream: TerminalStream,
chunk: Vec<u8>,
) {
if chunk.is_empty() {
return;
}
if let Some(job) = jobs.get_mut(&pid) {
job.terminal_buffer.extend_from_slice(&chunk);
if let Some(file) = job.output_redirect.as_mut()
&& let Err(error) = file.write_all(&chunk).await
{
mark_job_failed(
job,
format!(
"failed to append redirected output for task {}: {error}",
job.info.task_id
),
);
tracing::error!(
pid = %pid,
error = %error,
"failed to append redirected output"
);
}
match stream {
TerminalStream::Stdout => job.stdout_buffer.extend_from_slice(&chunk),
TerminalStream::Stderr => job.stderr_buffer.extend_from_slice(&chunk),
}
}
}
fn close_terminal_stream(jobs: &mut HashMap<u32, JobState>, pid: u32, stream: TerminalStream) {
if let Some(job) = jobs.get_mut(&pid) {
match stream {
TerminalStream::Stdout => job.stdout_closed = true,
TerminalStream::Stderr => job.stderr_closed = true,
}
}
}
async fn complete_job(
jobs: &mut HashMap<u32, JobState>,
pid: u32,
exit_code: i32,
output_path: Option<PathBuf>,
) {
if let Some(job) = jobs.get_mut(&pid) {
if !matches!(job.info.status, JobStatus::Running) {
tracing::debug!(
pid = %pid,
status = ?job.info.status,
"ignoring complete for non-running job"
);
return;
}
if job.kills_in_flight > 0 {
job.deferred_end = Some(Ending::Complete {
exit_code,
output_path,
});
return;
}
if let Err(error) = finalize_output_redirect(job).await {
tracing::error!(
pid = %pid,
error = %error,
"failed to flush redirected output on complete"
);
mark_job_failed(job, error);
return;
}
job.info.status = JobStatus::Completed { exit_code };
job.finished_at = Some(Instant::now());
job.kill_switch = None;
if output_path.is_some() {
job.info.output_path = output_path;
}
tracing::debug!(pid = %pid, exit_code = %exit_code, "job completed");
}
}
async fn fail_job(
jobs: &mut HashMap<u32, JobState>,
pid: u32,
error: String,
output_path: Option<PathBuf>,
) {
if let Some(job) = jobs.get_mut(&pid) {
if !matches!(job.info.status, JobStatus::Running) {
tracing::debug!(
pid = %pid,
status = ?job.info.status,
"ignoring fail for non-running job"
);
return;
}
if job.kills_in_flight > 0 {
job.deferred_end = Some(Ending::Fail { error, output_path });
return;
}
let status_error = if let Err(finalize_error) = finalize_output_redirect(job).await {
format!("{error}; additionally failed to flush redirected output: {finalize_error}")
} else {
error
};
mark_job_failed(job, status_error);
job.kill_switch = None;
if output_path.is_some() {
job.info.output_path = output_path;
}
tracing::debug!(pid = %pid, "job failed");
}
}
async fn begin_kill_by_execution_key(
jobs: &mut HashMap<u32, JobState>,
execution_key: &str,
) -> Vec<(u32, KillPlan)> {
let pids: Vec<u32> = jobs
.iter()
.filter_map(|(pid, state)| {
(state.info.execution_key == execution_key
&& matches!(state.info.status, JobStatus::Running))
.then_some(*pid)
})
.collect();
let mut plans = Vec::with_capacity(pids.len());
for pid in pids {
plans.push((pid, begin_kill(jobs, pid).await));
}
plans
}
async fn input_terminal(
jobs: &HashMap<u32, JobState>,
task_id: &str,
bytes: Vec<u8>,
) -> Result<(), String> {
let Some(pid) = find_pid_by_task_id(jobs, task_id) else {
return Err(format!("unknown task_id: {task_id}"));
};
let Some(job) = jobs.get(&pid) else {
return Err(format!("unknown task_id: {task_id}"));
};
if !matches!(job.info.status, JobStatus::Running) {
return Err(format!(
"task {} is not running; current status: {:?}",
task_id, job.info.status
));
}
let Some(tx) = &job.input_tx else {
return Err(format!(
"task {task_id} does not support terminal input in this runtime"
));
};
tx.send(bytes)
.await
.map_err(|_| format!("terminal input channel closed for task {task_id}"))
}
async fn redirect_task_output(
jobs: &mut HashMap<u32, JobState>,
task_id: &str,
output_path: PathBuf,
) -> Result<Vec<u8>, String> {
let Some(pid) = find_pid_by_task_id(jobs, task_id) else {
return Err(format!("unknown task_id: {task_id}"));
};
let Some(job) = jobs.get_mut(&pid) else {
return Err(format!("unknown task_id: {task_id}"));
};
start_output_redirect(job, output_path).await
}
fn tracked_delta(
reads: &mut ReadTracker,
task_id: &str,
delta: TerminalDelta,
tracked: bool,
) -> Result<TerminalDelta, String> {
let returned = (delta.cursor, delta.total_bytes, delta.status.clone());
if !tracked {
return Ok(delta);
}
if reads.delta_reads.get(task_id) == Some(&returned) {
Err(crate::poll_guard::unchanged_task_read(task_id))
} else {
reads.delta_reads.insert(task_id.to_string(), returned);
Ok(delta)
}
}
fn read_terminal_delta(
jobs: &HashMap<u32, JobState>,
task_id: &str,
cursor: usize,
max_bytes: usize,
) -> Result<TerminalDelta, String> {
find_pid_by_task_id(jobs, task_id).map_or_else(
|| Err(format!("unknown task_id: {task_id}")),
|pid| {
jobs.get(&pid).map_or_else(
|| Err(format!("unknown task_id: {task_id}")),
|job| terminal_read(job, cursor, max_bytes),
)
},
)
}
async fn start_output_redirect(
job: &mut JobState,
output_path: PathBuf,
) -> Result<Vec<u8>, String> {
if let Some(existing) = job.info.output_path.as_ref() {
if existing != &output_path {
return Err(format!(
"task {} already redirects output to {}",
job.info.task_id,
existing.display()
));
}
return Ok(job.terminal_buffer.clone());
}
let mut file = async_fs::OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(&output_path)
.await
.map_err(|error| {
format!(
"failed to open output redirect file {}: {error}",
output_path.display()
)
})?;
file.write_all(&job.terminal_buffer)
.await
.map_err(|error| {
format!(
"failed to write initial output redirect snapshot for task {}: {error}",
job.info.task_id
)
})?;
file.flush().await.map_err(|error| {
format!(
"failed to flush initial output redirect snapshot for task {}: {error}",
job.info.task_id
)
})?;
job.info.output_path = Some(output_path);
job.output_redirect = Some(file);
Ok(job.terminal_buffer.clone())
}
fn mark_job_failed(job: &mut JobState, error: String) {
job.info.status = JobStatus::Failed { error };
job.finished_at = Some(Instant::now());
job.kill_switch = None;
}
async fn finalize_output_redirect(job: &mut JobState) -> Result<(), String> {
if let Some(mut file) = job.output_redirect.take()
&& let Err(error) = file.flush().await
{
return Err(format!(
"failed to flush redirected output for task {}: {error}",
job.info.task_id
));
}
Ok(())
}
fn find_pid_by_task_id(jobs: &HashMap<u32, JobState>, task_id: &str) -> Option<u32> {
jobs.iter().find_map(|(pid, state)| {
if state.info.task_id == task_id {
Some(*pid)
} else {
None
}
})
}
async fn begin_kill(jobs: &mut HashMap<u32, JobState>, pid: u32) -> KillPlan {
let Some(job) = jobs.get_mut(&pid) else {
return KillPlan::Done(false);
};
if !matches!(job.info.status, JobStatus::Running) {
tracing::warn!(pid = %pid, "cannot kill non-running job");
return KillPlan::Done(false);
}
if let Some(kill_switch) = job.kill_switch.take() {
if kill_switch.send(()).await.is_err() {
tracing::warn!(pid = %pid, "failed to signal runtime kill switch");
return KillPlan::Done(false);
}
if let Err(error) = finalize_output_redirect(job).await {
tracing::warn!(
pid = %pid,
error = %error,
"failed to flush redirected output after kill switch signal"
);
}
job.info.status = JobStatus::Killed;
job.finished_at = Some(Instant::now());
job.stdout_closed = true;
job.stderr_closed = true;
tracing::info!(pid = %pid, "killed job via runtime kill switch");
return KillPlan::Done(true);
}
job.kills_in_flight += 1;
job.process_tree
.as_ref()
.map_or(KillPlan::Group(pid), |tree| KillPlan::Tree(tree.clone()))
}
async fn end_kill(jobs: &mut HashMap<u32, JobState>, pid: u32, killed: bool) -> bool {
let Some(job) = jobs.get_mut(&pid) else {
return false;
};
job.kills_in_flight = job.kills_in_flight.saturating_sub(1);
if killed {
job.deferred_end = None;
if matches!(job.info.status, JobStatus::Running) {
if let Err(error) = finalize_output_redirect(job).await {
tracing::warn!(
pid = %pid,
error = %error,
"failed to flush redirected output after kill"
);
}
job.info.status = JobStatus::Killed;
job.finished_at = Some(Instant::now());
job.stdout_closed = true;
job.stderr_closed = true;
tracing::info!(pid = %pid, "killed job");
}
return true;
}
match job.deferred_end.take() {
Some(Ending::Complete {
exit_code,
output_path,
}) => complete_job(jobs, pid, exit_code, output_path).await,
Some(Ending::Fail { error, output_path }) => {
fail_job(jobs, pid, error, output_path).await;
}
None => {}
}
false
}
const SCRIPT_PREVIEW_BYTES: usize = 50;
fn script_preview(script: &str) -> String {
if script.len() <= SCRIPT_PREVIEW_BYTES {
return script.to_string();
}
let end = (0..=SCRIPT_PREVIEW_BYTES)
.rev()
.find(|index| script.is_char_boundary(*index))
.unwrap_or(0);
format!("{}...", &script[..end])
}
fn format_running_jobs(jobs: &HashMap<u32, JobState>) -> String {
let running: Vec<_> = jobs
.values()
.filter(|job| matches!(job.info.status, JobStatus::Running))
.collect();
if running.is_empty() {
return String::new();
}
let mut output = String::new();
for job in running {
let script_preview = script_preview(&job.info.script);
let output_path = job
.info
.output_path
.as_ref()
.map_or_else(|| "(no output)".to_string(), |p| p.display().to_string());
let _ = writeln!(
output,
" - task {} (pid {}): `{}` -> {}",
job.info.task_id, job.info.pid, script_preview, output_path
);
}
output
}
fn terminal_read(job: &JobState, cursor: usize, max_bytes: usize) -> Result<TerminalDelta, String> {
let total_bytes = job.terminal_buffer.len();
if cursor > total_bytes {
return Err(format!(
"cursor {cursor} exceeds terminal buffer size {total_bytes} for task {}",
job.info.task_id
));
}
let end = total_bytes.min(cursor.saturating_add(max_bytes));
Ok(TerminalDelta {
task_id: job.info.task_id.clone(),
cursor: end,
total_bytes,
bytes: job.terminal_buffer[cursor..end].to_vec(),
status: job.info.status.clone(),
})
}
#[cfg(test)]
mod tests {
use super::*;
use executor_core::Executor;
use executor_core::Task;
use executor_core::tokio::TokioGlobal;
use tempfile::tempdir;
fn spawn_registry() -> JobRegistry {
let (registry, service) = job_registry_channel();
TokioGlobal
.spawn(async move { service.serve().await })
.detach();
registry
}
#[tokio::test]
async fn unchanged_terminal_read_is_refused_then_succeeds_after_new_output() {
let registry = spawn_registry();
registry
.register(7, "task-a", "exec-a", "run", TerminalMode::Sandboxed, None)
.await;
registry.append_stdout(7, b"one".to_vec()).await;
let first = registry
.terminal_read("task-a", 0, 1024)
.await
.expect("first read succeeds");
let second = registry.terminal_read("task-a", first.cursor, 1024).await;
assert!(
second.is_err(),
"an unchanged repeat read must be refused: {second:?}"
);
registry.append_stdout(7, b"two".to_vec()).await;
let third = registry
.terminal_read("task-a", first.cursor, 1024)
.await
.expect("read succeeds once output changed");
assert_eq!(third.bytes, b"two");
}
#[tokio::test]
async fn file_reads_track_unchanged_fingerprints() {
let registry = spawn_registry();
let dir = tempdir().expect("tempdir");
let path = dir.path().join("log.txt");
std::fs::write(&path, "v1").expect("write");
let meta = std::fs::metadata(&path).expect("metadata");
let reads = vec![(path.clone(), (meta.len(), meta.modified().ok()))];
assert!(!registry.file_reads_unchanged(reads.clone()).await);
registry.record_file_reads(reads.clone()).await;
assert!(registry.file_reads_unchanged(reads).await);
std::fs::write(&path, "v1-longer").expect("rewrite");
let meta = std::fs::metadata(&path).expect("metadata");
let changed = vec![(path, (meta.len(), meta.modified().ok()))];
assert!(!registry.file_reads_unchanged(changed).await);
}
#[tokio::test]
async fn test_job_registry_lifecycle() {
let (registry, service) = job_registry_channel();
TokioGlobal
.spawn(async move { service.serve().await })
.detach();
registry
.register(
12345,
"amber-forest-thunder-pearl",
"exec-test",
"sleep 10",
TerminalMode::Sandboxed,
Some(PathBuf::from("outputs/12345.txt")),
)
.await;
let jobs = registry.list().await;
assert_eq!(jobs.len(), 1);
assert_eq!(jobs[0].pid, 12345);
assert_eq!(jobs[0].task_id, "amber-forest-thunder-pearl");
assert_eq!(jobs[0].execution_key, "exec-test");
assert!(matches!(jobs[0].status, JobStatus::Running));
registry.complete(12345, 0, None).await;
let job = registry.get(12345).await.unwrap();
assert!(matches!(job.status, JobStatus::Completed { exit_code: 0 }));
}
#[tokio::test]
async fn test_format_running_jobs() {
let (registry, service) = job_registry_channel();
TokioGlobal
.spawn(async move { service.serve().await })
.detach();
registry
.register(
1,
"task-a",
"exec-a",
"echo hello",
TerminalMode::Sandboxed,
None,
)
.await;
registry
.register(
2,
"task-b",
"exec-b",
"sleep 5",
TerminalMode::Sandboxed,
None,
)
.await;
registry.complete(2, 0, None).await;
let running = registry.format_running_jobs().await;
assert!(running.contains("task task-a"));
assert!(!running.contains("task task-b"));
}
#[tokio::test]
async fn test_output_redirect_streams_full_output() {
let (registry, service) = job_registry_channel();
TokioGlobal
.spawn(async move { service.serve().await })
.detach();
let temp = tempdir().unwrap();
let redirect_path = temp.path().join("task-stream.log");
registry
.register(
101,
"task-stream",
"exec-stream",
"printf one",
TerminalMode::Sandboxed,
None,
)
.await;
registry
.append_stdout(101, b"line1\nline2\n".to_vec())
.await;
let snapshot = registry
.start_output_redirect("task-stream", redirect_path.clone())
.await
.unwrap();
assert_eq!(String::from_utf8_lossy(&snapshot), "line1\nline2\n");
registry.append_stderr(101, b"line3\n".to_vec()).await;
registry.complete(101, 0, None).await;
let job = registry.get(101).await.unwrap();
assert!(matches!(job.status, JobStatus::Completed { exit_code: 0 }));
let redirected = async_fs::read(&redirect_path).await.unwrap();
assert_eq!(
String::from_utf8_lossy(&redirected),
"line1\nline2\nline3\n"
);
}
#[tokio::test]
async fn test_read_terminal_delta_reads_incrementally() {
let (registry, service) = job_registry_channel();
TokioGlobal
.spawn(async move { service.serve().await })
.detach();
registry
.register(
202,
"task-delta",
"exec-delta",
"tail -f log",
TerminalMode::Sandboxed,
None,
)
.await;
registry.append_stdout(202, b"hello ".to_vec()).await;
registry.append_stderr(202, b"world".to_vec()).await;
let first = registry.terminal_read("task-delta", 0, 6).await.unwrap();
assert_eq!(String::from_utf8_lossy(&first.bytes), "hello ");
assert_eq!(first.cursor, 6);
assert_eq!(first.total_bytes, 11);
assert!(matches!(first.status, JobStatus::Running));
let second = registry
.terminal_read("task-delta", first.cursor, 32)
.await
.unwrap();
assert_eq!(String::from_utf8_lossy(&second.bytes), "world");
assert_eq!(second.cursor, 11);
assert_eq!(second.total_bytes, 11);
}
#[tokio::test]
async fn test_kill_by_execution_key_no_match() {
let (registry, service) = job_registry_channel();
TokioGlobal
.spawn(async move { service.serve().await })
.detach();
registry
.register(
1,
"task-a",
"exec-a",
"echo hello",
TerminalMode::Sandboxed,
None,
)
.await;
registry.complete(1, 0, None).await;
let killed = registry.kill_by_execution_key("exec-b").await;
assert_eq!(killed, 0);
}
#[tokio::test]
async fn test_kill_switch_path_marks_job_killed() {
let (registry, service) = job_registry_channel();
TokioGlobal
.spawn(async move { service.serve().await })
.detach();
registry
.register(
555,
"task-kill-switch",
"exec-kill-switch",
"sleep 10",
TerminalMode::Sandboxed,
None,
)
.await;
let (kill_tx, kill_rx) = async_channel::bounded::<()>(1);
registry.attach_kill_switch(555, kill_tx).await;
assert!(registry.kill_by_task_id("task-kill-switch").await);
assert!(kill_rx.recv().await.is_ok());
let job = registry.get(555).await.expect("job should exist");
assert!(matches!(job.status, JobStatus::Killed));
}
#[tokio::test]
async fn test_complete_does_not_override_killed_status() {
let (registry, service) = job_registry_channel();
TokioGlobal
.spawn(async move { service.serve().await })
.detach();
registry
.register(
556,
"task-killed-complete",
"exec-killed-complete",
"sleep 10",
TerminalMode::Sandboxed,
None,
)
.await;
let (kill_tx, _kill_rx) = async_channel::bounded::<()>(1);
registry.attach_kill_switch(556, kill_tx).await;
assert!(registry.kill_by_task_id("task-killed-complete").await);
registry.complete(556, 0, None).await;
let job = registry.get(556).await.expect("job should exist");
assert!(matches!(job.status, JobStatus::Killed));
}
#[tokio::test]
async fn test_format_running_jobs_cuts_a_multibyte_script_at_a_boundary() {
let (registry, service) = job_registry_channel();
TokioGlobal
.spawn(async move { service.serve().await })
.detach();
let script = "日".repeat(30);
assert!(!script.is_char_boundary(SCRIPT_PREVIEW_BYTES));
registry
.register(
1,
"task-cjk",
"exec-cjk",
&script,
TerminalMode::Sandboxed,
None,
)
.await;
let running = registry.format_running_jobs().await;
assert!(
running.contains(&format!("`{}...`", "日".repeat(16))),
"{running}"
);
assert_eq!(script_preview("short"), "short");
assert_eq!(script_preview(&"x".repeat(50)), "x".repeat(50));
assert_eq!(
script_preview(&"x".repeat(51)),
format!("{}...", "x".repeat(50))
);
}
fn running_job(pid: u32) -> HashMap<u32, JobState> {
let mut jobs = HashMap::new();
register_job(
&mut jobs,
pid,
"task".to_string(),
"exec".to_string(),
"true".to_string(),
TerminalMode::Sandboxed,
None,
);
jobs
}
#[tokio::test]
async fn overlapping_kills_hold_the_deferred_ending_until_the_last_ends() {
let mut jobs = running_job(7);
assert!(matches!(begin_kill(&mut jobs, 7).await, KillPlan::Group(7)));
assert!(matches!(begin_kill(&mut jobs, 7).await, KillPlan::Group(7)));
complete_job(&mut jobs, 7, 0, None).await;
assert_eq!(jobs[&7].info.status, JobStatus::Running, "the ending waits");
assert!(!end_kill(&mut jobs, 7, false).await);
assert_eq!(
jobs[&7].info.status,
JobStatus::Running,
"the other kill still runs"
);
assert!(!end_kill(&mut jobs, 7, false).await);
assert_eq!(
jobs[&7].info.status,
JobStatus::Completed { exit_code: 0 },
"the last kill to fail releases the ending"
);
}
#[tokio::test]
async fn a_successful_kill_wins_over_a_deferred_ending() {
let mut jobs = running_job(8);
assert!(matches!(begin_kill(&mut jobs, 8).await, KillPlan::Group(8)));
complete_job(&mut jobs, 8, 0, None).await;
assert!(end_kill(&mut jobs, 8, true).await);
assert_eq!(jobs[&8].info.status, JobStatus::Killed);
}
}