use std::future::Future;
use std::panic::Location;
use std::sync::Arc;
use std::time::Duration;
use crate::app::load;
use crate::app::shared::AppShared;
use crate::binding::Qualifier;
use crate::dependency::Dep;
use crate::error::{Closed, DispatchStage, LoadError, LookupError, RuntimeMissing, Shutdown, ShutdownError};
use crate::execution::notify::Draining;
use crate::execution::trace::{TraceMode, TraceSource};
use crate::execution::{ExecOptions, Execution};
use crate::lifecycle::phase::Phase as Stage;
use crate::lifecycle::shutdown;
use crate::module::Module;
use crate::module::handle::ModuleRef;
use crate::redact::{Redacted, redact};
use crate::runtime::Runtime;
use crate::signal::Signal;
use crate::timer::{BoxError, Timer};
use crate::transport::Transport;
use crate::transport::controller::MountedHandler;
use crate::transport::handler::HandlerInfo;
use crate::transport::pipeline::caught;
use crate::transport::server::mounted_parts;
#[derive(Clone)]
pub struct AppHandle {
pub(crate) shared: Arc<AppShared>,
}
impl AppHandle {
pub async fn get<T: ?Sized + Send + Sync + 'static>(&self) -> Result<Dep<T>, LookupError> {
self.shared.get_root::<T>().await
}
pub fn module<M: 'static>(&self) -> Result<ModuleRef, LookupError> {
self.shared.find_module::<M>(None)
}
pub fn module_keyed<M: 'static, Q: 'static>(&self) -> Result<ModuleRef, LookupError> {
self.shared.find_module::<M>(Some(Qualifier::of::<Q>()))
}
pub fn module_of<M: Module>(&self, config: &M) -> Result<ModuleRef, LookupError> {
self.shared.module_of(config)
}
pub async fn execute<F, R>(&self, opts: ExecOptions, f: F) -> Result<R, Closed>
where
F: AsyncFnOnce(&Execution) -> R,
{
let root = self.shared.graph().root;
self.shared.execute(root, opts, f).await
}
#[track_caller]
pub fn load(&self, module: impl Module) -> impl Future<Output = Result<ModuleRef, LoadError>> {
let at = Location::caller();
async move { load::load(&self.shared, Box::new(module), at).await }
}
pub fn close(&self, signal: Signal) -> impl Future<Output = Result<Shutdown, ShutdownError>> + Send + use<> {
shutdown::close(&self.shared, signal)
}
pub fn draining(&self) -> Draining<'_> {
Draining::new(self.shared.draining.listen())
}
pub fn is_draining(&self) -> bool {
self.shared.draining.is_fired()
}
pub fn drain_timeout(&self) -> Duration {
self.shared.config.drain()
}
pub fn timer(&self) -> Option<&Arc<dyn Timer>> {
self.shared.config.timer.as_ref()
}
pub fn trace_source(&self) -> Option<&Arc<dyn TraceSource>> {
self.shared.trace_source.get()
}
pub fn trace_mode(&self) -> TraceMode {
self.shared.config.trace
}
pub fn runtime(&self) -> Option<&Arc<dyn Runtime>> {
self.shared.config.runtime.as_ref()
}
pub fn phase(&self) -> Phase {
match self.shared.phase.get() {
Stage::Wired | Stage::Connecting | Stage::Running => Phase::Running,
Stage::Stopping => Phase::Stopping,
Stage::Draining => Phase::Draining,
Stage::Destroying => Phase::Destroying,
Stage::Closed => Phase::Closed,
}
}
pub fn redact(&self, err: BoxError) -> Redacted {
redact(&self.shared.graph().secrets, err)
}
pub fn handlers(&self) -> Vec<Arc<HandlerInfo>> {
self.shared.graph().handlers.iter().map(|handler| Arc::clone(&handler.info)).collect()
}
pub fn mounted<T: Transport>(&self) -> Result<Vec<MountedHandler<T>>, RuntimeMissing> {
mounted_parts::<T>(self).map(|(handlers, _)| handlers)
}
pub fn root(&self) -> ModuleRef {
self.shared.root()
}
pub async fn catch_panic<R, F>(&self, stage: DispatchStage, fut: F) -> Result<R, BoxError>
where
F: Future<Output = Result<R, BoxError>>,
{
let graph = self.shared.graph();
caught(stage, &graph.secrets, fut).await
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Phase {
Running,
Stopping,
Draining,
Destroying,
Closed,
}