Пример #1
0
        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
            }
        }