archivist_lib/
component_lifecycle.rsuse fidl::endpoints::RequestStream;
use fidl_fuchsia_process_lifecycle::{LifecycleRequest, LifecycleRequestStream};
use fuchsia_async as fasync;
use fuchsia_runtime::{take_startup_handle, HandleInfo, HandleType};
use futures::{Future, StreamExt};
use tracing::{debug, warn};
pub fn take_lifecycle_request_stream() -> LifecycleRequestStream {
let lifecycle_handle_info = HandleInfo::new(HandleType::Lifecycle, 0);
let lifecycle_handle = take_startup_handle(lifecycle_handle_info)
.expect("must have been provided a lifecycle channel in procargs");
let async_chan = fasync::Channel::from_channel(lifecycle_handle.into());
LifecycleRequestStream::from_channel(async_chan)
}
pub async fn on_stop_request<F, Fut>(mut request_stream: LifecycleRequestStream, cb: F)
where
F: FnOnce() -> Fut,
Fut: Future<Output = ()>,
{
match request_stream.next().await {
None => {
warn!("Lifecycle closed");
}
Some(Err(err)) => {
warn!(?err, "Lifecycle error");
}
Some(Ok(LifecycleRequest::Stop { .. })) => {
debug!("Initiating shutdown.");
cb().await
}
}
}