public static async Task DownloadUpdaterAsync(Progress <int> downloadProgress) { try { var url = ApplicationStateManager.GetNewVersionUpdaterUrl(); var downloadRootPath = Path.Combine(Paths.Root, "updaters"); if (!Directory.Exists(downloadRootPath)) { Directory.CreateDirectory(downloadRootPath); } var(success, downloadedFilePath) = await MinersDownloadManager.DownloadFileWebClientAsync(url, downloadRootPath, $"nhm_windows_updater_{ApplicationStateManager.OnlineVersion}", downloadProgress, ApplicationStateManager.ExitApplication.Token); if (!success || ApplicationStateManager.ExitApplication.Token.IsCancellationRequested) { return; } // stop devices ApplicationStateManager.StopAllDevice(); using (var updater = new Process()) { updater.StartInfo.UseShellExecute = false; updater.StartInfo.FileName = downloadedFilePath; updater.Start(); } } catch (Exception ex) { Logger.Error("UpdateHelpers", $"Updating failed: {ex.Message}"); } }
internal static async Task <bool> StartDownloadingUpdater(bool isUpdater) { try { // Let user know that something is happening after update process started var updateNotification = NotificationsManager.Instance.Notifications.Find(notif => notif.Group == NotificationsGroup.NhmUpdate); if (updateNotification != null) { updateNotification.NotificationContent = Translations.Tr("Download in progress..."); } // determine what how to update // #1 download updater.exe or zip depending on bin type var url = isUpdater ? VersionState.Instance.GetNewVersionUpdaterUrl() : VersionState.Instance.GetNewVersionZipUrl(); var downloadRootPath = Path.Combine(Paths.Root, "updaters"); if (!Directory.Exists(downloadRootPath)) { Directory.CreateDirectory(downloadRootPath); } var saveAsFile = isUpdater ? $"nhm_windows_updater_{VersionState.Instance.OnlineVersionStr}" : $"nhm_windows_{VersionState.Instance.OnlineVersionStr}"; var downloadProgress = updateNotification?.Action?.Progress ?? null; var(success, downloadedFilePath) = await MinersDownloadManager.DownloadFileWebClientAsync(url, downloadRootPath, saveAsFile, downloadProgress, ApplicationStateManager.ExitApplication.Token); if (!success) { if (updateNotification != null) { updateNotification.NotificationContent = Translations.Tr("Download unsuccessfull"); } return(false); } else { if (updateNotification != null) { updateNotification.NotificationContent = Translations.Tr("Download successfull"); } return(true); } } catch (Exception ex) { Logger.Error(Tag, $"StartDownloadingUpdater autoupdate Exception: {ex.Message}"); return(false); }; }
private async void UpdaterBtn_Click(object sender, EventArgs e) { UpdaterBtn.Enabled = false; GithubBtn.Enabled = false; ProgressBarVisible = true; try { var progressDownload = new Progress <(string loadMessageText, int perc)>(p => { progressBar1.Value = p.perc; label1.Text = p.loadMessageText; }); IProgress <(string loadMessageText, int prog)> progress = progressDownload; var downloadProgress = new Progress <int>(perc => progress?.Report((Translations.Tr($"Downloading updater: %{perc}"), perc))); var url = "https://github.com/luc1an24/pluginTesting/releases/download/1.0.1/nhm_windows_updater_1.9.2.12.exe"; var downloadRootPath = Path.GetTempPath(); var(success, downloadedFilePath) = await MinersDownloadManager.DownloadFileWebClientAsync(url, downloadRootPath, $"nhm_windows_updater_{ApplicationStateManager.OnlineVersion}", downloadProgress, ApplicationStateManager.ExitApplication.Token); if (!success || ApplicationStateManager.ExitApplication.Token.IsCancellationRequested) { return; } // stop devices ApplicationStateManager.StopAllDevice(); using (var updater = new Process()) { updater.StartInfo.UseShellExecute = false; updater.StartInfo.FileName = downloadedFilePath; updater.Start(); } } catch (Exception ex) { Logger.Error("Updater", $"Updating failed: {ex.Message}"); } ProgressBarVisible = false; UpdaterBtn.Enabled = true; GithubBtn.Enabled = true; Close(); }
private static async Task NhmAutoUpdateCheckLoop(CancellationToken stop) { try { // check for updates every 30s //var checkWaitTime = TimeSpan.FromSeconds(30); var checkWaitTime = TimeSpan.FromSeconds(60); // TODO DEBUG Func <bool> isActive = () => !stop.IsCancellationRequested; while (isActive()) { if (isActive()) { await TaskHelpers.TryDelay(checkWaitTime, stop); } var isAutoUpdate = UpdateSettings.Instance.AutoUpdateNiceHashMiner; var hasNewVersion = VersionState.Instance.IsNewVersionAvailable; // prevent sleep check if (isActive() && isAutoUpdate && hasNewVersion && !Launcher.IsUpdatedFailed) { try { // determine what how to update bool isUpdater = IsNHMInstalled() && IsRunningInstalledApp(); // #1 download updater.exe or zip depending on bin type var url = isUpdater ? VersionState.Instance.GetNewVersionUpdaterUrl() : VersionState.Instance.GetNewVersionZipUrl(); var downloadRootPath = Path.Combine(Paths.Root, "updaters"); if (!Directory.Exists(downloadRootPath)) { Directory.CreateDirectory(downloadRootPath); } var saveAsFile = isUpdater ? $"nhm_windows_updater_{VersionState.Instance.OnlineVersionStr}" : $"nhm_windows_{VersionState.Instance.OnlineVersionStr}"; var(success, downloadedFilePath) = await MinersDownloadManager.DownloadFileWebClientAsync(url, downloadRootPath, saveAsFile, DownloadProgress, ApplicationStateManager.ExitApplication.Token); if (!success) { // TODO notify that we cannot download the miner updates file continue; } OnAutoUpdate?.Invoke(); // #2 SAVE current state so we can resume it after the client updates ApplicationStateManager.SaveMiningState(); await Task.Delay(5000); // wait 5 seconds await ApplicationStateManager.StopAllDevicesTask(); await Task.Delay(5000); // wait 5 seconds // #3 restart accordingly if launcher or self containd app if (Launcher.IsLauncher) { try { // TODO here save what version and maybe kind of update we have File.Create(Paths.RootPath("do.update")); ApplicationStateManager.ExecuteApplicationExit(); } catch (Exception e) { Logger.Error("NICEHASH", $"Autoupdate IsLauncher error: {e.Message}"); // IF we fail restore mining state and show autoupdater failure nofitication await ApplicationStateManager.RestoreMiningState(); // TODO notify that the auto-update wasn't successful } } else { // TODO non launcher not priority right now } } catch (Exception ex) { Logger.Error(Tag, $"Check autoupdate Exception: {ex.Message}"); } } } } catch (TaskCanceledException e) { Logger.Info(Tag, $"NhmAutoUpdateCheckLoop TaskCanceledException: {e.Message}"); } catch (Exception e) { Logger.Error(Tag, $"NhmAutoUpdateCheckLoop Exception: {e.Message}"); } finally { Logger.Info(Tag, "Exiting NhmAutoUpdateCheckLoop run cleanup"); // cleanup } }
internal static async Task StartUpdateProcess(bool isUpdater) { try { // Let user know that something is happening after update process started if (Launcher.IsLauncher) { var notifications = NotificationsManager.Instance.Notifications; var updateNotification = notifications.Find(notif => notif.Group == NotificationsGroup.NhmUpdate); updateNotification.NotificationContent = Translations.Tr("Update in progress..."); } // determine what how to update // #1 download updater.exe or zip depending on bin type var url = isUpdater ? VersionState.Instance.GetNewVersionUpdaterUrl() : VersionState.Instance.GetNewVersionZipUrl(); var downloadRootPath = Path.Combine(Paths.Root, "updaters"); if (!Directory.Exists(downloadRootPath)) { Directory.CreateDirectory(downloadRootPath); } var saveAsFile = isUpdater ? $"nhm_windows_updater_{VersionState.Instance.OnlineVersionStr}" : $"nhm_windows_{VersionState.Instance.OnlineVersionStr}"; var(success, downloadedFilePath) = await MinersDownloadManager.DownloadFileWebClientAsync(url, downloadRootPath, saveAsFile, DownloadProgress, ApplicationStateManager.ExitApplication.Token); if (!success) { // TODO notify that we cannot download the miner updates file return; } OnAutoUpdate?.Invoke(); // #2 SAVE current state so we can resume it after the client updates ApplicationStateManager.SaveMiningState(); await Task.Delay(5000); // wait 5 seconds await ApplicationStateManager.StopAllDevicesTask(); await Task.Delay(5000); // wait 5 seconds // #3 restart accordingly if launcher or self containd app if (Launcher.IsLauncher) { try { // TODO here save what version and maybe kind of update we have File.Create(Paths.RootPath("do.update")); ApplicationStateManager.ExecuteApplicationExit(); } catch (Exception e) { Logger.Error("NICEHASH", $"Autoupdate IsLauncher error: {e.Message}"); // IF we fail restore mining state and show autoupdater failure nofitication await ApplicationStateManager.RestoreMiningState(); // TODO notify that the auto-update wasn't successful } } else { // TODO non launcher not priority right now } } catch (Exception ex) { Logger.Error(Tag, $"Check autoupdate Exception: {ex.Message}"); } }