Macro zng_env::on_process_start
source · macro_rules! on_process_start { ($closure:expr) => { ... }; }
Expand description
Register a FnOnce(&ProcessStartArgs)
closure to be called on init!
.
Components that spawn special process instances implemented on the same executable can use this macro to inject their own “main” without needing to ask the user to plug an init function on the executable main. The component can spawn an instance of the current executable with marker environment variables that identify the component’s process.
§Examples
The example below declares a “main” for a foo component and a function that spawns it.
zng_env::on_process_start!(|_| {
if std::env::var("FOO_MARKER").is_ok() {
println!("Spawned as foo!");
zng_env::exit(0);
}
});
fn main() {
zng_env::init!(); // foo_main OR
// normal main
}
pub fn spawn_foo() -> std::io::Result<()> {
std::process::Command::new(std::env::current_exe()?).env("FOO_MARKER", "").spawn()?;
Ok(())
}
Note the use of exit
, it is important to call it to collaborate with on_process_exit
handlers.
§App Context
This event happens on the executable process context, before any APP
context starts, you can use
zng::app::on_app_start
here to register a handler to be called in the app context, if and when it starts.