From df232cbc48c54a0ed3d2c3a8bbb391cffe5ff094 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 23 Feb 2023 07:30:32 -0700 Subject: [PATCH] refactor: rename shutdown/lifetime -> lifecycle --- crates/kumod/src/dest_site.rs | 2 +- .../kumod/src/{shutdown.rs => lifecycle.rs} | 19 +++++++++++-------- crates/kumod/src/main.rs | 10 +++++----- crates/kumod/src/mod_kumo.rs | 4 ++-- crates/kumod/src/queue.rs | 2 +- crates/kumod/src/smtp_server.rs | 2 +- crates/kumod/src/spool.rs | 2 +- 7 files changed, 22 insertions(+), 19 deletions(-) rename crates/kumod/src/{shutdown.rs => lifecycle.rs} (88%) diff --git a/crates/kumod/src/dest_site.rs b/crates/kumod/src/dest_site.rs index ad3020db..8744829f 100644 --- a/crates/kumod/src/dest_site.rs +++ b/crates/kumod/src/dest_site.rs @@ -1,6 +1,6 @@ +use crate::lifecycle::{Activity, ShutdownSubcription}; use crate::logging::{log_disposition, RecordType}; use crate::queue::{Queue, QueueManager}; -use crate::shutdown::{Activity, ShutdownSubcription}; use crate::spool::SpoolManager; use anyhow::Context; use config::load_config; diff --git a/crates/kumod/src/shutdown.rs b/crates/kumod/src/lifecycle.rs similarity index 88% rename from crates/kumod/src/shutdown.rs rename to crates/kumod/src/lifecycle.rs index d26c6cbc..a606cb03 100644 --- a/crates/kumod/src/shutdown.rs +++ b/crates/kumod/src/lifecycle.rs @@ -1,4 +1,4 @@ -//! This module helps to manage the lifetime of the process +//! This module helps to manage the life cycle of the process //! and to shut things down gracefully. //! //! See for more information. @@ -14,7 +14,7 @@ static STOPPING: OnceCell = OnceCell::new(); /// Represents some activity which cannot be ruthlessly interrupted. /// Obtain an Activity instance via Activity::get(). While any -/// Activity instances are alive in the program, Lifetime::wait_for_shutdown +/// Activity instances are alive in the program, LifeCycle::wait_for_shutdown /// cannot complete. #[derive(Clone)] pub struct Activity { @@ -35,10 +35,14 @@ impl Activity { Some(ACTIVE.get()?.lock().unwrap().as_ref()?.clone()) } + /// Obtain an Activity instance. + /// Returns Err if the process is shutting down and no new + /// activity can be initiated pub fn get() -> anyhow::Result { Self::get_opt().ok_or_else(|| anyhow::anyhow!("shutting down")) } + /// Returns true if the process is shutting down. pub fn is_shutting_down(&self) -> bool { SHUTTING_DOWN.load(Ordering::Relaxed) } @@ -72,17 +76,16 @@ impl ShutdownSubcription { } } -/// The Lifetime struct represents the lifetime of this server process. +/// The LifeCycle struct represents the life_cycle of this server process. /// Creating an instance of it will prepare the global state of the /// process and allow other code to work with Activity and ShutdownSubcription. -/// -pub struct Lifetime { +pub struct LifeCycle { activity_rx: MPSCReceiver<()>, request_shutdown_rx: MPSCReceiver<()>, } -impl Lifetime { - /// Initialize the process lifetime. +impl LifeCycle { + /// Initialize the process life_cycle. /// May be called only once; will panic if called multiple times. pub fn new() -> Self { let (activity_tx, activity_rx) = tokio::sync::mpsc::channel(1); @@ -111,7 +114,7 @@ impl Lifetime { /// Request that we shutdown the process. /// This will cause the wait_for_shutdown method on the process - /// Lifetime instance to wake up and initiate the shutdown + /// LifeCycle instance to wake up and initiate the shutdown /// procedure. pub async fn request_shutdown() { if let Some(state) = STOPPING.get() { diff --git a/crates/kumod/src/main.rs b/crates/kumod/src/main.rs index acf05cda..8fe9d7ab 100644 --- a/crates/kumod/src/main.rs +++ b/crates/kumod/src/main.rs @@ -1,4 +1,4 @@ -use crate::shutdown::Lifetime; +use crate::lifecycle::LifeCycle; use anyhow::Context; use caps::{CapSet, Capability, CapsHashSet}; use clap::Parser; @@ -10,12 +10,12 @@ use tracing_subscriber::{fmt, EnvFilter}; mod dest_site; mod http_server; +mod lifecycle; mod logging; mod metrics_helper; mod mod_kumo; mod queue; mod runtime; -mod shutdown; mod smtp_server; mod spool; @@ -114,17 +114,17 @@ async fn run(opts: Opt) -> anyhow::Result<()> { config::set_policy_path(policy).await?; } - let mut lifetime = Lifetime::new(); + let mut life_cycle = LifeCycle::new(); let mut config = config::load_config().await?; config.async_call_callback("init", ()).await?; if let Err(err) = crate::spool::SpoolManager::get().await.start_spool().await { tracing::error!("problem starting spool: {err:#}"); - Lifetime::request_shutdown().await; + LifeCycle::request_shutdown().await; } - lifetime.wait_for_shutdown().await; + life_cycle.wait_for_shutdown().await; // after waiting for those to idle out, shut down logging crate::logging::Logger::signal_shutdown().await; diff --git a/crates/kumod/src/mod_kumo.rs b/crates/kumod/src/mod_kumo.rs index e5fc21d8..f89d0519 100644 --- a/crates/kumod/src/mod_kumo.rs +++ b/crates/kumod/src/mod_kumo.rs @@ -1,8 +1,8 @@ use crate::dest_site::DestSiteConfig; use crate::http_server::HttpListenerParams; +use crate::lifecycle::LifeCycle; use crate::logging::LogFileParams; use crate::queue::QueueConfig; -use crate::shutdown::Lifetime; use crate::smtp_server::{EsmtpListenerParams, RejectError}; use config::get_or_create_module; use mlua::{Function, Lua, LuaSerdeExt, Value}; @@ -62,7 +62,7 @@ pub fn register(lua: &Lua) -> anyhow::Result<()> { tokio::spawn(async move { if let Err(err) = define_spool(params).await { tracing::error!("Error in spool: {err:#}"); - Lifetime::request_shutdown().await; + LifeCycle::request_shutdown().await; } }) .await diff --git a/crates/kumod/src/queue.rs b/crates/kumod/src/queue.rs index 3b17002d..03547903 100644 --- a/crates/kumod/src/queue.rs +++ b/crates/kumod/src/queue.rs @@ -1,6 +1,6 @@ use crate::dest_site::SiteManager; +use crate::lifecycle::{Activity, ShutdownSubcription}; use crate::logging::{log_disposition, RecordType}; -use crate::shutdown::{Activity, ShutdownSubcription}; use crate::spool::SpoolManager; use chrono::Utc; use config::load_config; diff --git a/crates/kumod/src/smtp_server.rs b/crates/kumod/src/smtp_server.rs index 6b8343bf..6eedc10d 100644 --- a/crates/kumod/src/smtp_server.rs +++ b/crates/kumod/src/smtp_server.rs @@ -1,7 +1,7 @@ use crate::dest_site::ResolvedAddress; +use crate::lifecycle::{Activity, ShutdownSubcription}; use crate::logging::{log_disposition, RecordType}; use crate::queue::QueueManager; -use crate::shutdown::{Activity, ShutdownSubcription}; use crate::spool::{SpoolHandle, SpoolManager}; use anyhow::{anyhow, Context}; use chrono::Utc; diff --git a/crates/kumod/src/spool.rs b/crates/kumod/src/spool.rs index 427a441c..8b71dd5b 100644 --- a/crates/kumod/src/spool.rs +++ b/crates/kumod/src/spool.rs @@ -1,7 +1,7 @@ +use crate::lifecycle::Activity; use crate::logging::{log_disposition, RecordType}; use crate::mod_kumo::{DefineSpoolParams, SpoolKind}; use crate::queue::QueueManager; -use crate::shutdown::Activity; use anyhow::Context; use chrono::Utc; use message::Message;