stop domain manager's inner tasks on shutdown

This commit is contained in:
Brian Picciano 2023-06-18 14:28:46 +02:00
parent 6941ceec8e
commit 6da68dc042
2 changed files with 20 additions and 7 deletions

View File

@ -139,7 +139,7 @@ pub trait Manager: Sync + Send {
fn all_domains(&self) -> Result<Vec<domain::Name>, unexpected::Error>; fn all_domains(&self) -> Result<Vec<domain::Name>, unexpected::Error>;
} }
struct ManagerImpl { pub struct ManagerImpl {
origin_store: sync::Arc<dyn origin::store::Store>, origin_store: sync::Arc<dyn origin::store::Store>,
domain_config_store: sync::Arc<dyn config::Store>, domain_config_store: sync::Arc<dyn config::Store>,
domain_checker: checker::DNSChecker, domain_checker: checker::DNSChecker,
@ -170,7 +170,7 @@ pub fn new(
domain_config_store: sync::Arc<dyn config::Store>, domain_config_store: sync::Arc<dyn config::Store>,
domain_checker: checker::DNSChecker, domain_checker: checker::DNSChecker,
acme_manager: Option<sync::Arc<dyn acme::manager::Manager>>, acme_manager: Option<sync::Arc<dyn acme::manager::Manager>>,
) -> sync::Arc<dyn Manager> { ) -> ManagerImpl {
let canceller = CancellationToken::new(); let canceller = CancellationToken::new();
let origin_sync_handler = { let origin_sync_handler = {
@ -188,22 +188,20 @@ pub fn new(
}) })
}; };
sync::Arc::new(ManagerImpl { ManagerImpl {
origin_store, origin_store,
domain_config_store, domain_config_store,
domain_checker, domain_checker,
acme_manager, acme_manager,
canceller, canceller,
origin_sync_handler, origin_sync_handler,
}) }
} }
impl ManagerImpl { impl ManagerImpl {
pub async fn stop(self) { pub fn stop(self) -> tokio::task::JoinHandle<()> {
self.canceller.cancel(); self.canceller.cancel();
self.origin_sync_handler self.origin_sync_handler
.await
.expect("origin_sync_handler errored");
} }
} }

View File

@ -12,6 +12,8 @@ use std::net::SocketAddr;
use std::str::FromStr; use std::str::FromStr;
use std::{future, path, sync}; use std::{future, path, sync};
use domiply::domain::manager::Manager;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version)] #[command(version)]
#[command(about = "A domiply to another dimension")] #[command(about = "A domiply to another dimension")]
@ -152,6 +154,8 @@ async fn main() {
https_params.as_ref().map(|p| p.domain_acme_manager.clone()), https_params.as_ref().map(|p| p.domain_acme_manager.clone()),
); );
let domain_manager = sync::Arc::new(domain_manager);
let service = domiply::service::new( let service = domiply::service::new(
domain_manager.clone(), domain_manager.clone(),
config.domain_checker_target_a, config.domain_checker_target_a,
@ -317,5 +321,16 @@ async fn main() {
while wait_group.next().await.is_some() {} while wait_group.next().await.is_some() {}
// TODO this is currently required so that we can be sure domain_manager is no longer used by
// anything else, and the into_inner below works. It would be great if service could accept a
// ref to domain_manager instead, and then maybe this wouldn't be needed?
drop(service);
sync::Arc::into_inner(domain_manager)
.unwrap()
.stop()
.await
.expect("domain manager failed to shutdown cleanly");
log::info!("Graceful shutdown complete"); log::info!("Graceful shutdown complete");
} }