pipelight_exec/lib.rs
1//! ## Warning - Unstable crate.
2//!
3//! This crate is still in development and undergoing API changes.
4//!
5//! It is internally used in the **pipelight** cicd engine:
6//! <https://github.com/pipelight/pipelight>
7//!
8//! ## Breanking changes
9//!
10//! - v0.4 : New API for easier process manipulation.
11//! p.run_detached() becomes p.detach().run();
12//!
13//! ## About
14//!
15//! A crate for easy process management (on Unix systems).
16//!
17//! It makes a best effort to leverage
18//! the [standard library process crate](https://doc.rust-lang.org/std/process/struct.Command.html).
19//!
20//! Features:
21//!
22//! - Get process execution time.
23//! - Spawn and Kill background processes.
24//! - Display a running process standard inputs/outputs.
25//!
26//! - Interoperability with [rustix](https://docs.rs/rustix/latest/rustix/)
27//! and [sysinfo](https://docs.rs/sysinfo/latest/sysinfo/)
28//! crates.
29//!
30//! ## Usage
31//! ### Spawn a process
32//!
33//! Spawn a simple process in the background.
34//! or in other words, execute a process and detach it.
35//!
36//! It keeps running after parent process exit and terminal exit.
37//!
38//! ```rust
39//! # use pipelight_exec::Process;
40//! # use miette::Report;
41//!
42//! // Runs a child process and wait until execution is over.
43//! let mut p = Process::new()
44//! .stdin("echo test")
45//! .to_owned();
46//! p.run()?;
47//!
48//! # Ok::<(), Report>(())
49//! ```
50//!
51//! Runs child process in the background.
52//! Do not wait until process is over and return as soon as child is spawned.
53//!
54//! ```rust
55//! # use pipelight_exec::Process;
56//! # use miette::Report;
57//!
58//! let mut p = Process::new()
59//! .stdin("echo test")
60//! .background()
61//! .to_owned();
62//! p.run()?;
63//!
64//! # Ok::<(), Report>(())
65//! ```
66//!
67//! Runs a child process and wait for it to return,
68//! but do not print stdout and stderr to console.
69//!
70//! In other words it is detached from standards inputs and outputs.
71//!
72//! ```rust
73//! # use pipelight_exec::Process;
74//! # use miette::Report;
75//!
76//! let mut p = Process::new()
77//! .stdin("echo test")
78//! .detach()
79//! .to_owned();
80//! p.run()?;
81//!
82//! # Ok::<(), Report>(())
83//! ```
84//!
85//! Runs a disowned child process that won't be killed if parent is killed.
86//!
87//! ```rust
88//! # use pipelight_exec::Process;
89//! # use miette::Report;
90//!
91//! let mut p = Process::new()
92//! .stdin("echo test")
93//! .orphan()
94//! .to_owned();
95//! p.run()?;
96//!
97//! # Ok::<(), Report>(())
98//! ```
99//!
100//! Runs a disowned child process that won't be killed if parent is killed.
101//! and stores process outputs in ./.pipelight/proc/:uuid/
102//! Practical if you want to process child output from another program.
103//!
104//! ```rust
105//! # use pipelight_exec::Process;
106//! # use miette::Report;
107//!
108//! let mut p = Process::new()
109//! .stdin("echo test")
110//! .fs()
111//! .detach()
112//! .to_owned();
113//! p.run()?;
114//!
115//! # Ok::<(), Report>(())
116//! ```
117//!
118//! Read a process i/o.
119//!
120//! ```rust,ignore
121//! # use pipelight_exec::Process;
122//! # use miette::{Report, IntoDiagnostic};
123//!
124//! let mut p = Process::new()
125//! .stdin("echo test")
126//! .background()
127//! .fs()
128//! .to_owned();
129//! p.run()?;
130//!
131//! // Later in execution
132//! p.io.read().into_diagnostic()?;
133//! println!("{:?}", p.io.stdout); // Some("stuff\n")
134//!
135//! # Ok::<(), Report>(())
136//! ```
137//!
138//!
139//! ### Find a process.
140//!
141//! Find a running process, with handy search options.
142//!
143//! ```rust
144//! # use pipelight_exec::Finder;
145//! # use miette::Report;
146//!
147//! let process_finder = Finder::new().seed("my_proc").root("/my/dir").search()?;
148//!
149//! let pid = 1792;
150//! let process_finder = Finder::new().pid(&pid).search()?;
151//!
152//! # Ok::<(), Report>(())
153//! ```
154//!
155//! ## Beware - Unconventional process management
156//!
157//! In linux, a process inputs and outputs are
158//! exposed at /proc/:process_id/fd/:fd_id
159//! and are deleted as soon as the process finishes its execution.
160//!
161//! A very good straightforward rustacean overview at [procfs crate](https://docs.rs/procfs/0.17.0/procfs/)
162//!
163//! To keep track of running and dead processes, this crate can
164//! redirect some process i/o into its own managed files.
165//! Pipelight managed processes are stored in a the .pipelight/proc/ directory.
166//! It has the same structure as your /proc/.
167//!
168//! However, to ease ones developer life, standard outputs are polled, read and finally stored as text files.
169//! /proc/:pid/2 (file descriptor / buffer)-> ./pipelight/proc/:uuid/2(text file)
170//!
171//! For example,
172//! The following code runs a process whose outputs are redirected
173//! to pipelight temporary filesystem thanks to the `fs()` method.
174//!
175//! ```rust,ignore
176//! # use pipelight_exec::Process;
177//! # use miette::{Report, IntoDiagnostic};
178//!
179//! let p = Process::new().stdin("pwd").fs().run()?;
180//! p.io.read().into_diagnostic()?;
181//! let stdout: Option<String> = p.io.stdout;
182//!
183//! # Ok::<(), Report>(())
184//! ```
185//! It allows us to read a process outputs **during** and long **after** execution.
186
187// Crate lint rules
188#[allow(unused_variables)]
189#[allow(unused_imports)]
190#[allow(unused_must_use)]
191//
192pub mod dates;
193mod globals;
194mod io;
195mod process;
196mod state;
197
198// Re-export
199pub use io::*;
200pub use process::*;
201pub use state::statuable::Statuable;
202pub use state::*;