Skip to content

Commit

Permalink
Runtime framework start with '--model' arg.
Browse files Browse the repository at this point in the history
  • Loading branch information
pwalski committed Oct 27, 2023
1 parent 09fc708 commit ec0886e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
4 changes: 3 additions & 1 deletion runtimes/dummy/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::env;
use std::io;

fn main() {
let mut input = String::new();
let args: Vec<String> = env::args().collect();

println!("Dummy");
println!("Dummy runtime. Args: {args:?}");

loop {
input.clear();
Expand Down
17 changes: 15 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,11 @@ async fn run<T: process::Runtime + Clone + Unpin + 'static>(cli: Cli) -> anyhow:
});
}
ExeScriptCommand::Start { args, .. } => {
log::debug!("Start cmd args: {args:?}");
let args = process::RuntimeArgs::new(args).map_err(|e| {
log::debug!("Raw Start cmd args: {args:?}");
let args = T::parse_args(args).map_err(|e| {
RpcMessageError::Activity(format!("invalid args: {}", e))
})?;
log::debug!("Start cmd model: {}", args.model);

send_state(
&report_url,
Expand All @@ -255,6 +256,7 @@ async fn run<T: process::Runtime + Clone + Unpin + 'static>(cli: Cli) -> anyhow:
.start(&args)
.await
.map_err(|e| RpcMessageError::Activity(e.to_string()))?;
log::debug!("Started process");

send_state(
&report_url,
Expand Down Expand Up @@ -337,3 +339,14 @@ async fn run<T: process::Runtime + Clone + Unpin + 'static>(cli: Cli) -> anyhow:

Ok(())
}

#[cfg(test)]
mod tests {
use crate::process::{self, RuntimeArgs};

#[test]
fn arg_test() {
let args = vec!["--model".into(), "dummy".into()];
process::RuntimeArgs::new(&args).unwrap();
}
}
6 changes: 4 additions & 2 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub struct Shares {
}

pub trait Runtime {
fn parse_args(args: &[String]) -> anyhow::Result<RuntimeArgs>;

fn start(args: &RuntimeArgs) -> anyhow::Result<Child>;

fn run<ReportFn: Fn(Shares) + 'static>(stdout: ChildStdout, report_fn: ReportFn);
Expand All @@ -33,8 +35,8 @@ pub struct RuntimeArgs {
}

impl RuntimeArgs {
pub fn new(args: &[String]) -> anyhow::Result<Self> {
Ok(Self::try_parse_from(args)?)
pub fn new(cmd: &String, args: &[String]) -> anyhow::Result<Self> {
Ok(Self::try_parse_from(std::iter::once(cmd).chain(args))?)
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/process/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ pub struct Dummy {}
impl Unpin for Dummy {}

impl Runtime for Dummy {
fn start(_args: &RuntimeArgs) -> anyhow::Result<Child> {
fn parse_args(args: &[String]) -> anyhow::Result<RuntimeArgs> {
RuntimeArgs::new(&"dummy.exe".into(), args)
}

fn start(args: &RuntimeArgs) -> anyhow::Result<Child> {
let exe = super::find_exe("dummy.exe")?;
let mut cmd = Command::new(&exe);
let work_dir = exe.parent().unwrap();
cmd.stdout(Stdio::piped())
.stdin(Stdio::null())
.current_dir(work_dir);

.current_dir(work_dir)
.arg("--model")
.arg(&args.model);
Ok(cmd.kill_on_drop(true).spawn()?)
}

Expand Down
1 change: 1 addition & 0 deletions src/process/win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use winapi::um::handleapi::INVALID_HANDLE_VALUE;
#[cfg(target_os = "windows")]
use winapi::um::winnt::HANDLE;

#[cfg(target_os = "windows")]
use std::{mem, ptr};

#[cfg(target_os = "windows")]
Expand Down

0 comments on commit ec0886e

Please sign in to comment.