zc2 0.0.53

P2P compute broker with yen-denominated billing, WAL, and broker mesh support
//! Unattended jobs (ADR ZC-0021): a job spec, a durable outbox, a dispatcher
//! that drives the job through the hub's Spaces queue, and the
//! `/v2/submissions` routes and `zc job` verbs on top.
//!
//! The logic lives here so the shared agent files only carry a field on
//! `Core`, one route merge and one task spawn.

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};

/// What `Core` holds: the store and the doorbell that wakes the dispatcher.
pub struct Outbox {
    pub store: Store,
    pub wake: Arc<Notify>,
    /// Serialises submissions so a repeated `request_id` cannot make two jobs.
    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(()),
        }
    }

    /// Validate, write the job to disk (fsynced) and ring the dispatcher.
    /// When this returns `Ok` the job survives a crash. The flag says the
    /// `request_id` had been submitted before and that job is returned.
    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))
    }
}

/// Start the dispatcher on the agent's runtime. It stops when `stop` turns
/// true; whatever it was doing is picked up again from the outbox on the next
/// start.
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));
}