Skip to content
Snippets Groups Projects
Commit 5c258f41 authored by Jason Volk's avatar Jason Volk Committed by Jason Volk
Browse files

fixes for modules


Signed-off-by: default avatarJason Volk <jason@zemos.net>
parent 15126ee1
No related branches found
No related tags found
No related merge requests found
use std::ffi::OsStr; use std::ffi::OsStr;
use super::{path, Library}; use super::{path, Library};
use crate::{Error, Result}; use crate::{Err, Result};
const OPEN_FLAGS: i32 = libloading::os::unix::RTLD_LAZY | libloading::os::unix::RTLD_GLOBAL; const OPEN_FLAGS: i32 = libloading::os::unix::RTLD_LAZY | libloading::os::unix::RTLD_GLOBAL;
...@@ -16,7 +16,7 @@ pub fn from_path(path: &OsStr) -> Result<Library> { ...@@ -16,7 +16,7 @@ pub fn from_path(path: &OsStr) -> Result<Library> {
let lib = unsafe { Library::open(Some(path), OPEN_FLAGS) }; let lib = unsafe { Library::open(Some(path), OPEN_FLAGS) };
if let Err(e) = lib { if let Err(e) = lib {
let name = path::to_name(path)?; let name = path::to_name(path)?;
return Err(Error::Err(format!("Loading module {name:?} failed: {e}"))); return Err!("Loading module {name:?} failed: {e}");
} }
Ok(lib.expect("module loaded")) Ok(lib.expect("module loaded"))
......
#![cfg(conduit_mods)] #![cfg(conduit_mods)]
#[no_link]
extern crate conduit_service;
use std::{ use std::{
future::Future, future::Future,
pin::Pin, pin::Pin,
sync::{atomic::Ordering, Arc}, sync::{atomic::Ordering, Arc},
}; };
use conduit::{mods, Error, Result}; use conduit::{debug, error, mods, Error, Result};
use tracing::{debug, error}; use conduit_service::Services;
use crate::Server; use crate::Server;
type RunFuncResult = Pin<Box<dyn Future<Output = Result<(), Error>>>>; type StartFuncResult = Pin<Box<dyn Future<Output = Result<Arc<Services>>> + Send>>;
type RunFuncProto = fn(&Arc<conduit::Server>) -> RunFuncResult; type StartFuncProto = fn(&Arc<conduit::Server>) -> StartFuncResult;
type RunFuncResult = Pin<Box<dyn Future<Output = Result<()>> + Send>>;
type RunFuncProto = fn(&Arc<Services>) -> RunFuncResult;
type StopFuncResult = Pin<Box<dyn Future<Output = Result<()>> + Send>>;
type StopFuncProto = fn(Arc<Services>) -> StopFuncResult;
const RESTART_THRESH: &str = "conduit_service"; const RESTART_THRESH: &str = "conduit_service";
const MODULE_NAMES: &[&str] = &[ const MODULE_NAMES: &[&str] = &[
...@@ -33,15 +42,25 @@ pub(crate) async fn run(server: &Arc<Server>, starts: bool) -> Result<(bool, boo ...@@ -33,15 +42,25 @@ pub(crate) async fn run(server: &Arc<Server>, starts: bool) -> Result<(bool, boo
let main_lock = server.mods.read().await; let main_lock = server.mods.read().await;
let main_mod = (*main_lock).last().expect("main module loaded"); let main_mod = (*main_lock).last().expect("main module loaded");
if starts { if starts {
let start = main_mod.get::<RunFuncProto>("start")?; let start = main_mod.get::<StartFuncProto>("start")?;
if let Err(error) = start(&server.server).await { match start(&server.server).await {
error!("Starting server: {error}"); Ok(services) => server.services.lock().await.insert(services),
return Err(error); Err(error) => {
} error!("Starting server: {error}");
return Err(error);
},
};
} }
server.server.stopping.store(false, Ordering::Release); server.server.stopping.store(false, Ordering::Release);
let run = main_mod.get::<RunFuncProto>("run")?; let run = main_mod.get::<RunFuncProto>("run")?;
if let Err(error) = run(&server.server).await { if let Err(error) = run(server
.services
.lock()
.await
.as_ref()
.expect("services initialized"))
.await
{
error!("Running server: {error}"); error!("Running server: {error}");
return Err(error); return Err(error);
} }
...@@ -49,8 +68,17 @@ pub(crate) async fn run(server: &Arc<Server>, starts: bool) -> Result<(bool, boo ...@@ -49,8 +68,17 @@ pub(crate) async fn run(server: &Arc<Server>, starts: bool) -> Result<(bool, boo
let stops = !reloads || stale(server).await? <= restart_thresh(); let stops = !reloads || stale(server).await? <= restart_thresh();
let starts = reloads && stops; let starts = reloads && stops;
if stops { if stops {
let stop = main_mod.get::<RunFuncProto>("stop")?; let stop = main_mod.get::<StopFuncProto>("stop")?;
if let Err(error) = stop(&server.server).await { if let Err(error) = stop(
server
.services
.lock()
.await
.take()
.expect("services initialized"),
)
.await
{
error!("Stopping server: {error}"); error!("Stopping server: {error}");
return Err(error); return Err(error);
} }
...@@ -103,7 +131,7 @@ pub(crate) async fn close(server: &Arc<Server>, force: bool) -> Result<usize, Er ...@@ -103,7 +131,7 @@ pub(crate) async fn close(server: &Arc<Server>, force: bool) -> Result<usize, Er
async fn stale_count(server: &Arc<Server>) -> usize { async fn stale_count(server: &Arc<Server>) -> usize {
let watermark = stale(server).await.unwrap_or(available()); let watermark = stale(server).await.unwrap_or(available());
available() - watermark available().saturating_sub(watermark)
} }
async fn stale(server: &Arc<Server>) -> Result<usize, Error> { async fn stale(server: &Arc<Server>) -> Result<usize, Error> {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment