refactor(server): reduce complexity starting server

This commit is contained in:
Paul Campbell 2024-04-09 07:20:13 +01:00
parent 1b0c09d3fe
commit 7e08640743
2 changed files with 63 additions and 39 deletions

View file

@ -42,7 +42,7 @@ pub struct RepoBranches {
dev: String, dev: String,
} }
#[derive(Debug, PartialEq, Eq, Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
pub struct Forge { pub struct Forge {
forge_type: ForgeType, forge_type: ForgeType,
hostname: String, hostname: String,
@ -53,6 +53,7 @@ pub struct Forge {
repos: HashMap<String, Repo>, repos: HashMap<String, Repo>,
} }
impl Forge { impl Forge {
#[allow(dead_code)]
pub const fn forge_type(&self) -> &ForgeType { pub const fn forge_type(&self) -> &ForgeType {
&self.forge_type &self.forge_type
} }
@ -75,8 +76,13 @@ impl Forge {
.map(|(name, repo)| (RepoName(name.clone()), repo)) .map(|(name, repo)| (RepoName(name.clone()), repo))
} }
} }
impl Display for Forge {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{} - {}@{}", self.forge_type, self.user, self.hostname)
}
}
#[derive(Debug, PartialEq, Eq, Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
pub struct Repo { pub struct Repo {
repo: String, repo: String,
branch: String, branch: String,

View file

@ -9,9 +9,11 @@ use tracing::{error, info};
use crate::{ use crate::{
filesystem::FileSystem, filesystem::FileSystem,
server::config::{ForgeName, RepoName}, server::config::{Forge, ForgeName, RepoName},
}; };
use self::{actors::repo::RepoActor, config::Repo};
pub fn init(fs: FileSystem) { pub fn init(fs: FileSystem) {
let file_name = "git-next-server.toml"; let file_name = "git-next-server.toml";
let path = PathBuf::from(file_name); let path = PathBuf::from(file_name);
@ -43,51 +45,67 @@ pub fn start(fs: FileSystem) {
return; return;
} }
}; };
info!("Config loaded"); info!("Config loaded");
info!("Loaded config"); let actors = config
let mut actors: Vec<(ForgeName, RepoName, actors::repo::RepoActor)> = vec![]; .forges()
config.forges().for_each(|(forge_name, forge)| { .flat_map(|(forge_name, forge)| create_forge_repos(forge, forge_name))
let span = tracing::info_span!("Forge", %forge_name); .collect::<Vec<_>>();
let _guard = span.enter();
info!("Forge: {}", forge_name);
info!("Forge Type: {}", forge.forge_type());
info!("Hostname: {}", forge.hostname());
info!("User: {}", forge.user());
forge.repos().for_each(|(repo_name, repo)| {
let span = tracing::info_span!("Repo", %repo_name, %repo);
let _guard = span.enter();
info!("Creating Repo");
let actor = actors::repo::RepoActor::new(config::RepoDetails::new(
&repo_name,
repo,
&forge_name,
forge,
));
actors.push((forge_name.clone(), repo_name, actor));
info!("Created Repo");
});
});
info!("Sending StartRepo to all actors...");
actix::System::new().block_on(async { actix::System::new().block_on(async {
let mut addresses: Vec<Addr<actors::repo::RepoActor>> = vec![]; let addresses = actors.into_iter().map(start_actor).collect::<Vec<_>>();
actors.into_iter().for_each(|actor| {
let (forge_name, repo_name, actor) = actor;
let span = tracing::info_span!("Forge/Repo", %forge_name, %repo_name);
let _guard = span.enter();
info!("Starting actor");
let addr = actor.start();
info!("Sending StartRepo to actor");
addr.do_send(actors::repo::StartRepo);
addresses.push(addr);
info!("Sent StartRepo to actor");
});
let _ = actix_rt::signal::ctrl_c().await; let _ = actix_rt::signal::ctrl_c().await;
info!("Ctrl-C received, shutting down..."); info!("Ctrl-C received, shutting down...");
drop(addresses); drop(addresses);
}); });
} }
fn create_forge_repos(
forge: &Forge,
forge_name: ForgeName,
) -> Vec<(ForgeName, RepoName, RepoActor)> {
let forge = forge.clone();
let span = tracing::info_span!("Forge", %forge_name, %forge);
let _guard = span.enter();
info!("Creating Forge");
forge
.repos()
.map(create_actor(forge_name, forge.clone()))
.collect::<Vec<_>>()
}
fn create_actor(
forge_name: ForgeName,
forge: config::Forge,
) -> impl Fn((RepoName, &Repo)) -> (ForgeName, RepoName, RepoActor) {
move |(repo_name, repo)| {
let span = tracing::info_span!("Repo", %repo_name, %repo);
let _guard = span.enter();
info!("Creating Repo");
let actor = actors::repo::RepoActor::new(config::RepoDetails::new(
&repo_name,
repo,
&forge_name,
&forge,
));
info!("Created Repo");
(forge_name.clone(), repo_name, actor)
}
}
fn start_actor(
actor: (ForgeName, RepoName, actors::repo::RepoActor),
) -> Addr<actors::repo::RepoActor> {
let (forge_name, repo_name, actor) = actor;
let span = tracing::info_span!("Forge/Repo", %forge_name, %repo_name);
let _guard = span.enter();
info!("Starting");
let addr = actor.start();
addr.do_send(actors::repo::StartRepo);
info!("Started");
addr
}
pub fn init_logging() -> Result<(), tracing::subscriber::SetGlobalDefaultError> { pub fn init_logging() -> Result<(), tracing::subscriber::SetGlobalDefaultError> {
use tracing_subscriber::prelude::*; use tracing_subscriber::prelude::*;