move services ctor/dtor detail into service

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-06-05 05:41:29 +00:00
parent 427aa4645c
commit e4aa20ebeb
2 changed files with 35 additions and 27 deletions

View file

@ -6,14 +6,12 @@ use tracing::{debug, error, info};
extern crate conduit_admin as admin;
extern crate conduit_core as conduit;
extern crate conduit_database as database;
extern crate conduit_service as service;
use std::sync::atomic::Ordering;
use conduit::{debug_info, trace, Error, Result, Server};
use database::KeyValueDatabase;
use service::{services, Services};
use service::services;
use crate::{layers, serve};
@ -58,15 +56,10 @@ pub(crate) async fn run(server: Arc<Server>) -> Result<(), Error> {
/// Async initializations
#[tracing::instrument(skip_all)]
#[allow(clippy::let_underscore_must_use)]
pub(crate) async fn start(server: Arc<Server>) -> Result<(), Error> {
debug!("Starting...");
let d = Arc::new(KeyValueDatabase::load_or_create(&server).await?);
let s = Box::new(Services::build(server, d.clone()).await?);
_ = service::SERVICES
.write()
.expect("write locked")
.insert(Box::leak(s));
service::init(&server).await?;
services().start().await?;
#[cfg(feature = "systemd")]
@ -84,23 +77,10 @@ pub(crate) async fn stop(_server: Arc<Server>) -> Result<(), Error> {
// Wait for all completions before dropping or we'll lose them to the module
// unload and explode.
services().shutdown().await;
// Deactivate services(). Any further use will panic the caller.
let s = service::SERVICES
.write()
.expect("write locked")
.take()
.unwrap();
service::fini();
let s: *mut Services = std::ptr::from_ref(s).cast_mut();
//SAFETY: Services was instantiated in start() and leaked into the SERVICES
// global perusing as 'static for the duration of run_server(). Now we reclaim
// it to drop it before unloading the module. If this is not done there will be
// multiple instances after module reload.
let s = unsafe { Box::from_raw(s) };
debug!("Cleaning up...");
// Drop it so we encounter any trouble before the infolog message
drop(s);
#[cfg(feature = "systemd")]
sd_notify::notify(true, &[sd_notify::NotifyState::Stopping]).expect("failed to notify systemd of stopping state");

View file

@ -17,9 +17,9 @@ pub mod users;
extern crate conduit_core as conduit;
extern crate conduit_database as database;
use std::sync::RwLock;
use std::sync::{Arc, RwLock};
pub(crate) use conduit::{config, debug_error, debug_info, debug_warn, utils, Config, Error, PduCount, Result};
pub(crate) use conduit::{config, debug_error, debug_info, debug_warn, utils, Config, Error, PduCount, Result, Server};
pub(crate) use database::KeyValueDatabase;
pub use globals::{server_is_ours, user_is_local};
pub use pdu::PduEvent;
@ -30,7 +30,35 @@ pub(crate) use crate as service;
conduit::mod_ctor! {}
conduit::mod_dtor! {}
pub static SERVICES: RwLock<Option<&'static Services>> = RwLock::new(None);
static SERVICES: RwLock<Option<&Services>> = RwLock::new(None);
#[allow(clippy::let_underscore_must_use)]
pub async fn init(server: &Arc<Server>) -> Result<()> {
let d = Arc::new(KeyValueDatabase::load_or_create(server).await?);
let s = Box::new(Services::build(server.clone(), d.clone()).await?);
_ = SERVICES.write().expect("write locked").insert(Box::leak(s));
Ok(())
}
pub fn fini() {
// Deactivate services(). Any further use will panic the caller.
let s = SERVICES
.write()
.expect("write locked")
.take()
.expect("services initialized");
let s: *mut Services = std::ptr::from_ref(s).cast_mut();
//SAFETY: Services was instantiated in init() and leaked into the SERVICES
// global perusing as 'static for the duration of service. Now we reclaim
// it to drop it before unloading the module. If this is not done there wil
// be multiple instances after module reload.
let s = unsafe { Box::from_raw(s) };
// Drop it so we encounter any trouble before the infolog message
drop(s);
}
#[must_use]
pub fn services() -> &'static Services {