Skip to main content

AppLifecycleHandler

Trait AppLifecycleHandler 

Source
pub trait AppLifecycleHandler<A, S>: Send + Sync
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>, A: App<S> + 'static,
{ // 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:

  1. Parse the crate::config::AppConfig
  2. Initialize tracing to enable logs/traces
  3. Build the crate::app::context::AppContext and the crate::app::App’s custom state
  4. Run the roadster/app CLI command, if one was specified when the app was started
  5. Register AppLifecycleHandlers, crate::health::check::HealthChecks, and crate::service::Services
  6. Run the app’s registered AppLifecycleHandler::before_health_checks hooks.
  7. Run the registered crate::health::check::HealthChecks
  8. Run the app’s registered AppLifecycleHandler::before_services hooks.
  9. Run the registered crate::service::Services
  10. Wait for a shutdown signal, e.g., Ctrl+c or a custom signal from crate::app::App::graceful_shutdown_signal, and stop the crate::service::Services when the signal is received.
  11. Run Roadster’s graceful shutdown logic
  12. Run the app’s registered AppLifecycleHandler::on_shutdown hooks.

Required Methods§

Source

fn name(&self) -> String

The name of the AppLifecycleHandler.

Provided Methods§

Source

fn enabled(&self, _state: &S) -> bool

Whether the AppLifecycleHandler is enabled.

Source

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.

Source

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.

Source

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.

Source

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".

Implementors§

Source§

impl<A, S> AppLifecycleHandler<A, S> for DbMigrationLifecycleHandler
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>, A: App<S> + 'static,

Source§

impl<A, S> AppLifecycleHandler<A, S> for DbSeaOrmGracefulShutdownLifecycleHandler
where S: Clone + Send + Sync + 'static, AppContext: FromRef<S>, A: App<S> + 'static,