scientific-workflow 0.18.1

Configuration-driven scientific tasks, typed state, and durable recordings
Documentation
//! Socket-only controller SDK; it does not link the execution library.
pub use crate::protocol::{Command, Event, Outcome, Response, Status};
use crate::protocol::{Request, VERSION, read_frame, write_frame};
use std::io;
use std::os::unix::net::UnixStream;
use std::path::{Path, PathBuf};
/// Reconnectable engine handle. Each request uses its own connection.
#[derive(Clone, Debug)]
pub struct RunClient {
    socket: PathBuf,
}
impl RunClient {
    /// Connects and negotiates the supported wire version.
    pub fn connect(socket: impl AsRef<Path>) -> io::Result<Self> {
        let client = Self {
            socket: socket.as_ref().into(),
        };
        client.request(Command::Hello)?;
        Ok(client)
    }
    /// Sends one command; acknowledgements include the resulting canonical snapshot.
    pub fn request(&self, command: Command) -> io::Result<Response> {
        let mut stream = UnixStream::connect(&self.socket)?;
        stream.set_write_timeout(Some(std::time::Duration::from_secs(10)))?;
        if !matches!(command, Command::Wait) {
            stream.set_read_timeout(Some(std::time::Duration::from_secs(30)))?;
        }
        let id = format!(
            "{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_nanos()
        );
        write_frame(
            &mut stream,
            &Request {
                version: VERSION,
                id: id.clone(),
                command,
            },
        )?;
        let response: Response = read_frame(&mut stream)?;
        if response.version != VERSION || response.id != id {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "mismatched protocol response",
            ));
        }
        if let Some(error) = &response.error {
            return Err(io::Error::other(format!(
                "{error}: {}",
                response.message.as_deref().unwrap_or("")
            )));
        }
        Ok(response)
    }
    /// Reads current status. Connection failure means liveness is unknown.
    pub fn status(&self) -> io::Result<Status> {
        Ok(self.request(Command::Status)?.status)
    }
    /// Returns a durable page after `after`; reconnect with the last received sequence.
    pub fn events(&self, after: u64) -> io::Result<Response> {
        self.request(Command::Events { after })
    }
    /// Blocks for the actual terminal outcome; disconnection is an error.
    pub fn wait(&self) -> io::Result<Status> {
        Ok(self.request(Command::Wait)?.status)
    }
    /// Pauses scientific execution at cooperative boundaries.
    pub fn pause(&self) -> io::Result<Status> {
        Ok(self.request(Command::Pause)?.status)
    }
    /// Resumes after safety gates recover.
    pub fn resume(&self) -> io::Result<Status> {
        Ok(self.request(Command::Resume)?.status)
    }
    /// Cancels active and pending work.
    pub fn cancel(&self) -> io::Result<Status> {
        Ok(self.request(Command::Cancel)?.status)
    }
    /// Local socket endpoint.
    pub fn socket(&self) -> &Path {
        &self.socket
    }
}