forked from kemitix/git-next
refactor(server): reduce complexity starting server
This commit is contained in:
parent
1b0c09d3fe
commit
7e08640743
2 changed files with 63 additions and 39 deletions
|
@ -42,7 +42,7 @@ pub struct RepoBranches {
|
|||
dev: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Deserialize)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
|
||||
pub struct Forge {
|
||||
forge_type: ForgeType,
|
||||
hostname: String,
|
||||
|
@ -53,6 +53,7 @@ pub struct Forge {
|
|||
repos: HashMap<String, Repo>,
|
||||
}
|
||||
impl Forge {
|
||||
#[allow(dead_code)]
|
||||
pub const fn forge_type(&self) -> &ForgeType {
|
||||
&self.forge_type
|
||||
}
|
||||
|
@ -75,8 +76,13 @@ impl Forge {
|
|||
.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 {
|
||||
repo: String,
|
||||
branch: String,
|
||||
|
|
|
@ -9,9 +9,11 @@ use tracing::{error, info};
|
|||
|
||||
use crate::{
|
||||
filesystem::FileSystem,
|
||||
server::config::{ForgeName, RepoName},
|
||||
server::config::{Forge, ForgeName, RepoName},
|
||||
};
|
||||
|
||||
use self::{actors::repo::RepoActor, config::Repo};
|
||||
|
||||
pub fn init(fs: FileSystem) {
|
||||
let file_name = "git-next-server.toml";
|
||||
let path = PathBuf::from(file_name);
|
||||
|
@ -43,51 +45,67 @@ pub fn start(fs: FileSystem) {
|
|||
return;
|
||||
}
|
||||
};
|
||||
|
||||
info!("Config loaded");
|
||||
info!("Loaded config");
|
||||
let mut actors: Vec<(ForgeName, RepoName, actors::repo::RepoActor)> = vec![];
|
||||
config.forges().for_each(|(forge_name, forge)| {
|
||||
let span = tracing::info_span!("Forge", %forge_name);
|
||||
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...");
|
||||
let actors = config
|
||||
.forges()
|
||||
.flat_map(|(forge_name, forge)| create_forge_repos(forge, forge_name))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
actix::System::new().block_on(async {
|
||||
let mut addresses: Vec<Addr<actors::repo::RepoActor>> = 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 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();
|
||||
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> {
|
||||
use tracing_subscriber::prelude::*;
|
||||
|
||||
|
|
Loading…
Reference in a new issue