use std::path::PathBuf;
use std::time::Duration;
use async_trait::async_trait;
use crate::fleet::{spawn_worker, SpawnedChild};
use crate::provision::ProvisionSpec;
use crate::transport::TransportResult;
#[async_trait]
pub trait WorkerLauncher: Send + Sync {
async fn launch(&self, spec: &ProvisionSpec, wait: Duration) -> TransportResult<SpawnedChild>;
}
pub struct LocalSubprocessLauncher {
pub worker_bin: PathBuf,
pub worker_args: Vec<String>,
}
impl LocalSubprocessLauncher {
pub fn new(worker_bin: impl Into<PathBuf>, worker_args: Vec<String>) -> Self {
Self {
worker_bin: worker_bin.into(),
worker_args,
}
}
}
#[async_trait]
impl WorkerLauncher for LocalSubprocessLauncher {
async fn launch(&self, spec: &ProvisionSpec, wait: Duration) -> TransportResult<SpawnedChild> {
spawn_worker(&self.worker_bin, &self.worker_args, spec, wait).await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn local_subprocess_launcher_is_a_trait_object() {
let launcher = LocalSubprocessLauncher::new("/bin/true", vec!["subagent-worker".into()]);
let _dyn: &dyn WorkerLauncher = &launcher;
assert_eq!(launcher.worker_bin, PathBuf::from("/bin/true"));
assert_eq!(launcher.worker_args, vec!["subagent-worker".to_string()]);
}
}