feat(server): give Server an incremental generation counter

This commit is contained in:
Paul Campbell 2024-05-07 08:17:29 +01:00
parent 9b211179bf
commit b1638a65fc
8 changed files with 84 additions and 28 deletions

View file

@ -11,14 +11,14 @@ use crate::server::{
actors::repo::webhook::WebhookAuth, actors::repo::webhook::WebhookAuth,
config::{RepoConfig, RepoDetails, Webhook}, config::{RepoConfig, RepoDetails, Webhook},
gitforge, gitforge,
types::MessageToken, types::{MessageToken, ServerGeneration},
}; };
use self::webhook::WebhookId; use self::webhook::WebhookId;
pub struct RepoActor { pub struct RepoActor {
generation: ServerGeneration,
message_token: MessageToken, message_token: MessageToken,
span: tracing::Span,
details: RepoDetails, details: RepoDetails,
webhook: Webhook, webhook: Webhook,
webhook_id: Option<WebhookId>, // INFO: if [None] then no webhook is configured webhook_id: Option<WebhookId>, // INFO: if [None] then no webhook is configured
@ -30,8 +30,12 @@ pub struct RepoActor {
forge: gitforge::Forge, forge: gitforge::Forge,
} }
impl RepoActor { impl RepoActor {
pub(crate) fn new(details: RepoDetails, webhook: Webhook, net: Network) -> Self { pub(crate) fn new(
let span = tracing::info_span!("RepoActor", repo = %details); details: RepoDetails,
webhook: Webhook,
generation: ServerGeneration,
net: Network,
) -> Self {
let forge = match details.forge.forge_type { let forge = match details.forge.forge_type {
#[cfg(feature = "forgejo")] #[cfg(feature = "forgejo")]
crate::server::config::ForgeType::ForgeJo => { crate::server::config::ForgeType::ForgeJo => {
@ -42,8 +46,8 @@ impl RepoActor {
}; };
debug!(?forge, "new"); debug!(?forge, "new");
Self { Self {
generation,
message_token: MessageToken::new(), message_token: MessageToken::new(),
span,
details, details,
webhook, webhook,
webhook_id: None, webhook_id: None,
@ -58,13 +62,14 @@ impl RepoActor {
} }
impl Actor for RepoActor { impl Actor for RepoActor {
type Context = Context<Self>; type Context = Context<Self>;
#[tracing::instrument(name = "RepoActor::stopping", skip_all)]
fn stopping(&mut self, ctx: &mut Self::Context) -> Running { fn stopping(&mut self, ctx: &mut Self::Context) -> Running {
let _gaurd = self.span.enter();
info!("Checking webhook"); info!("Checking webhook");
match self.webhook_id.take() { match self.webhook_id.take() {
Some(webhook_id) => { Some(webhook_id) => {
let repo_details = self.details.clone(); let repo_details = self.details.clone();
let net = self.net.clone(); let net = self.net.clone();
info!(%webhook_id, "Unregistring webhook");
webhook::unregister(webhook_id, repo_details, net) webhook::unregister(webhook_id, repo_details, net)
.in_current_span() .in_current_span()
.into_actor(self) .into_actor(self)
@ -79,8 +84,8 @@ impl std::fmt::Display for RepoActor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!( write!(
f, f,
"{}/{}", "{}:{}:{}",
self.details.forge.forge_name, self.details.repo_alias self.generation, self.details.forge.forge_name, self.details.repo_alias
) )
} }
} }

View file

@ -14,6 +14,7 @@ use crate::server::{
ForgeConfig, ForgeName, GitDir, RepoAlias, RepoDetails, ServerConfig, ServerRepoConfig, ForgeConfig, ForgeName, GitDir, RepoAlias, RepoDetails, ServerConfig, ServerRepoConfig,
ServerStorage, Webhook, ServerStorage, Webhook,
}, },
types::ServerGeneration,
}; };
#[derive(Debug, derive_more::Display, derive_more::From)] #[derive(Debug, derive_more::Display, derive_more::From)]
@ -33,6 +34,7 @@ pub enum Error {
type Result<T> = core::result::Result<T, Error>; type Result<T> = core::result::Result<T, Error>;
pub struct Server { pub struct Server {
generation: ServerGeneration,
fs: FileSystem, fs: FileSystem,
net: Network, net: Network,
} }
@ -85,8 +87,12 @@ impl Handler<ServerConfig> for Server {
} }
} }
impl Server { impl Server {
pub const fn new(fs: FileSystem, net: Network) -> Self { pub const fn new(generation: ServerGeneration, fs: FileSystem, net: Network) -> Self {
Self { fs, net } Self {
generation,
fs,
net,
}
} }
fn create_forge_data_directories( fn create_forge_data_directories(
&self, &self,
@ -144,6 +150,7 @@ impl Server {
let server_storage = server_storage.clone(); let server_storage = server_storage.clone();
let webhook = webhook.clone(); let webhook = webhook.clone();
let net = self.net.clone(); let net = self.net.clone();
let generation = self.generation;
move |(repo_alias, server_repo_config)| { move |(repo_alias, server_repo_config)| {
let span = tracing::info_span!("create_actor", alias = %repo_alias, config = %server_repo_config); let span = tracing::info_span!("create_actor", alias = %repo_alias, config = %server_repo_config);
let _guard = span.enter(); let _guard = span.enter();
@ -162,6 +169,7 @@ impl Server {
// INFO: can't canonicalise gitdir as the path needs to exist to do that and we may not // INFO: can't canonicalise gitdir as the path needs to exist to do that and we may not
// have cloned the repo yet // have cloned the repo yet
let repo_details = RepoDetails::new( let repo_details = RepoDetails::new(
generation,
&repo_alias, &repo_alias,
server_repo_config, server_repo_config,
&forge_name, &forge_name,
@ -169,7 +177,7 @@ impl Server {
gitdir, gitdir,
); );
info!("Starting Repo Actor"); info!("Starting Repo Actor");
let actor = RepoActor::new(repo_details, webhook.clone(), net.clone()); let actor = RepoActor::new(repo_details, webhook.clone(), generation, net.clone());
(forge_name.clone(), repo_alias, actor) (forge_name.clone(), repo_alias, actor)
} }
} }

View file

@ -14,6 +14,8 @@ use serde::Deserialize;
use kxio::fs::FileSystem; use kxio::fs::FileSystem;
use tracing::info; use tracing::info;
use crate::server::types::ServerGeneration;
#[derive(Debug, derive_more::From, derive_more::Display)] #[derive(Debug, derive_more::From, derive_more::Display)]
pub enum Error { pub enum Error {
Io(std::io::Error), Io(std::io::Error),
@ -359,6 +361,7 @@ impl Deref for BranchName {
/// The derived information about a repo, used to interact with it /// The derived information about a repo, used to interact with it
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct RepoDetails { pub struct RepoDetails {
pub generation: ServerGeneration,
pub repo_alias: RepoAlias, pub repo_alias: RepoAlias,
pub repo_path: RepoPath, pub repo_path: RepoPath,
pub branch: BranchName, pub branch: BranchName,
@ -368,6 +371,7 @@ pub struct RepoDetails {
} }
impl RepoDetails { impl RepoDetails {
pub fn new( pub fn new(
generation: ServerGeneration,
repo_alias: &RepoAlias, repo_alias: &RepoAlias,
server_repo_config: &ServerRepoConfig, server_repo_config: &ServerRepoConfig,
forge_name: &ForgeName, forge_name: &ForgeName,
@ -375,6 +379,7 @@ impl RepoDetails {
gitdir: GitDir, gitdir: GitDir,
) -> Self { ) -> Self {
Self { Self {
generation,
repo_alias: repo_alias.clone(), repo_alias: repo_alias.clone(),
repo_path: RepoPath(server_repo_config.repo.clone()), repo_path: RepoPath(server_repo_config.repo.clone()),
repo_config: server_repo_config.repo_config(), repo_config: server_repo_config.repo_config(),
@ -414,7 +419,8 @@ impl Display for RepoDetails {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!( write!(
f, f,
"{}:{}/{}:{}@{}/{}@{}", "gen-{}:{}:{}/{}:{}@{}/{}@{}",
self.generation,
self.forge.forge_type, self.forge.forge_type,
self.forge.forge_name, self.forge.forge_name,
self.repo_alias, self.repo_alias,

View file

@ -157,6 +157,7 @@ fn repo_details_find_default_push_remote_finds_correct_remote() -> Result<()> {
let cwd = std::env::current_dir().map_err(RepoValidationError::Io)?; let cwd = std::env::current_dir().map_err(RepoValidationError::Io)?;
let mut repo_details = common::repo_details( let mut repo_details = common::repo_details(
1, 1,
ServerGeneration::new(),
common::forge_details(1, ForgeType::MockForge), common::forge_details(1, ForgeType::MockForge),
None, None,
GitDir::new(&cwd), // Server GitDir - should be ignored GitDir::new(&cwd), // Server GitDir - should be ignored
@ -180,6 +181,7 @@ fn gitdir_validate_should_pass_a_valid_git_repo() -> Result<()> {
let cwd = std::env::current_dir().map_err(RepoValidationError::Io)?; let cwd = std::env::current_dir().map_err(RepoValidationError::Io)?;
let mut repo_details = common::repo_details( let mut repo_details = common::repo_details(
1, 1,
ServerGeneration::new(),
common::forge_details(1, ForgeType::MockForge), common::forge_details(1, ForgeType::MockForge),
None, None,
GitDir::new(&cwd), // Server GitDir - should be ignored GitDir::new(&cwd), // Server GitDir - should be ignored
@ -198,6 +200,7 @@ fn gitdir_validate_should_fail_a_non_git_dir() {
let cwd = fs.base(); let cwd = fs.base();
let repo_details = common::repo_details( let repo_details = common::repo_details(
1, 1,
ServerGeneration::new(),
common::forge_details(1, ForgeType::MockForge), common::forge_details(1, ForgeType::MockForge),
None, None,
GitDir::new(cwd), // Server GitDir - should be ignored GitDir::new(cwd), // Server GitDir - should be ignored
@ -211,6 +214,7 @@ fn gitdir_validate_should_fail_a_git_repo_with_wrong_remote() {
let_assert!(Ok(cwd) = std::env::current_dir().map_err(RepoValidationError::Io)); let_assert!(Ok(cwd) = std::env::current_dir().map_err(RepoValidationError::Io));
let mut repo_details = common::repo_details( let mut repo_details = common::repo_details(
1, 1,
ServerGeneration::new(),
common::forge_details(1, ForgeType::MockForge), common::forge_details(1, ForgeType::MockForge),
None, None,
GitDir::new(&cwd), // Server GitDir - should be ignored GitDir::new(&cwd), // Server GitDir - should be ignored

View file

@ -1,6 +1,9 @@
use crate::server::config::{ use crate::server::{
ApiToken, BranchName, ForgeDetails, ForgeName, ForgeType, GitDir, Hostname, RepoAlias, config::{
RepoBranches, RepoConfig, RepoDetails, RepoPath, User, ApiToken, BranchName, ForgeDetails, ForgeName, ForgeType, GitDir, Hostname, RepoAlias,
RepoBranches, RepoConfig, RepoDetails, RepoPath, User,
},
types::ServerGeneration,
}; };
pub fn forge_details(n: u32, forge_type: ForgeType) -> ForgeDetails { pub fn forge_details(n: u32, forge_type: ForgeType) -> ForgeDetails {
@ -30,11 +33,13 @@ pub fn forge_name(n: u32) -> ForgeName {
} }
pub fn repo_details( pub fn repo_details(
n: u32, n: u32,
generation: ServerGeneration,
forge: ForgeDetails, forge: ForgeDetails,
repo_config: Option<RepoConfig>, repo_config: Option<RepoConfig>,
gitdir: GitDir, gitdir: GitDir,
) -> RepoDetails { ) -> RepoDetails {
RepoDetails { RepoDetails {
generation,
repo_alias: repo_alias(n), repo_alias: repo_alias(n),
repo_path: repo_path(n), repo_path: repo_path(n),
gitdir, gitdir,

View file

@ -2,7 +2,10 @@ use assert2::let_assert;
use kxio::network::{MockNetwork, StatusCode}; use kxio::network::{MockNetwork, StatusCode};
use crate::server::config::{BranchName, ForgeType}; use crate::server::{
config::{BranchName, ForgeType},
types::ServerGeneration,
};
use super::*; use super::*;
@ -14,6 +17,7 @@ fn test_name() {
let net = Network::new_mock(); let net = Network::new_mock();
let repo_details = common::repo_details( let repo_details = common::repo_details(
1, 1,
ServerGeneration::new(),
common::forge_details(1, ForgeType::MockForge), common::forge_details(1, ForgeType::MockForge),
Some(common::repo_config(1)), Some(common::repo_config(1)),
GitDir::new(fs.base()), GitDir::new(fs.base()),
@ -40,6 +44,7 @@ async fn test_branches_get() {
let repo_details = common::repo_details( let repo_details = common::repo_details(
1, 1,
ServerGeneration::new(),
common::forge_details(1, ForgeType::MockForge), common::forge_details(1, ForgeType::MockForge),
Some(common::repo_config(1)), Some(common::repo_config(1)),
GitDir::new(fs.base()), GitDir::new(fs.base()),

View file

@ -11,7 +11,10 @@ use std::path::PathBuf;
use tracing::{error, info, level_filters::LevelFilter}; use tracing::{error, info, level_filters::LevelFilter};
use crate::{fs::FileSystem, server::actors::server::Server}; use crate::{
fs::FileSystem,
server::{actors::server::Server, types::ServerGeneration},
};
pub fn init(fs: FileSystem) { pub fn init(fs: FileSystem) {
let file_name = "git-next-server.toml"; let file_name = "git-next-server.toml";
@ -37,22 +40,26 @@ pub fn init(fs: FileSystem) {
pub async fn start(fs: FileSystem, net: Network) { pub async fn start(fs: FileSystem, net: Network) {
init_logging(); init_logging();
info!("Starting Server..."); let generation = ServerGeneration::new();
let server_config = match config::ServerConfig::load(&fs) { {
Ok(server_config) => server_config, let span = tracing::info_span!("Server", %generation);
Err(err) => { let _guard = span.enter();
error!("Failed to load config file. Error: {}", err); info!("Starting Server...");
return; let server_config = match config::ServerConfig::load(&fs) {
} Ok(server_config) => server_config,
}; Err(err) => {
error!("Failed to load config file. Error: {}", err);
return;
}
};
let server = Server::new(fs, net).start(); let server = Server::new(generation, fs.clone(), net.clone()).start();
server.do_send(server_config); server.do_send(server_config);
}
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(server);
} }
pub fn init_logging() { pub fn init_logging() {

View file

@ -35,3 +35,19 @@ impl std::fmt::Display for MessageToken {
write!(f, "{}", self.0) write!(f, "{}", self.0)
} }
} }
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct ServerGeneration(u32);
impl ServerGeneration {
pub fn new() -> Self {
Self::default()
}
pub const fn next(&self) -> Self {
Self(self.0 + 1)
}
}
impl std::fmt::Display for ServerGeneration {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}