Skip to content

Commit

Permalink
feat(core): run app cleanup code before updater restart, closes #3605 (
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Mar 5, 2022
1 parent 58070c1 commit fce7d3b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changes/updater-restart-cleanup.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Run `AppHandle` cleanup code before restarting the application when a new update is installed.
8 changes: 7 additions & 1 deletion core/tauri/src/app.rs
Expand Up @@ -289,12 +289,18 @@ impl<R: Runtime> AppHandle<R> {
Ok(())
}

/// Exits the app
/// Exits the app. This is the same as [`std::process::exit`], but it performs cleanup on this application.
pub fn exit(&self, exit_code: i32) {
self.cleanup_before_exit();
std::process::exit(exit_code);
}

/// Restarts the app. This is the same as [`crate::api::process::restart`], but it performs cleanup on this application.
pub fn restart(&self) {
self.cleanup_before_exit();
crate::api::process::restart(&self.env());
}

/// Runs necessary cleanup tasks before exiting the process
fn cleanup_before_exit(&self) {
#[cfg(shell_execute)]
Expand Down
5 changes: 5 additions & 0 deletions core/tauri/src/lib.rs
Expand Up @@ -399,6 +399,11 @@ impl<A: Assets> Context<A> {
// TODO: expand these docs
/// Manages a running application.
pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
/// The application handle associated with this manager.
fn app_handle(&self) -> AppHandle<R> {
sealed::ManagerBase::app_handle(self)
}

/// The [`Config`] the manager was created with.
fn config(&self) -> Arc<Config> {
self.manager().config()
Expand Down
8 changes: 2 additions & 6 deletions core/tauri/src/updater/mod.rs
Expand Up @@ -333,10 +333,7 @@ mod error;
pub use self::error::Error;

use crate::{
api::{dialog::blocking::ask, process::restart},
runtime::Runtime,
utils::config::UpdaterConfig,
Env, Manager, Window,
api::dialog::blocking::ask, runtime::Runtime, utils::config::UpdaterConfig, Env, Manager, Window,
};

/// Check for new updates
Expand Down Expand Up @@ -560,14 +557,13 @@ Release Notes:
updater.download_and_install(pubkey.clone()).await?;

// Ask user if we need to restart the application
let env = window.state::<Env>().inner().clone();
let should_exit = ask(
Some(&window),
"Ready to Restart",
"The installation was successful, do you want to restart the application now?",
);
if should_exit {
restart(&env);
window.app_handle().restart();
}
}

Expand Down

0 comments on commit fce7d3b

Please sign in to comment.