pub trait AppLifecycleHandler<A, S>: Send + Sync{
// Required method
fn name(&self) -> String;
// Provided methods
fn enabled(&self, _state: &S) -> bool { ... }
fn priority(&self, _state: &S) -> i32 { ... }
fn before_health_checks<'life0, 'life1, 'async_trait>(
&'life0 self,
_prepared_app: &'life1 PreparedAppWithoutCli<A, S>,
) -> Pin<Box<dyn Future<Output = RoadsterResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn before_services<'life0, 'life1, 'async_trait>(
&'life0 self,
_prepared_app: &'life1 PreparedAppWithoutCli<A, S>,
) -> Pin<Box<dyn Future<Output = RoadsterResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn on_shutdown<'life0, 'life1, 'async_trait>(
&'life0 self,
_state: &'life1 S,
) -> Pin<Box<dyn Future<Output = RoadsterResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
}Expand description
Trait used to hook into various stages of the app’s lifecycle.
This trait has some overlap with the crate::health::check::HealthCheck. For example, both
traits have methods that are called during app startup before the app’s services are started.
However, health checks are intended to be used both during app startup and in the
/api/_health API endpoint. This enforces a constraint on the
crate::health::check::HealthCheck trait that doesn’t exist for the AppLifecycleHandler
trait – the crate::health::check::HealthCheck is stored in the
crate::app::context::AppContext, which means it can’t take the state as a type parameter and
therefore also can’t take it as parameter to its methods. Because this constraint only exists
for the health check use case, we split out the other lifecycle hooks into a separate trait that
allows better ergonomics – instead of needing to store a weak reference to the app context as
in a health check, AppLifecycleHandler can simply accept the context as a parameter on its
methods.
The app’s lifecycle generally looks something like this:
- Parse the
crate::config::AppConfig - Initialize tracing to enable logs/traces
- Build the
crate::app::context::AppContextand thecrate::app::App’s custom state - Run the roadster/app CLI command, if one was specified when the app was started
- Register
AppLifecycleHandlers,crate::health::check::HealthChecks, andcrate::service::Services - Run the app’s registered
AppLifecycleHandler::before_health_checkshooks. - Run the registered
crate::health::check::HealthChecks - Run the app’s registered
AppLifecycleHandler::before_serviceshooks. - Run the registered
crate::service::Services - Wait for a shutdown signal, e.g.,
Ctrl+cor a custom signal fromcrate::app::App::graceful_shutdown_signal, and stop thecrate::service::Services when the signal is received. - Run Roadster’s graceful shutdown logic
- Run the app’s registered
AppLifecycleHandler::on_shutdownhooks.
Required Methods§
Sourcefn name(&self) -> String
fn name(&self) -> String
The name of the AppLifecycleHandler.
Provided Methods§
Sourcefn enabled(&self, _state: &S) -> bool
fn enabled(&self, _state: &S) -> bool
Whether the AppLifecycleHandler is enabled.
Sourcefn priority(&self, _state: &S) -> i32
fn priority(&self, _state: &S) -> i32
Used to determine the order in which the AppLifecycleHandler will run when during app
startup. Smaller numbers will run before larger numbers. For example, a
AppLifecycleHandler with priority -10 will run before a AppLifecycleHandler
with priority 10.
If two AppLifecycleHandlers have the same priority, they are not guaranteed to run
in any particular order relative to each other. This may be fine for many
AppLifecycleHandlers .
If the order in which your AppLifecycleHandler runs doesn’t particularly matter, it’s
generally safe to set its priority as 0.
Sourcefn before_health_checks<'life0, 'life1, 'async_trait>(
&'life0 self,
_prepared_app: &'life1 PreparedAppWithoutCli<A, S>,
) -> Pin<Box<dyn Future<Output = RoadsterResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn before_health_checks<'life0, 'life1, 'async_trait>(
&'life0 self,
_prepared_app: &'life1 PreparedAppWithoutCli<A, S>,
) -> Pin<Box<dyn Future<Output = RoadsterResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
This method is run right before the app’s crate::health::check::HealthChecks during
app startup.
Sourcefn before_services<'life0, 'life1, 'async_trait>(
&'life0 self,
_prepared_app: &'life1 PreparedAppWithoutCli<A, S>,
) -> Pin<Box<dyn Future<Output = RoadsterResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn before_services<'life0, 'life1, 'async_trait>(
&'life0 self,
_prepared_app: &'life1 PreparedAppWithoutCli<A, S>,
) -> Pin<Box<dyn Future<Output = RoadsterResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
This method is run right before the app’s crate::service::Services are started.
Sourcefn on_shutdown<'life0, 'life1, 'async_trait>(
&'life0 self,
_state: &'life1 S,
) -> Pin<Box<dyn Future<Output = RoadsterResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn on_shutdown<'life0, 'life1, 'async_trait>(
&'life0 self,
_state: &'life1 S,
) -> Pin<Box<dyn Future<Output = RoadsterResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
This method is run after the app’s crate::service::Services have stopped.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".