daemon: Start bringing up contexts and state loading/saving

This commit is contained in:
Vicki Pfau 2024-05-24 17:06:43 -07:00 committed by Jeremy Whiting
parent 23267c65e0
commit c6113ee739
6 changed files with 275 additions and 19 deletions

View file

@ -6,6 +6,10 @@
*/
use anyhow::{anyhow, ensure, Result};
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use std::marker::PhantomData;
use std::path::PathBuf;
use tokio::signal::unix::{signal, SignalKind};
use tokio::task::JoinSet;
use tokio_util::sync::CancellationToken;
@ -14,7 +18,7 @@ use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::registry::LookupSpan;
use zbus::connection::Connection;
use crate::daemon::config::read_config;
use crate::daemon::config::{read_config, read_state};
use crate::sls::{LogLayer, LogReceiver};
use crate::Service;
@ -25,16 +29,40 @@ mod user;
pub use root::daemon as root;
pub use user::daemon as user;
pub(crate) struct Daemon {
services: JoinSet<Result<()>>,
token: CancellationToken,
pub(crate) trait DaemonContext: Sized {
type State: for<'a> Deserialize<'a> + Serialize + Default + Debug;
type Config: for<'a> Deserialize<'a> + Default + Debug;
fn state_path(&self) -> Result<PathBuf> {
let config_path = self.user_config_path()?;
Ok(config_path.join("state.toml"))
}
fn user_config_path(&self) -> Result<PathBuf>;
fn system_config_path(&self) -> Result<PathBuf>;
fn state(&self) -> Self::State;
async fn start(
&mut self,
state: Self::State,
config: Self::Config,
daemon: &mut Daemon<Self>,
) -> Result<()>;
async fn reload(&mut self, config: Self::Config, daemon: &mut Daemon<Self>) -> Result<()>;
}
impl Daemon {
pub(crate) struct Daemon<C: DaemonContext> {
services: JoinSet<Result<()>>,
token: CancellationToken,
_context: PhantomData<C>,
}
impl<C: DaemonContext> Daemon<C> {
pub(crate) async fn new<S: SubscriberExt + Send + Sync + for<'a> LookupSpan<'a>>(
subscriber: S,
connection: Connection,
) -> Result<Daemon> {
) -> Result<Daemon<C>> {
let services = JoinSet::new();
let token = CancellationToken::new();
@ -43,7 +71,11 @@ impl Daemon {
let subscriber = subscriber.with(remote_logger);
tracing::subscriber::set_global_default(subscriber)?;
let mut daemon = Daemon { services, token };
let mut daemon = Daemon {
services,
token,
_context: PhantomData::default(),
};
daemon.add_service(log_receiver);
Ok(daemon)
@ -57,12 +89,16 @@ impl Daemon {
token
}
pub(crate) async fn run(&mut self) -> Result<()> {
pub(crate) async fn run(&mut self, mut context: C) -> Result<()> {
ensure!(
!self.services.is_empty(),
"Can't run a daemon with no services attached."
);
let state = read_state(&context).await?;
let config = read_config(&context).await?;
context.start(state, config, self).await?;
let mut res = loop {
let mut sigterm = signal(SignalKind::terminate())?;
let mut sigquit = signal(SignalKind::quit())?;
@ -81,10 +117,14 @@ impl Daemon {
},
e = sighup.recv() => match e {
Some(_) => {
if let Err(error) = read_config().await {
error!("Failed to reload configuration: {error}");
match read_config(&context).await {
Ok(config) =>
context.reload(config, self).await,
Err(error) => {
error!("Failed to load configuration: {error}");
Ok(())
}
}
Ok(())
}
None => Err(anyhow!("SIGHUP machine broke")),
},