Skip to main content

scientific_workflow_client/
lib.rs

1//! Socket-only controller SDK; it does not link the execution library.
2pub use scientific_workflow_protocol::{Command, Event, Outcome, Response, Status};
3use scientific_workflow_protocol::{Request, VERSION, read_frame, write_frame};
4use std::io;
5use std::os::unix::net::UnixStream;
6use std::path::{Path, PathBuf};
7/// Reconnectable engine handle. Each request uses its own connection.
8#[derive(Clone, Debug)]
9pub struct RunClient {
10    socket: PathBuf,
11}
12impl RunClient {
13    /// Connects and negotiates the supported wire version.
14    pub fn connect(socket: impl AsRef<Path>) -> io::Result<Self> {
15        let client = Self {
16            socket: socket.as_ref().into(),
17        };
18        client.request(Command::Hello)?;
19        Ok(client)
20    }
21    /// Sends one command; acknowledgements include the resulting canonical snapshot.
22    pub fn request(&self, command: Command) -> io::Result<Response> {
23        let mut stream = UnixStream::connect(&self.socket)?;
24        stream.set_write_timeout(Some(std::time::Duration::from_secs(10)))?;
25        if !matches!(command, Command::Wait) {
26            stream.set_read_timeout(Some(std::time::Duration::from_secs(30)))?;
27        }
28        let id = format!(
29            "{}-{}",
30            std::process::id(),
31            std::time::SystemTime::now()
32                .duration_since(std::time::UNIX_EPOCH)
33                .unwrap_or_default()
34                .as_nanos()
35        );
36        write_frame(
37            &mut stream,
38            &Request {
39                version: VERSION,
40                id: id.clone(),
41                command,
42            },
43        )?;
44        let response: Response = read_frame(&mut stream)?;
45        if response.version != VERSION || response.id != id {
46            return Err(io::Error::new(
47                io::ErrorKind::InvalidData,
48                "mismatched protocol response",
49            ));
50        }
51        if let Some(error) = &response.error {
52            return Err(io::Error::other(format!(
53                "{error}: {}",
54                response.message.as_deref().unwrap_or("")
55            )));
56        }
57        Ok(response)
58    }
59    /// Reads current status. Connection failure means liveness is unknown.
60    pub fn status(&self) -> io::Result<Status> {
61        Ok(self.request(Command::Status)?.status)
62    }
63    /// Returns a durable page after `after`; reconnect with the last received sequence.
64    pub fn events(&self, after: u64) -> io::Result<Response> {
65        self.request(Command::Events { after })
66    }
67    /// Blocks for the actual terminal outcome; disconnection is an error.
68    pub fn wait(&self) -> io::Result<Status> {
69        Ok(self.request(Command::Wait)?.status)
70    }
71    /// Pauses scientific execution at cooperative boundaries.
72    pub fn pause(&self) -> io::Result<Status> {
73        Ok(self.request(Command::Pause)?.status)
74    }
75    /// Resumes after safety gates recover.
76    pub fn resume(&self) -> io::Result<Status> {
77        Ok(self.request(Command::Resume)?.status)
78    }
79    /// Cancels active and pending work.
80    pub fn cancel(&self) -> io::Result<Status> {
81        Ok(self.request(Command::Cancel)?.status)
82    }
83    /// Local socket endpoint.
84    pub fn socket(&self) -> &Path {
85        &self.socket
86    }
87}