From 3338506119f72f58cfec5e0de807121f8596e073 Mon Sep 17 00:00:00 2001 From: Oscar Beaumont Date: Wed, 14 Sep 2022 19:02:11 +0800 Subject: [PATCH] properly shutdown the core when using server --- Cargo.lock | Bin 187256 -> 187256 bytes apps/desktop/src-tauri/src/main.rs | 4 ++-- apps/server/src/main.rs | 11 ++--------- apps/server/src/utils.rs | 6 +++++- core/src/lib.rs | 7 +++---- core/src/util/db.rs | 8 +++++--- 6 files changed, 17 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ebce41f967c7998bfe510dafeb05ac4c5d84e583..9c0872bb03873db7b800a1988f1b9e5103ce0769 100644 GIT binary patch delta 1307 zcmb7^J4*vW6ot7LVqz8&6;V4EjE^FsN!aLuh!7Pdr52XiT@wTq5j0kkU}+K56)xEL z#v&k*g-j=wJ`jUHK(Vqju~K~Dt|GSHW{QFDoSC`j%u7*vDN2R8M0-e6!x6(Q`k=+TI?qxI^}5)P@ohBje7EjL(4iBT5^pxFf&qU<6_RxWj3#X-%h>7aEw^Wfn? zR0yDCmDA%{7Il$BpSAw%?PO7o7x^|J_I*J!m-5j-E4;BZDoL!N_hXREa@sF;+hE7b z;YBtCH^?DK&O1L@oxfFITmx3(pT1784??Hx2hxRiT4xmol?t_b!Y+;XJ zKF0F>t&vyAWq1s;*e)3jXVw9_lX2{4&iyU8((%vYYZR)tqu~kQ6vtQC^-$d$g6uNX p(rY``i+B+GH5U8n!XEOkqE4l2m=>2g%mYin{sTTlVGYklz5r7KpH%<= delta 446 zcmeydjr+$o?hSFmP}mJ zCWpI1EP#Zry>*!E5+Q>BSeeYH^G{$(nttItW6t)|eN3ytVPXpk?!ViC4m;rq E0CfYP*8l(j 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 };