archivist_lib/
component_lifecycle.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use 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};

/// Takes the startup handle for LIFECYCLE and returns a stream listening for Lifecycle FIDL
/// requests on it.
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)
}

/// Serves the Lifecycle protocol from the component runtime used for controlled shutdown of the
/// archivist. When Stop is requrested executes the given callback.
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
        }
    }
}