From ca673f95456e901ac5212c9befa43138a914309a Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 7 Apr 2024 20:16:04 +0100 Subject: [PATCH] feat(server): start repo actors --- src/server/mod.rs | 50 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/src/server/mod.rs b/src/server/mod.rs index 9e8054d..db104a4 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -1,11 +1,16 @@ mod actors; mod config; +use actix::prelude::*; + use std::path::PathBuf; use tracing::{error, info}; -use crate::filesystem::FileSystem; +use crate::{ + filesystem::FileSystem, + server::config::{ForgeName, RepoName}, +}; pub fn init(fs: FileSystem) { let file_name = "git-next-server.toml"; @@ -40,19 +45,50 @@ pub fn start(fs: FileSystem) { }; info!("Config loaded"); info!("Loaded config"); - config.forges().for_each(|(name, forge)| { - let span = tracing::info_span!("Forge", name); + let mut actors: Vec<(ForgeName, RepoName, actors::repo::RepoActor)> = vec![]; + config.forges().for_each(|(forge_name, forge)| { + let forge_name = ForgeName(forge_name.clone()); + let span = tracing::info_span!("Forge", %forge_name); let _guard = span.enter(); - info!("Forge: {}", name); + info!("Forge: {}", forge_name); info!("Forge Type: {}", forge.forge_type()); info!("Hostname: {}", forge.hostname()); info!("User: {}", forge.user()); - forge.repos().for_each(|(name, path)| { - let span = tracing::info_span!("Repo", name); + forge.repos().for_each(|(repo_name, path)| { + let repo_name = RepoName(repo_name.clone()); + let span = tracing::info_span!("Repo", %repo_name); let _guard = span.enter(); - info!("Repo: {} - {}", name, path); + info!("Repo: {} - {}", repo_name, path); + let actor = actors::repo::RepoActor { + defailt: config::RepoDetails { + name: repo_name.clone(), + path: config::RepoPath(path.clone()), + forge: (&forge_name, forge).into(), + }, + }; + actors.push((forge_name.clone(), repo_name.clone(), actor)); + info!("Created actor for repo: {} - {}", repo_name, path); }); }); + info!("Sending StartRepo to all actors..."); + + actix::System::new().block_on(async { + let mut addresses: Vec> = 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; + info!("Ctrl-C received, shutting down..."); + drop(addresses); + }); } pub fn init_logging() -> Result<(), tracing::subscriber::SetGlobalDefaultError> {