pub mod cli;
pub mod dispatcher;
pub mod routes;
pub mod spaces_hub;
pub mod spec;
pub mod store;
#[cfg(test)]
pub(crate) mod fakehub;
use crate::agent::core::Core;
use spec::{JobSpec, SpecError};
use std::path::Path;
use std::sync::{Arc, Mutex};
use store::{Job, Store};
use tokio::sync::{watch, Notify};
pub struct Outbox {
pub store: Store,
pub wake: Arc<Notify>,
submit_lock: Mutex<()>,
}
#[derive(Debug)]
pub enum SubmitError {
Spec(SpecError),
Io(String),
}
impl Outbox {
pub fn new(home: &Path) -> Self {
Self {
store: Store::new(home),
wake: Arc::new(Notify::new()),
submit_lock: Mutex::new(()),
}
}
pub fn submit(&self, spec: JobSpec) -> Result<(Job, bool), SubmitError> {
let checked = spec::validate(&spec).map_err(SubmitError::Spec)?;
let _guard = self.submit_lock.lock().unwrap();
if let Some(rid) = &spec.request_id {
if let Some(old) = self
.store
.list()
.into_iter()
.find(|j| j.spec.request_id.as_ref() == Some(rid))
{
return Ok((old, true));
}
}
let job = Job::new(spec, checked);
self.store
.create(&job)
.map_err(|e| SubmitError::Io(e.to_string()))?;
self.wake.notify_one();
Ok((job, false))
}
}
pub fn spawn(core: Arc<Core>, stop: watch::Receiver<bool>) {
let watch = Mutex::new(crate::agent::hub::CredWatch::new(
core.cfg.state_dir.join("credentials"),
));
let fallback = core.cfg.api_url.clone();
let creds: Arc<dyn Fn() -> dispatcher::Creds + Send + Sync> = Arc::new(move || {
let c = watch.lock().unwrap().current()?;
Some((c.api_url.unwrap_or_else(|| fallback.clone()), c.api_key))
});
let events = core.clone();
let emit: dispatcher::Emit = Arc::new(move |kind, data| {
events.events.publish(kind, data);
});
let d = dispatcher::Dispatcher::new(
core.outbox.store.clone(),
creds,
emit,
dispatcher::Config::default(),
core.outbox.wake.clone(),
);
tokio::spawn(d.run(stop));
}