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,17 +45,40 @@ 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 actors = config
|
||||
.forges()
|
||||
.flat_map(|(forge_name, forge)| create_forge_repos(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();
|
||||
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)| {
|
||||
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");
|
||||
|
@ -61,31 +86,24 @@ pub fn start(fs: FileSystem) {
|
|||
&repo_name,
|
||||
repo,
|
||||
&forge_name,
|
||||
forge,
|
||||
&forge,
|
||||
));
|
||||
actors.push((forge_name.clone(), repo_name, actor));
|
||||
info!("Created Repo");
|
||||
});
|
||||
});
|
||||
info!("Sending StartRepo to all actors...");
|
||||
(forge_name.clone(), repo_name, actor)
|
||||
}
|
||||
}
|
||||
|
||||
actix::System::new().block_on(async {
|
||||
let mut addresses: Vec<Addr<actors::repo::RepoActor>> = vec![];
|
||||
actors.into_iter().for_each(|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 actor");
|
||||
info!("Starting");
|
||||
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;
|
||||
info!("Ctrl-C received, shutting down...");
|
||||
drop(addresses);
|
||||
});
|
||||
info!("Started");
|
||||
addr
|
||||
}
|
||||
|
||||
pub fn init_logging() -> Result<(), tracing::subscriber::SetGlobalDefaultError> {
|
||||
|
|
Loading…
Reference in a new issue