Skip to content

Commit

Permalink
health check + better bootstrap + format
Browse files Browse the repository at this point in the history
  • Loading branch information
TroyKomodo committed Apr 29, 2024
1 parent d34f1a5 commit eaa9de9
Show file tree
Hide file tree
Showing 33 changed files with 5,543 additions and 5,822 deletions.
6 changes: 6 additions & 0 deletions foundations/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ metrics = [
"once_cell",
]

health-check = [
"scc",
"telemetry-server",
"once_cell",
]

settings = [
"serde",
"serde_yaml",
Expand Down
2 changes: 2 additions & 0 deletions foundations/examples/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ struct Config {
}

impl Bootstrap for Config {
type Settings = Self;

fn runtime_mode(&self) -> RuntimeSettings {
self.runtime.clone()
}
Expand Down
2 changes: 2 additions & 0 deletions foundations/examples/src/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub struct HttpServerSettings {
}

impl Bootstrap for HttpServerSettings {
type Settings = Self;

fn runtime_mode(&self) -> RuntimeSettings {
self.runtime.clone()
}
Expand Down
118 changes: 58 additions & 60 deletions foundations/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,80 @@ use std::future::Future;

use anyhow::Context;
use scuffle_foundations_macros::auto_settings;

pub use scuffle_foundations_macros::bootstrap;

use crate::{
settings::{
cli::{Cli, Matches},
Settings,
},
BootstrapResult,
};
use crate::settings::cli::{Cli, Matches};
use crate::settings::Settings;
use crate::BootstrapResult;

pub fn bootstrap<
C: Bootstrap + std::fmt::Debug,
F: Fn(Matches<C>) -> Fut,
Fut: Future<Output = anyhow::Result<()>>,
>(
config: &C,
info: crate::ServiceInfo,
main: F,
pub fn bootstrap<C: Bootstrap, F: Fn(Matches<C>) -> Fut, Fut: Future<Output = anyhow::Result<()>>>(
default_settings: &C::Settings,
info: crate::ServiceInfo,
main: F,
) -> BootstrapResult<()> {
let mut cli = Cli::<C>::new(config).with_service_info(info);
let mut cli = Cli::<C::Settings>::new(default_settings).with_service_info(info);

for arg in C::additional_args() {
cli = cli.with_arg(arg);
}

for arg in C::additional_args() {
cli = cli.with_arg(arg);
}
let matches = cli.parse()?;

let matches = cli.parse()?;
let matches = Matches {
settings: C::from(matches.settings),
args: matches.args,
};

let runtime = match matches.settings.runtime_mode() {
RuntimeSettings::Steal { name, threads } => {
crate::runtime::Runtime::new_steal(threads, &name)
}
RuntimeSettings::NoSteal { name, threads } => {
crate::runtime::Runtime::new_no_steal(threads, &name)
}
}
.context("Failed to create runtime")?;
let runtime = match matches.settings.runtime_mode() {
RuntimeSettings::Steal { name, threads } => crate::runtime::Runtime::new_steal(threads, &name),
RuntimeSettings::NoSteal { name, threads } => crate::runtime::Runtime::new_no_steal(threads, &name),
}
.context("Failed to create runtime")?;

runtime.block_on(async move {
#[cfg(feature = "_telemetry")]
if let Some(telemetry) = matches.settings.telemetry_config() {
crate::telementry::settings::init(info, telemetry).await;
}
runtime.block_on(async move {
#[cfg(feature = "_telemetry")]
if let Some(telemetry) = matches.settings.telemetry_config() {
crate::telementry::settings::init(info, telemetry).await;
}

main(matches).await
})
main(matches).await
})
}

#[auto_settings(crate_path = "crate")]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum RuntimeSettings {
Steal {
threads: usize,
name: String,
},
#[settings(default)]
NoSteal {
threads: usize,
name: String,
},
Steal {
threads: usize,
name: String,
},
#[settings(default)]
NoSteal {
threads: usize,
name: String,
},
}

pub trait Bootstrap: serde::Serialize + serde::de::DeserializeOwned + Settings {
fn runtime_mode(&self) -> RuntimeSettings {
RuntimeSettings::NoSteal {
threads: num_cpus::get(),
name: String::new(),
}
}
pub trait Bootstrap: Sized + From<Self::Settings> {
type Settings: serde::Serialize + serde::de::DeserializeOwned + Settings;

fn runtime_mode(&self) -> RuntimeSettings {
RuntimeSettings::NoSteal {
threads: num_cpus::get(),
name: String::new(),
}
}

#[cfg(feature = "_telemetry")]
fn telemetry_config(&self) -> Option<crate::telementry::settings::TelementrySettings> {
None
}
#[cfg(feature = "_telemetry")]
fn telemetry_config(&self) -> Option<crate::telementry::settings::TelementrySettings> {
None
}

fn additional_args() -> Vec<clap::Arg> {
vec![]
}
}

fn additional_args() -> Vec<clap::Arg> {
vec![]
}
impl Bootstrap for () {
type Settings = Self;
}
Loading

0 comments on commit eaa9de9

Please sign in to comment.