feat(server): extract Server actor
This commit is contained in:
parent
96994f2390
commit
9b211179bf
4 changed files with 199 additions and 173 deletions
|
@ -1,2 +1,3 @@
|
||||||
pub mod repo;
|
pub mod repo;
|
||||||
|
pub mod server;
|
||||||
pub mod webhook;
|
pub mod webhook;
|
||||||
|
|
190
src/server/actors/server.rs
Normal file
190
src/server/actors/server.rs
Normal file
|
@ -0,0 +1,190 @@
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use actix::prelude::*;
|
||||||
|
|
||||||
|
use kxio::{fs::FileSystem, network::Network};
|
||||||
|
use tracing::{error, info};
|
||||||
|
|
||||||
|
use crate::server::{
|
||||||
|
actors::{
|
||||||
|
repo::{CloneRepo, RepoActor},
|
||||||
|
webhook::{AddWebhookRecipient, WebhookActor, WebhookRouter},
|
||||||
|
},
|
||||||
|
config::{
|
||||||
|
ForgeConfig, ForgeName, GitDir, RepoAlias, RepoDetails, ServerConfig, ServerRepoConfig,
|
||||||
|
ServerStorage, Webhook,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug, derive_more::Display, derive_more::From)]
|
||||||
|
pub enum Error {
|
||||||
|
#[display("Failed to create data directories")]
|
||||||
|
FailedToCreateDataDirectory(kxio::fs::Error),
|
||||||
|
|
||||||
|
#[display("The forge data path is not a directory: {path:?}")]
|
||||||
|
ForgeDirIsNotDirectory {
|
||||||
|
path: PathBuf,
|
||||||
|
},
|
||||||
|
|
||||||
|
Config(crate::server::config::Error),
|
||||||
|
|
||||||
|
Io(std::io::Error),
|
||||||
|
}
|
||||||
|
type Result<T> = core::result::Result<T, Error>;
|
||||||
|
|
||||||
|
pub struct Server {
|
||||||
|
fs: FileSystem,
|
||||||
|
net: Network,
|
||||||
|
}
|
||||||
|
impl Actor for Server {
|
||||||
|
type Context = Context<Self>;
|
||||||
|
}
|
||||||
|
impl Handler<ServerConfig> for Server {
|
||||||
|
type Result = ();
|
||||||
|
|
||||||
|
#[allow(clippy::cognitive_complexity)] // TODO: (#75) reduce complexity
|
||||||
|
fn handle(&mut self, msg: ServerConfig, _ctx: &mut Self::Context) -> Self::Result {
|
||||||
|
let server_config = msg;
|
||||||
|
// Server Storage
|
||||||
|
let dir = server_config.storage().path();
|
||||||
|
if !dir.exists() {
|
||||||
|
info!(?dir, "server storage doesn't exist - creating it");
|
||||||
|
if let Err(err) = self.fs.dir_create(dir) {
|
||||||
|
error!(?err, ?dir, "Failed to create server storage");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let Ok(canon) = dir.canonicalize() else {
|
||||||
|
error!(?dir, "Failed to confirm server storage");
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
info!(dir = ?canon, "server storage");
|
||||||
|
|
||||||
|
// Forge directories in Server Storage
|
||||||
|
let server_storage = server_config.storage();
|
||||||
|
if let Err(err) = self.create_forge_data_directories(&server_config, dir) {
|
||||||
|
error!(?err, "Failure creating forge storage");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Webhook Server
|
||||||
|
info!("Starting Webhook Server...");
|
||||||
|
let webhook_router = WebhookRouter::new().start();
|
||||||
|
let webhook = server_config.webhook();
|
||||||
|
|
||||||
|
// Forge Actors
|
||||||
|
for (forge_name, forge_config) in server_config.forges() {
|
||||||
|
self.create_forge_repos(forge_config, forge_name.clone(), server_storage, webhook)
|
||||||
|
.into_iter()
|
||||||
|
.map(|a| self.start_actor(a))
|
||||||
|
.map(|(alias, addr)| AddWebhookRecipient(alias, addr.recipient()))
|
||||||
|
.for_each(|msg| webhook_router.do_send(msg));
|
||||||
|
}
|
||||||
|
|
||||||
|
WebhookActor::new(webhook_router.recipient()).start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Server {
|
||||||
|
pub const fn new(fs: FileSystem, net: Network) -> Self {
|
||||||
|
Self { fs, net }
|
||||||
|
}
|
||||||
|
fn create_forge_data_directories(
|
||||||
|
&self,
|
||||||
|
server_config: &ServerConfig,
|
||||||
|
server_dir: &std::path::Path,
|
||||||
|
) -> Result<()> {
|
||||||
|
for (forge_name, _forge_config) in server_config.forges() {
|
||||||
|
let forge_dir: PathBuf = (&forge_name).into();
|
||||||
|
let path = server_dir.join(&forge_dir);
|
||||||
|
if self.fs.path_exists(&path)? {
|
||||||
|
if !self.fs.path_is_dir(&path)? {
|
||||||
|
return Err(Error::ForgeDirIsNotDirectory { path });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
info!(%forge_name, ?path, "creating storage");
|
||||||
|
self.fs.dir_create_all(&path)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_forge_repos(
|
||||||
|
&self,
|
||||||
|
forge_config: &ForgeConfig,
|
||||||
|
forge_name: ForgeName,
|
||||||
|
server_storage: &ServerStorage,
|
||||||
|
webhook: &Webhook,
|
||||||
|
) -> Vec<(ForgeName, RepoAlias, RepoActor)> {
|
||||||
|
let span =
|
||||||
|
tracing::info_span!("create_forge_repos", name = %forge_name, config = %forge_config);
|
||||||
|
|
||||||
|
let _guard = span.enter();
|
||||||
|
info!("Creating Forge");
|
||||||
|
let mut repos = vec![];
|
||||||
|
let creator = self.create_actor(forge_name, forge_config.clone(), server_storage, webhook);
|
||||||
|
for (repo_alias, server_repo_config) in forge_config.repos() {
|
||||||
|
let forge_repo = creator((repo_alias, server_repo_config));
|
||||||
|
info!(
|
||||||
|
alias = %forge_repo.1,
|
||||||
|
"Created Repo"
|
||||||
|
);
|
||||||
|
repos.push(forge_repo);
|
||||||
|
}
|
||||||
|
repos
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_actor(
|
||||||
|
&self,
|
||||||
|
forge_name: ForgeName,
|
||||||
|
forge_config: ForgeConfig,
|
||||||
|
server_storage: &ServerStorage,
|
||||||
|
webhook: &Webhook,
|
||||||
|
) -> impl Fn((RepoAlias, &ServerRepoConfig)) -> (ForgeName, RepoAlias, RepoActor) {
|
||||||
|
let server_storage = server_storage.clone();
|
||||||
|
let webhook = webhook.clone();
|
||||||
|
let net = self.net.clone();
|
||||||
|
move |(repo_alias, server_repo_config)| {
|
||||||
|
let span = tracing::info_span!("create_actor", alias = %repo_alias, config = %server_repo_config);
|
||||||
|
let _guard = span.enter();
|
||||||
|
info!("Creating Repo");
|
||||||
|
let gitdir = server_repo_config.gitdir().map_or_else(
|
||||||
|
|| {
|
||||||
|
GitDir::from(
|
||||||
|
server_storage
|
||||||
|
.path()
|
||||||
|
.join(forge_name.to_string())
|
||||||
|
.join(repo_alias.to_string()),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
|gitdir| gitdir,
|
||||||
|
);
|
||||||
|
// INFO: can't canonicalise gitdir as the path needs to exist to do that and we may not
|
||||||
|
// have cloned the repo yet
|
||||||
|
let repo_details = RepoDetails::new(
|
||||||
|
&repo_alias,
|
||||||
|
server_repo_config,
|
||||||
|
&forge_name,
|
||||||
|
&forge_config,
|
||||||
|
gitdir,
|
||||||
|
);
|
||||||
|
info!("Starting Repo Actor");
|
||||||
|
let actor = RepoActor::new(repo_details, webhook.clone(), net.clone());
|
||||||
|
(forge_name.clone(), repo_alias, actor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn start_actor(
|
||||||
|
&self,
|
||||||
|
actor: (ForgeName, RepoAlias, RepoActor),
|
||||||
|
) -> (RepoAlias, Addr<RepoActor>) {
|
||||||
|
let (forge_name, repo_alias, actor) = actor;
|
||||||
|
let span = tracing::info_span!("start_actor", forge = %forge_name, repo = %repo_alias);
|
||||||
|
let _guard = span.enter();
|
||||||
|
info!("Starting");
|
||||||
|
let addr = actor.start();
|
||||||
|
addr.do_send(CloneRepo);
|
||||||
|
info!("Started");
|
||||||
|
(repo_alias, addr)
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,5 @@
|
||||||
|
use actix::prelude::*;
|
||||||
|
|
||||||
pub mod load;
|
pub mod load;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
@ -23,7 +25,8 @@ impl std::error::Error for Error {}
|
||||||
type Result<T> = core::result::Result<T, Error>;
|
type Result<T> = core::result::Result<T, Error>;
|
||||||
|
|
||||||
/// Mapped from the `git-next-server.toml` file
|
/// Mapped from the `git-next-server.toml` file
|
||||||
#[derive(Debug, PartialEq, Eq, Deserialize)]
|
#[derive(Debug, PartialEq, Eq, Deserialize, Message)]
|
||||||
|
#[rtype(result = "()")]
|
||||||
pub struct ServerConfig {
|
pub struct ServerConfig {
|
||||||
webhook: Webhook,
|
webhook: Webhook,
|
||||||
storage: ServerStorage,
|
storage: ServerStorage,
|
||||||
|
|
|
@ -11,31 +11,7 @@ use std::path::PathBuf;
|
||||||
|
|
||||||
use tracing::{error, info, level_filters::LevelFilter};
|
use tracing::{error, info, level_filters::LevelFilter};
|
||||||
|
|
||||||
use crate::{
|
use crate::{fs::FileSystem, server::actors::server::Server};
|
||||||
fs::FileSystem,
|
|
||||||
server::{
|
|
||||||
actors::webhook,
|
|
||||||
config::{ForgeConfig, ForgeName, GitDir, RepoAlias, ServerStorage, Webhook},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
use self::{actors::repo::RepoActor, config::ServerRepoConfig};
|
|
||||||
|
|
||||||
#[derive(Debug, derive_more::Display, derive_more::From)]
|
|
||||||
pub enum Error {
|
|
||||||
#[display("Failed to create data directories")]
|
|
||||||
FailedToCreateDataDirectory(kxio::fs::Error),
|
|
||||||
|
|
||||||
#[display("The forge data path is not a directory: {path:?}")]
|
|
||||||
ForgeDirIsNotDirectory {
|
|
||||||
path: PathBuf,
|
|
||||||
},
|
|
||||||
|
|
||||||
Config(crate::server::config::Error),
|
|
||||||
|
|
||||||
Io(std::io::Error),
|
|
||||||
}
|
|
||||||
type Result<T> = core::result::Result<T, Error>;
|
|
||||||
|
|
||||||
pub fn init(fs: FileSystem) {
|
pub fn init(fs: FileSystem) {
|
||||||
let file_name = "git-next-server.toml";
|
let file_name = "git-next-server.toml";
|
||||||
|
@ -69,158 +45,14 @@ pub async fn start(fs: FileSystem, net: Network) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// Server Storage
|
|
||||||
let dir = server_config.storage().path();
|
|
||||||
if !dir.exists() {
|
|
||||||
info!(?dir, "server storage doesn't exist - creating it");
|
|
||||||
if let Err(err) = fs.dir_create(dir) {
|
|
||||||
error!(?err, ?dir, "Failed to create server storage");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let Ok(canon) = dir.canonicalize() else {
|
|
||||||
error!(?dir, "Failed to confirm server storage");
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
info!(dir = ?canon, "server storage");
|
|
||||||
|
|
||||||
// Forge directories in Server Storage
|
let server = Server::new(fs, net).start();
|
||||||
let server_storage = server_config.storage();
|
server.do_send(server_config);
|
||||||
if let Err(err) = create_forge_data_directories(&server_config, &fs, &dir) {
|
|
||||||
error!(?err, "Failure creating forge storage");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Webhook Server
|
|
||||||
info!("Starting Webhook Server...");
|
|
||||||
let webhook_router = webhook::WebhookRouter::new().start();
|
|
||||||
let webhook = server_config.webhook();
|
|
||||||
|
|
||||||
// Forge Actors
|
|
||||||
for (forge_name, forge_config) in server_config.forges() {
|
|
||||||
create_forge_repos(
|
|
||||||
forge_config,
|
|
||||||
forge_name.clone(),
|
|
||||||
server_storage,
|
|
||||||
webhook,
|
|
||||||
&net,
|
|
||||||
)
|
|
||||||
.into_iter()
|
|
||||||
.map(start_actor)
|
|
||||||
.map(|(alias, addr)| webhook::AddWebhookRecipient(alias, addr.recipient()))
|
|
||||||
.for_each(|msg| webhook_router.do_send(msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
let webhook_server = webhook::WebhookActor::new(webhook_router.recipient()).start();
|
|
||||||
info!("Server running - Press Ctrl-C to stop...");
|
info!("Server running - Press Ctrl-C to stop...");
|
||||||
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(webhook_server);
|
drop(server);
|
||||||
}
|
|
||||||
|
|
||||||
fn create_forge_data_directories(
|
|
||||||
server_config: &config::ServerConfig,
|
|
||||||
fs: &FileSystem,
|
|
||||||
server_dir: &&std::path::Path,
|
|
||||||
) -> Result<()> {
|
|
||||||
for (forge_name, _forge_config) in server_config.forges() {
|
|
||||||
let forge_dir: PathBuf = (&forge_name).into();
|
|
||||||
let path = server_dir.join(&forge_dir);
|
|
||||||
if fs.path_exists(&path)? {
|
|
||||||
if !fs.path_is_dir(&path)? {
|
|
||||||
return Err(Error::ForgeDirIsNotDirectory { path });
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
info!(%forge_name, ?path, "creating storage");
|
|
||||||
fs.dir_create_all(&path)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_forge_repos(
|
|
||||||
forge_config: &ForgeConfig,
|
|
||||||
forge_name: ForgeName,
|
|
||||||
server_storage: &ServerStorage,
|
|
||||||
webhook: &Webhook,
|
|
||||||
net: &Network,
|
|
||||||
) -> Vec<(ForgeName, RepoAlias, RepoActor)> {
|
|
||||||
let span =
|
|
||||||
tracing::info_span!("create_forge_repos", name = %forge_name, config = %forge_config);
|
|
||||||
let _guard = span.enter();
|
|
||||||
info!("Creating Forge");
|
|
||||||
let mut repos = vec![];
|
|
||||||
let creator = create_actor(
|
|
||||||
forge_name,
|
|
||||||
forge_config.clone(),
|
|
||||||
server_storage,
|
|
||||||
webhook,
|
|
||||||
net,
|
|
||||||
);
|
|
||||||
for (repo_alias, server_repo_config) in forge_config.repos() {
|
|
||||||
let forge_repo = creator((repo_alias, server_repo_config));
|
|
||||||
info!(
|
|
||||||
alias = %forge_repo.1,
|
|
||||||
"Created Repo"
|
|
||||||
);
|
|
||||||
repos.push(forge_repo);
|
|
||||||
}
|
|
||||||
repos
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_actor(
|
|
||||||
forge_name: ForgeName,
|
|
||||||
forge_config: config::ForgeConfig,
|
|
||||||
server_storage: &ServerStorage,
|
|
||||||
webhook: &Webhook,
|
|
||||||
net: &Network,
|
|
||||||
) -> impl Fn((RepoAlias, &ServerRepoConfig)) -> (ForgeName, RepoAlias, RepoActor) {
|
|
||||||
let server_storage = server_storage.clone();
|
|
||||||
let webhook = webhook.clone();
|
|
||||||
let net = net.clone();
|
|
||||||
move |(repo_alias, server_repo_config)| {
|
|
||||||
let span =
|
|
||||||
tracing::info_span!("create_actor", alias = %repo_alias, config = %server_repo_config);
|
|
||||||
let _guard = span.enter();
|
|
||||||
info!("Creating Repo");
|
|
||||||
let gitdir = server_repo_config.gitdir().map_or_else(
|
|
||||||
|| {
|
|
||||||
GitDir::from(
|
|
||||||
server_storage
|
|
||||||
.path()
|
|
||||||
.join(forge_name.to_string())
|
|
||||||
.join(repo_alias.to_string()),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
|gitdir| gitdir,
|
|
||||||
);
|
|
||||||
// INFO: can't canonicalise gitdir as the path needs to exist to do that and we may not
|
|
||||||
// have cloned the repo yet
|
|
||||||
let repo_details = config::RepoDetails::new(
|
|
||||||
&repo_alias,
|
|
||||||
server_repo_config,
|
|
||||||
&forge_name,
|
|
||||||
&forge_config,
|
|
||||||
gitdir,
|
|
||||||
);
|
|
||||||
info!("Starting Repo Actor");
|
|
||||||
let actor = actors::repo::RepoActor::new(repo_details, webhook.clone(), net.clone());
|
|
||||||
(forge_name.clone(), repo_alias, actor)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn start_actor(
|
|
||||||
actor: (ForgeName, RepoAlias, actors::repo::RepoActor),
|
|
||||||
) -> (RepoAlias, Addr<actors::repo::RepoActor>) {
|
|
||||||
let (forge_name, repo_alias, actor) = actor;
|
|
||||||
let span = tracing::info_span!("start_actor", forge = %forge_name, repo = %repo_alias);
|
|
||||||
let _guard = span.enter();
|
|
||||||
info!("Starting");
|
|
||||||
let addr = actor.start();
|
|
||||||
addr.do_send(actors::repo::CloneRepo);
|
|
||||||
info!("Started");
|
|
||||||
(repo_alias, addr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_logging() {
|
pub fn init_logging() {
|
||||||
|
|
Loading…
Reference in a new issue