refactor(server): reduce complexity starting server
All checks were successful
ci/woodpecker/push/docker Pipeline was successful
ci/woodpecker/push/todo-check Pipeline was successful
ci/woodpecker/push/release Pipeline was successful
ci/woodpecker/push/build Pipeline was successful

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,17 +45,40 @@ 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<_>>();
actix::System::new().block_on(async {
let addresses = actors.into_iter().map(start_actor).collect::<Vec<_>>();
let _ = actix_rt::signal::ctrl_c().await;
info!("Ctrl-C received, shutting down...");
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(); let _guard = span.enter();
info!("Forge: {}", forge_name); info!("Creating Forge");
info!("Forge Type: {}", forge.forge_type()); forge
info!("Hostname: {}", forge.hostname()); .repos()
info!("User: {}", forge.user()); .map(create_actor(forge_name, forge.clone()))
forge.repos().for_each(|(repo_name, repo)| { .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 span = tracing::info_span!("Repo", %repo_name, %repo);
let _guard = span.enter(); let _guard = span.enter();
info!("Creating Repo"); info!("Creating Repo");
@ -61,31 +86,24 @@ pub fn start(fs: FileSystem) {
&repo_name, &repo_name,
repo, repo,
&forge_name, &forge_name,
forge, &forge,
)); ));
actors.push((forge_name.clone(), repo_name, actor));
info!("Created Repo"); info!("Created Repo");
}); (forge_name.clone(), repo_name, actor)
}); }
info!("Sending StartRepo to all actors..."); }
actix::System::new().block_on(async { fn start_actor(
let mut addresses: Vec<Addr<actors::repo::RepoActor>> = vec![]; actor: (ForgeName, RepoName, actors::repo::RepoActor),
actors.into_iter().for_each(|actor| { ) -> Addr<actors::repo::RepoActor> {
let (forge_name, repo_name, actor) = actor; let (forge_name, repo_name, actor) = actor;
let span = tracing::info_span!("Forge/Repo", %forge_name, %repo_name); let span = tracing::info_span!("Forge/Repo", %forge_name, %repo_name);
let _guard = span.enter(); let _guard = span.enter();
info!("Starting actor"); info!("Starting");
let addr = actor.start(); let addr = actor.start();
info!("Sending StartRepo to actor");
addr.do_send(actors::repo::StartRepo); addr.do_send(actors::repo::StartRepo);
addresses.push(addr); info!("Started");
info!("Sent StartRepo to actor"); addr
});
let _ = actix_rt::signal::ctrl_c().await;
info!("Ctrl-C received, shutting down...");
drop(addresses);
});
} }
pub fn init_logging() -> Result<(), tracing::subscriber::SetGlobalDefaultError> { pub fn init_logging() -> Result<(), tracing::subscriber::SetGlobalDefaultError> {