isothrum_client/lib.rs
1//! The client of the Isothrum runner service.
2//!
3//! One service per runner home (`isothrum serve --home <home>`) runs every operation of that
4//! home: one at a time per run, runs side by side, each operation's events streamed to every
5//! subscriber. An app or the `isothrum` command line is its client: it sends commands, and
6//! follows the events.
7//!
8//! [`Client::connect`] finds the home's service through `<home>/service.json` and starts one
9//! when none answers. The wire protocol is in [`proto`]: newline-delimited JSON over a Unix domain
10//! socket in the home (owner-only) or a Windows named pipe (the user's only).
11//!
12//! # A service of another release
13//!
14//! [`Client::connect`] takes the service it finds, whatever its release (it fails only when the
15//! service speaks another protocol). An app that bundles Isothrum wants its own release:
16//! [`Client::connect_exact`] connects only to a service of this crate's [`VERSION`] and protocol,
17//! started from the build of the binary it would start ([`ServiceInfo::build`]: an app updated in
18//! place carries the same release, and its old service still runs the old program). It finds the
19//! one that runs, and when it is another release's or another build's:
20//!
21//! - it asks it to stop when idle (`shutdown_if_idle`, which every protocol keeps): one with
22//! operations queued or running is left running, and [`ClientError::Busy`] names them, for the
23//! app to tell its user (and connect again once they ended);
24//! - one that doesn't answer is ended (its pid, from `service.json`) only when the home's
25//! service lock is still held, the process with that pid started no later than the service
26//! said it did (a pid reused since is never ended), and the home's journal shows no operation
27//! of it unfinished;
28//! - once it is gone (its lock released, its endpoint closed), `binary` is started, as
29//! [`Client::connect`] starts it, and the client connects, checking the new service is this
30//! release ([`ClientError::Version`] when `binary` is another).
31//!
32//! The app reads none of the home's files itself. Two apps replacing one home's service take
33//! turns (`<home>/service.replace.lock`).
34//!
35//! ```no_run
36//! # async fn demo(bundled: &std::path::Path) -> Result<(), isothrum_client::ClientError> {
37//! use isothrum_client::{Client, ClientError};
38//!
39//! match Client::connect_exact(std::path::Path::new("/home/ada/.isothrum"), Some(bundled)).await {
40//! Ok(client) => assert_eq!(client.service().version, isothrum_client::VERSION),
41//! Err(ClientError::Busy { version, operations, .. }) => {
42//! println!("Isothrum {version} is still running {} operation(s): try again once they end", operations.len());
43//! }
44//! Err(e) => return Err(e),
45//! }
46//! # Ok(())
47//! # }
48//! ```
49//!
50//! ```no_run
51//! # async fn demo() -> Result<(), isothrum_client::ClientError> {
52//! use isothrum_client::{Client, proto::{Command, OpOptions, Target}};
53//!
54//! let client = Client::connect(std::path::Path::new("/home/ada/.isothrum"), None).await?;
55//! let up = Command::Up {
56//! target: Target::new("/stacks/web", None),
57//! options: OpOptions::default(),
58//! lifetime_s: None,
59//! };
60//! let mut operation = client.follow(up).await?;
61//! while let Some(event) = operation.next().await {
62//! println!("{:?}", event?);
63//! }
64//! # Ok(())
65//! # }
66//! ```
67
68mod client;
69mod discovery;
70mod error;
71mod pool;
72pub mod proto;
73mod replace;
74mod spawn;
75pub mod v1;
76pub mod wire;
77
78pub use client::{Accepted, Client, Followed, HeldConsole, Subscription};
79pub use discovery::{BUILD_ENV, ServiceInfo, ServicePaths, build_of};
80pub use error::ClientError;
81pub use isothrum_runner::InputVariable;
82pub use pool::Pool;
83
84/// This crate's release: the version [`Client::connect_exact`] wants of a service.
85pub const VERSION: &str = env!("CARGO_PKG_VERSION");