mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 10:36:00 +02:00
Implemented job wait
This commit adds a `job wait` command so users can wait for the completion of another job. For this, a data structure for joining and signaling job completion was implemented (since the native rust `JoinHandle` cannot be cloned).
This commit is contained in:
@ -453,6 +453,7 @@ pub fn add_shell_command_context(mut engine_state: EngineState) -> EngineState {
|
||||
JobList,
|
||||
JobKill,
|
||||
JobTag,
|
||||
JobWait,
|
||||
Job,
|
||||
};
|
||||
|
||||
|
@ -8,7 +8,7 @@ use std::{
|
||||
|
||||
use nu_engine::{command_prelude::*, ClosureEvalOnce};
|
||||
use nu_protocol::{
|
||||
engine::{Closure, Job, Redirection, ThreadJob},
|
||||
engine::{Closure, Job, Redirection, ThreadJob, WaitSignal},
|
||||
report_shell_error, OutDest, Signals,
|
||||
};
|
||||
|
||||
@ -54,7 +54,9 @@ impl Command for JobSpawn {
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
|
||||
let closure: Closure = call.req(engine_state, stack, 0)?;
|
||||
let spanned_closure: Spanned<Closure> = call.req(engine_state, stack, 0)?;
|
||||
|
||||
let closure: Closure = spanned_closure.item;
|
||||
|
||||
let tag: Option<String> = call.get_flag(engine_state, stack, "tag")?;
|
||||
|
||||
@ -75,8 +77,10 @@ impl Command for JobSpawn {
|
||||
let jobs = job_state.jobs.clone();
|
||||
let mut jobs = jobs.lock().expect("jobs lock is poisoned!");
|
||||
|
||||
let on_termination = Arc::new(WaitSignal::new());
|
||||
|
||||
let id = {
|
||||
let thread_job = ThreadJob::new(job_signals, tag);
|
||||
let thread_job = ThreadJob::new(job_signals, tag, on_termination.clone());
|
||||
job_state.current_thread_job = Some(thread_job.clone());
|
||||
jobs.add_job(Job::Thread(thread_job))
|
||||
};
|
||||
@ -89,15 +93,19 @@ impl Command for JobSpawn {
|
||||
Some(Redirection::Pipe(OutDest::Null)),
|
||||
Some(Redirection::Pipe(OutDest::Null)),
|
||||
);
|
||||
ClosureEvalOnce::new_preserve_out_dest(&job_state, &stack, closure)
|
||||
let result_value = ClosureEvalOnce::new(&job_state, &stack, closure)
|
||||
.run_with_input(Value::nothing(head).into_pipeline_data())
|
||||
.and_then(|data| data.drain())
|
||||
.and_then(|data| data.into_value(spanned_closure.span))
|
||||
.unwrap_or_else(|err| {
|
||||
if !job_state.signals().interrupted() {
|
||||
report_shell_error(&job_state, &err);
|
||||
}
|
||||
|
||||
Value::error(err, head)
|
||||
});
|
||||
|
||||
on_termination.signal(result_value);
|
||||
|
||||
{
|
||||
let mut jobs = job_state.jobs.lock().expect("jobs lock is poisoned!");
|
||||
|
||||
|
82
crates/nu-command/src/experimental/job_wait.rs
Normal file
82
crates/nu-command/src/experimental/job_wait.rs
Normal file
@ -0,0 +1,82 @@
|
||||
use nu_engine::command_prelude::*;
|
||||
use nu_protocol::{engine::Job, JobId};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct JobWait;
|
||||
|
||||
impl Command for JobWait {
|
||||
fn name(&self) -> &str {
|
||||
"job wait"
|
||||
}
|
||||
|
||||
fn description(&self) -> &str {
|
||||
"Wait for a job to complete and return its result value"
|
||||
}
|
||||
|
||||
fn signature(&self) -> nu_protocol::Signature {
|
||||
Signature::build("job wait")
|
||||
.category(Category::Experimental)
|
||||
.required("id", SyntaxShape::Int, "The id of the running to wait for.")
|
||||
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
|
||||
}
|
||||
|
||||
fn search_terms(&self) -> Vec<&str> {
|
||||
vec!["join"]
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
|
||||
let id_arg: Spanned<i64> = call.req(engine_state, stack, 0)?;
|
||||
|
||||
if id_arg.item < 0 {
|
||||
return Err(ShellError::NeedsPositiveValue { span: id_arg.span });
|
||||
}
|
||||
|
||||
let id: JobId = JobId::new(id_arg.item as usize);
|
||||
|
||||
let mut jobs = engine_state.jobs.lock().expect("jobs lock is poisoned!");
|
||||
|
||||
match jobs.lookup_mut(id) {
|
||||
None => {
|
||||
return Err(ShellError::JobNotFound {
|
||||
id: id.get(),
|
||||
span: head,
|
||||
});
|
||||
}
|
||||
|
||||
Some(Job::Frozen { .. }) => {
|
||||
return Err(ShellError::UnsupportedJobType {
|
||||
id: id.get() as usize,
|
||||
span: head,
|
||||
kind: "frozen".to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
Some(Job::Thread(job)) => {
|
||||
let on_termination = job.on_termination().clone();
|
||||
|
||||
// .join() blocks so we drop our mutex guard
|
||||
drop(jobs);
|
||||
|
||||
let result = on_termination.join().clone().with_span(head);
|
||||
|
||||
Ok(result.into_pipeline_data())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
example: "let id = job spawn { sleep 5sec; 'hi there' }; job wait $id",
|
||||
description: "Wait for a job to complete",
|
||||
result: Some(Value::test_string("hi there")),
|
||||
}]
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ mod job_kill;
|
||||
mod job_list;
|
||||
mod job_spawn;
|
||||
mod job_tag;
|
||||
mod job_wait;
|
||||
|
||||
#[cfg(all(unix, feature = "os"))]
|
||||
mod job_unfreeze;
|
||||
@ -14,6 +15,7 @@ pub use job_kill::JobKill;
|
||||
pub use job_list::JobList;
|
||||
pub use job_spawn::JobSpawn;
|
||||
pub use job_tag::JobTag;
|
||||
pub use job_wait::JobWait;
|
||||
|
||||
#[cfg(all(unix, feature = "os"))]
|
||||
pub use job_unfreeze::JobUnfreeze;
|
||||
|
Reference in New Issue
Block a user