archivist_lib/
component_lifecycle.rs1use fidl::endpoints::RequestStream;
6use fidl_fuchsia_process_lifecycle::{LifecycleRequest, LifecycleRequestStream};
7use fuchsia_async as fasync;
8use fuchsia_runtime::{take_startup_handle, HandleInfo, HandleType};
9use futures::{Future, StreamExt};
10use log::{debug, warn};
11
12pub fn take_lifecycle_request_stream() -> LifecycleRequestStream {
15 let lifecycle_handle_info = HandleInfo::new(HandleType::Lifecycle, 0);
16 let lifecycle_handle = take_startup_handle(lifecycle_handle_info)
17 .expect("must have been provided a lifecycle channel in procargs");
18 let async_chan = fasync::Channel::from_channel(lifecycle_handle.into());
19 LifecycleRequestStream::from_channel(async_chan)
20}
21
22pub async fn on_stop_request<F, Fut>(mut request_stream: LifecycleRequestStream, cb: F)
25where
26 F: FnOnce() -> Fut,
27 Fut: Future<Output = ()>,
28{
29 match request_stream.next().await {
30 None => {
31 warn!("Lifecycle closed");
32 }
33 Some(Err(err)) => {
34 warn!(err:?; "Lifecycle error");
35 }
36 Some(Ok(LifecycleRequest::Stop { .. })) => {
37 debug!("Initiating shutdown.");
38 cb().await
39 }
40 }
41}