Skip to content

Commit

Permalink
fix: changed std::process::Command to tokio::process::Command to …
Browse files Browse the repository at this point in the history
…be non blocking

Signed-off-by: ESPIE <[email protected]>
  • Loading branch information
remi-espie committed May 2, 2024
1 parent 10127f0 commit f91d524
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
17 changes: 10 additions & 7 deletions src/agent/src/agents/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use std::collections::HashSet;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::{fs::create_dir_all, process::Command};
use std::{fs::create_dir_all};
use tokio::process::Command;
use tokio::sync::Mutex;

#[derive(Deserialize)]
Expand Down Expand Up @@ -39,21 +40,23 @@ impl RustAgent {
.spawn()
.expect("Failed to build function");

child_processes.lock().await.insert(child.id());
child_processes.lock().await.insert(child.id().unwrap());

let output = child.wait_with_output().expect("Failed to wait on child");
let output = child.wait_with_output().await.expect("Failed to wait on child");

Ok(AgentOutput {
exit_code: output.status.code().unwrap(),
stdout: std::str::from_utf8(&output.stdout).unwrap().to_string(),
stderr: std::str::from_utf8(&output.stderr).unwrap().to_string(),
})
} else {
let output = Command::new("cargo")
let child = Command::new("cargo")
.arg("build")
.current_dir(function_dir)
.output()
.spawn()
.expect("Failed to build function");

let output = child.wait_with_output().await.expect("Failed to wait on child");

Ok(AgentOutput {
exit_code: output.status.code().unwrap(),
Expand Down Expand Up @@ -158,9 +161,9 @@ impl Agent for RustAgent {
.spawn()
.expect("Failed to run function");

child_processes.lock().await.insert(child.id());
child_processes.lock().await.insert(child.id().unwrap());

let output = child.wait_with_output().expect("Failed to wait on child");
let output = child.wait_with_output().await.expect("Failed to wait on child");

let agent_output = AgentOutput {
exit_code: output.status.code().unwrap(),
Expand Down
8 changes: 5 additions & 3 deletions src/agent/src/workload/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ impl WorkloadRunner for WorkloadRunnerService {
let child_processes = CHILD_PROCESSES.lock().await;

for &child_id in child_processes.iter() {
nix::sys::signal::kill(
match nix::sys::signal::kill(
nix::unistd::Pid::from_raw(child_id as i32),
nix::sys::signal::Signal::SIGTERM,
)
.unwrap();
) {
Ok(_) => println!("Sent SIGTERM to child process {}", child_id),
Err(e) => println!("Failed to send SIGTERM to child process {}: {}", child_id, e),
}
}

process::exit(0);
Expand Down

0 comments on commit f91d524

Please sign in to comment.