diff --git a/Cargo.lock b/Cargo.lock index 3c9941a11..0051b7514 100644 Binary files a/Cargo.lock and b/Cargo.lock differ diff --git a/apps/desktop/src-tauri/src/main.rs b/apps/desktop/src-tauri/src/main.rs index 9fb97486e..bc33381af 100644 --- a/apps/desktop/src-tauri/src/main.rs +++ b/apps/desktop/src-tauri/src/main.rs @@ -6,7 +6,7 @@ use std::path::PathBuf; use sdcore::Node; -use tauri::{api::path, Manager, RunEvent}; +use tauri::{api::path, async_runtime::block_on, Manager, RunEvent}; use tracing::{debug, error}; #[cfg(target_os = "macos")] mod macos; @@ -79,7 +79,7 @@ async fn main() { } }); - node.shutdown(); + block_on(node.shutdown()); app_handler.exit(0); } }) diff --git a/apps/server/src/main.rs b/apps/server/src/main.rs index 3711f815b..95e1ccf0c 100644 --- a/apps/server/src/main.rs +++ b/apps/server/src/main.rs @@ -29,14 +29,7 @@ async fn main() { .unwrap_or(8080); let (node, router) = Node::new(data_dir).await; - - ctrlc::set_handler({ - let node = node.clone(); - move || { - node.shutdown(); - } - }) - .expect("Error setting Ctrl-C handler"); + let signal = utils::axum_shutdown_signal(node.clone()); let app = axum::Router::new() .route("/", get(|| async { "Spacedrive Server!" })) @@ -55,7 +48,7 @@ async fn main() { info!("Listening on http://localhost:{}", port); axum::Server::bind(&addr) .serve(app.into_make_service()) - .with_graceful_shutdown(utils::axum_shutdown_signal()) + .with_graceful_shutdown(signal) .await .expect("Error with HTTP server!"); } diff --git a/apps/server/src/utils.rs b/apps/server/src/utils.rs index f6437d365..a6bdc360a 100644 --- a/apps/server/src/utils.rs +++ b/apps/server/src/utils.rs @@ -1,7 +1,10 @@ +use std::sync::Arc; + +use sdcore::Node; use tokio::signal; /// shutdown_signal will inform axum to gracefully shutdown when the process is asked to shutdown. -pub async fn axum_shutdown_signal() { +pub async fn axum_shutdown_signal(node: Arc) { let ctrl_c = async { signal::ctrl_c() .await @@ -25,4 +28,5 @@ pub async fn axum_shutdown_signal() { } println!("signal received, starting graceful shutdown"); + node.shutdown().await; } diff --git a/core/src/lib.rs b/core/src/lib.rs index 776a5e5f5..d7086cd75 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -1,5 +1,4 @@ use api::{CoreEvent, Ctx, Router}; -use futures::executor::block_on; use job::JobManager; use library::LibraryManager; use node::NodeConfigManager; @@ -116,9 +115,9 @@ impl Node { } } - pub fn shutdown(&self) { + pub async fn shutdown(&self) { info!("Spacedrive shutting down..."); - block_on(self.jobs.pause()); - info!("Shutdown complete."); + self.jobs.pause().await; + info!("Spacedrive Core shutdown successful!"); } } diff --git a/core/src/util/db.rs b/core/src/util/db.rs index 66163895d..9e651e38d 100644 --- a/core/src/util/db.rs +++ b/core/src/util/db.rs @@ -20,7 +20,7 @@ static MIGRATIONS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/prisma/migrations #[derive(Error, Debug)] pub enum MigrationError { #[error("An error occurred while initialising a new database connection: {0}")] - NewClient(#[from] NewClientError), + NewClient(#[from] Box), #[error("The temporary file path for the database migrations is invalid.")] InvalidDirectory, #[error("An error occurred creating the temporary directory for the migrations: {0}")] @@ -40,7 +40,9 @@ pub async fn load_and_migrate( base_path: &Path, db_url: &str, ) -> Result { - let client = prisma::new_client_with_url(db_url).await?; + let client = prisma::new_client_with_url(db_url) + .await + .map_err(Box::new)?; let temp_migrations_dir = base_path.join("./migrations_temp"); let migrations_directory_path = temp_migrations_dir .to_str() @@ -60,7 +62,7 @@ pub async fn load_and_migrate( .extract(&temp_migrations_dir) .map_err(MigrationError::ExtractMigrations)?; - let mut connector = match &ConnectionInfo::from_url(&db_url)? { + let mut connector = match &ConnectionInfo::from_url(db_url)? { ConnectionInfo::Sqlite { .. } => SqlMigrationConnector::new_sqlite(), ConnectionInfo::InMemorySqlite { .. } => unreachable!(), // This is how it is in the Prisma Rust tests };