Compare commits

..
3 changed files with 46 additions and 22 deletions
+24 -18
View File
@@ -181,7 +181,8 @@ fn nsis_language_id(app_language: &str) -> &'static str {
}
}
/// Same wire shape as the updater plugin's JS `DownloadEvent`.
/// Same wire shape as the updater plugin's JS `DownloadEvent`, plus `Installing` when the
/// install starts.
#[derive(Clone, Serialize, specta::Type)]
#[serde(tag = "event", content = "data")]
pub enum DownloadEvent {
@@ -194,12 +195,17 @@ pub enum DownloadEvent {
chunk_length: usize,
},
Finished,
Installing,
}
const MIRRORS: [&str; 2] = ["https://update.hwdns.net/", "https://gh-proxy.org/"];
/// A source delivering less than this per window is dropped for the next one.
const MIN_PROGRESS: u64 = 1024 * 1024;
const PROGRESS_WINDOW: Duration = Duration::from_secs(30);
#[cfg(target_os = "windows")]
const INSTALL_TIMEOUT: Duration = Duration::from_secs(30);
#[cfg(not(target_os = "windows"))]
const INSTALL_TIMEOUT: Duration = Duration::from_secs(120);
/// The manifest's URL, then the other mirror, then GitHub itself.
fn download_sources(url: &tauri::Url) -> Vec<tauri::Url> {
@@ -292,8 +298,18 @@ impl SilentUpdater {
Ok(updater_builder.build()?.check().await?)
}
/// Installs verified bytes, dropping the cache first so a failure leaves nothing to
/// re-trigger. A timeout abandons the await, not the detached install task; a successful
/// Windows install exits the process inside the plugin (#2558).
async fn install(update: Update, bytes: Vec<u8>) -> Result<()> {
Ok(tokio::task::spawn_blocking(move || update.install(&bytes)).await??)
Self::delete_cache();
let install = tokio::task::spawn_blocking(move || update.install(&bytes));
match tokio::time::timeout(INSTALL_TIMEOUT, install).await {
Ok(result) => Ok(result??),
Err(_) => Err(anyhow::anyhow!(
"install timed out after {INSTALL_TIMEOUT:?}; it may still finish in the background"
)),
}
}
/// Installs a newer cached update before normal startup, if the user confirms.
@@ -368,23 +384,13 @@ impl SilentUpdater {
Self::show_update_splash(app_handle, &version);
// `install()` may hang (#2558); on Windows NSIS can take over without returning.
let install = tokio::time::timeout(std::time::Duration::from_secs(30), Self::install(update, bytes));
match install
.await
.unwrap_or_else(|_| Err(anyhow::anyhow!("install timed out (30s)")))
{
match Self::install(update, bytes).await {
Ok(()) => {
logging!(info, Type::System, "Update v{version} install triggered at startup");
Self::delete_cache();
logging!(info, Type::System, "Update v{version} installed at startup");
true
}
Err(e) => {
logging!(
warn,
Type::System,
"Startup install failed: {e:#}, will retry next launch"
);
logging!(warn, Type::System, "Startup install failed: {e:#}");
Self::close_update_splash(app_handle);
false
}
@@ -402,7 +408,7 @@ impl SilentUpdater {
let cancel = CancellationToken::new();
*self.manual_cancel.lock() = Some(cancel.clone());
let prepared = tokio::select! {
prepared = Self::prepare_install(app_handle, version, on_event) => Some(prepared),
prepared = Self::prepare_install(app_handle, version, &on_event) => Some(prepared),
() = cancel.cancelled() => None,
};
// `cancel_download` cancels under the lock, so once the token is taken back its state is
@@ -417,15 +423,15 @@ impl SilentUpdater {
};
logging!(info, Type::System, "Installing update v{version}...");
on_event(DownloadEvent::Installing);
Self::install(update, bytes).await?;
Self::delete_cache();
Ok(InstallUpdateOutcome::Installed)
}
async fn prepare_install(
app_handle: &tauri::AppHandle,
version: &str,
on_event: impl Fn(DownloadEvent) + Sync,
on_event: &(impl Fn(DownloadEvent) + Sync),
) -> Result<Option<(Update, Vec<u8>)>> {
let Some(update) = Self::check(app_handle)
.await?
@@ -267,6 +267,9 @@ export function UpdateViewer({ ref }: { ref?: Ref<UpdateViewerRef> }) {
})
const { downloaded, total } = download
// Install cannot be cancelled; only the download can.
const [installing, setInstalling] = useState(false)
const progress = total <= 0 ? 0 : Math.min((downloaded / total) * 100, 100)
useImperativeHandle(ref, () => ({
@@ -333,6 +336,8 @@ export function UpdateViewer({ ref }: { ref?: Ref<UpdateViewerRef> }) {
...prev,
total: prev.total || prev.downloaded,
}))
} else if (event.event === 'Installing') {
setInstalling(true)
}
}
@@ -366,6 +371,7 @@ export function UpdateViewer({ ref }: { ref?: Ref<UpdateViewerRef> }) {
} finally {
setUpdateState(false)
setDownload({ downloaded: 0, total: 0 })
setInstalling(false)
}
})
@@ -419,6 +425,7 @@ export function UpdateViewer({ ref }: { ref?: Ref<UpdateViewerRef> }) {
}}
okBtn={t('settings.modals.update.actions.update')}
cancelBtn={t('shared.actions.cancel')}
disableCancel={installing}
onClose={() => setOpen(false)}
onCancel={() => {
if (updateState) {
@@ -560,7 +567,7 @@ export function UpdateViewer({ ref }: { ref?: Ref<UpdateViewerRef> }) {
</Box>
{updateState && (
<LinearProgress
variant={total > 0 ? 'determinate' : 'indeterminate'}
variant={total > 0 && !installing ? 'determinate' : 'indeterminate'}
value={progress}
sx={{ mt: 1 }}
/>
+14 -3
View File
@@ -260,10 +260,16 @@ export type DnsOverrideOutcome =
| { status: 'applied' }
| { status: 'confirmation_required'; source: string }
/** Same wire shape as the updater plugin's JS `DownloadEvent`. */
/**
* Same wire shape as the updater plugin's JS `DownloadEvent`, plus `Installing` when the
* install starts.
*/
export type DownloadEvent = DownloadEvent_Serialize | DownloadEvent_Deserialize
/** Same wire shape as the updater plugin's JS `DownloadEvent`. */
/**
* Same wire shape as the updater plugin's JS `DownloadEvent`, plus `Installing` when the
* install starts.
*/
export type DownloadEvent_Deserialize =
| {
event: 'Started'
@@ -278,8 +284,12 @@ export type DownloadEvent_Deserialize =
}
}
| { event: 'Finished' }
| { event: 'Installing' }
/** Same wire shape as the updater plugin's JS `DownloadEvent`. */
/**
* Same wire shape as the updater plugin's JS `DownloadEvent`, plus `Installing` when the
* install starts.
*/
export type DownloadEvent_Serialize =
| {
event: 'Started'
@@ -294,6 +304,7 @@ export type DownloadEvent_Serialize =
}
}
| { event: 'Finished' }
| { event: 'Installing' }
/** Operation associated with a pending failure. */
export type FailedOperation =