-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add `AppLifecycleHandler` trait with methods that are called at various stages of an app's lifecycle. The initial set of methods are the following: - `before_service_cli` - `before_health_checks` - `before_services` - `on_shutdown` Closes #350
- Loading branch information
1 parent
20f6827
commit 8342a9e
Showing
18 changed files
with
496 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
[app] | ||
shutdown-on-error = true | ||
|
||
[lifecycle-handler] | ||
default-enable = true | ||
|
||
[service] | ||
default-enable = true | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[lifecycle-handler.db-migration] | ||
priority = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
use crate::app::context::AppContext; | ||
use crate::config::app_config::CustomConfig; | ||
use crate::util::serde::default_true; | ||
use config::{FileFormat, FileSourceString}; | ||
use serde_derive::{Deserialize, Serialize}; | ||
use std::collections::BTreeMap; | ||
use validator::Validate; | ||
|
||
pub fn default_config() -> config::File<FileSourceString, FileFormat> { | ||
config::File::from_str(include_str!("default.toml"), FileFormat::Toml) | ||
} | ||
|
||
#[derive(Debug, Clone, Validate, Serialize, Deserialize)] | ||
#[serde(rename_all = "kebab-case")] | ||
#[non_exhaustive] | ||
pub struct LifecycleHandler { | ||
#[serde(default = "default_true")] | ||
pub default_enable: bool, | ||
|
||
#[cfg(feature = "db-sql")] | ||
#[validate(nested)] | ||
pub db_migration: LifecycleHandlerConfig<()>, | ||
|
||
/// Allows providing configs for custom lifecycle handlers. Any configs that aren't pre-defined | ||
/// above will be collected here. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```toml | ||
/// [lifecycle-handler.foo] | ||
/// enable = true | ||
/// x = "y" | ||
/// ``` | ||
/// | ||
/// This will be parsed as: | ||
/// ```raw | ||
/// LifecycleHandler#custom: { | ||
/// "foo": { | ||
/// LifecycleHandlerConfig#common: { | ||
/// enable: true, | ||
/// priority: 10 | ||
/// }, | ||
/// LifecycleHandlerConfig<CustomConfig>#custom: { | ||
/// "x": "y" | ||
/// } | ||
/// } | ||
/// } | ||
/// ``` | ||
#[serde(flatten)] | ||
pub custom: BTreeMap<String, LifecycleHandlerConfig<CustomConfig>>, | ||
} | ||
|
||
#[derive(Debug, Clone, Default, Serialize, Deserialize)] | ||
#[serde(rename_all = "kebab-case", default)] | ||
#[non_exhaustive] | ||
pub struct CommonConfig { | ||
// Optional so we can tell the difference between a consumer explicitly enabling/disabling | ||
// the lifecycle handler, vs the lifecycle handler being enabled/disabled by default. | ||
// If this is `None`, the value will match the value of `LifecycleHandler#default_enable`. | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub enable: Option<bool>, | ||
pub priority: i32, | ||
} | ||
|
||
impl CommonConfig { | ||
pub fn enabled(&self, context: &AppContext) -> bool { | ||
self.enable | ||
.unwrap_or(context.config().lifecycle_handler.default_enable) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Validate, Serialize, Deserialize)] | ||
#[serde(rename_all = "kebab-case")] | ||
#[non_exhaustive] | ||
pub struct LifecycleHandlerConfig<T> { | ||
#[serde(flatten, default)] | ||
pub common: CommonConfig, | ||
#[serde(flatten)] | ||
pub custom: T, | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::config::app_config::AppConfig; | ||
use rstest::rstest; | ||
|
||
#[rstest] | ||
#[case(true, None, true)] | ||
#[case(true, Some(true), true)] | ||
#[case(true, Some(false), false)] | ||
#[case(false, None, false)] | ||
#[case(false, Some(true), true)] | ||
#[case(false, Some(false), false)] | ||
#[cfg_attr(coverage_nightly, coverage(off))] | ||
fn common_config_enabled( | ||
#[case] default_enable: bool, | ||
#[case] enable: Option<bool>, | ||
#[case] expected_enabled: bool, | ||
) { | ||
// Arrange | ||
let mut config = AppConfig::test(None).unwrap(); | ||
config.lifecycle_handler.default_enable = default_enable; | ||
|
||
let context = AppContext::test(Some(config), None, None).unwrap(); | ||
|
||
let common_config = CommonConfig { | ||
enable, | ||
priority: 0, | ||
}; | ||
|
||
// Act/Assert | ||
assert_eq!(common_config.enabled(&context), expected_enabled); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.