use crate::application::error::EngineError;
#[cfg(feature = "jobs")]
use crate::application::error::ShutdownError;
use crate::application::startup::Started;
pub(crate) async fn shutdown(started: Started) -> Result<(), EngineError> {
let Started {
resources,
#[cfg(feature = "jobs")]
worker,
} = started;
#[cfg(feature = "jobs")]
let mut first_error: Option<EngineError> = None;
#[cfg(feature = "mail")]
{
if let Some(mailer) = resources.mail {
mailer.shutdown().await;
}
}
#[cfg(feature = "storage")]
{
drop(resources.storage);
}
#[cfg(feature = "cache")]
{
if let Some(cache) = resources.cache {
cache.close().await;
}
}
#[cfg(feature = "jobs")]
{
if let Some(worker_handle) = worker {
worker_handle.shutdown.cancel();
let result = worker_handle.join.await;
match result {
Ok(Ok(())) => { }
Ok(Err(source)) => {
if first_error.is_none() {
first_error = Some(EngineError::Shutdown {
subsystem: "jobs",
source: ShutdownError::Worker(source),
});
}
}
Err(join_err) => {
if first_error.is_none() {
first_error = Some(EngineError::Shutdown {
subsystem: "jobs",
source: ShutdownError::Worker(
arcature_jobs::WorkerError::Database {
source: arcature_db::sqlx::Error::WorkerCrashed,
},
),
});
let _ = join_err; }
}
}
}
}
#[cfg(feature = "db")]
{
if let Some(db) = resources.db {
db.close().await;
}
}
#[cfg(not(any(
feature = "db",
feature = "cache",
feature = "storage",
feature = "mail",
feature = "jobs"
)))]
{
let _ = resources;
}
#[cfg(feature = "jobs")]
{
if let Some(err) = first_error {
return Err(err);
}
}
Ok(())
}