Skip to content

Commit

Permalink
Merge pull request #2037 from nicehash/master-manualUpdate
Browse files Browse the repository at this point in the history
give option for manual update of miner
  • Loading branch information
luc1an24 authored Apr 15, 2020
2 parents 615ef03 + 4b89654 commit 3723548
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 63 deletions.
2 changes: 0 additions & 2 deletions src/NHMCore/ApplicationState/VersionState.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using NHM.Common;
using NHMCore.Notifications;
using NHMCore.Utils;
using System;
using System.Windows.Forms;
Expand Down Expand Up @@ -50,7 +49,6 @@ public bool IsNewVersionAvailable
// display new version
// notify all components
//DisplayVersion?.Invoke(null, displayNewVer); // TODO broken make VersionUpdatesState
if (hasNewVersion) AvailableNotifications.CreateNhmUpdateInfo();
return hasNewVersion;
}
catch (Exception e)
Expand Down
24 changes: 19 additions & 5 deletions src/NHMCore/Notifications/AvailableNotifications.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,30 @@ public static void CreateIs64BitOSInfo()
NotificationsManager.Instance.AddNotificationToList(notification);
}

public static void CreateNhmUpdateInfo()
public static void CreateNhmUpdateInfo(bool isInstallerVersion)
{
var notification = new Notification(NotificationsType.Info, NotificationsGroup.NhmUpdate, Tr("NiceHash Miner Update"), Tr("New version of NiceHash Miner is available."));
if (!Configs.UpdateSettings.Instance.AutoUpdateNiceHashMiner)
{
notification.Actions.Add(new NotificationAction
if (isInstallerVersion)
{
Info = Tr("Visit release Page"),
Action = () => {Process.Start(Links.VisitReleasesUrl); }
});
notification.Actions.Add(new NotificationAction
{
Info = Tr("Start updater"),
Action = () =>
{
ApplicationStateManager.App.Dispatcher.Invoke(async () => await UpdateHelpers.StartUpdateProcess(isInstallerVersion));
}
});
}
else
{
notification.Actions.Add(new NotificationAction
{
Info = Tr("Visit release Page"),
Action = () => { Process.Start(Links.VisitReleasesUrl); }
});
}
}

NotificationsManager.Instance.AddNotificationToList(notification);
Expand Down
119 changes: 68 additions & 51 deletions src/NHMCore/Utils/UpdateHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using NHM.MinersDownloader;
using NHMCore.ApplicationState;
using NHMCore.Configs;
using NHMCore.Notifications;
using System;
using System.Diagnostics;
using System.IO;
Expand Down Expand Up @@ -62,59 +63,16 @@ private static async Task NhmAutoUpdateCheckLoop(CancellationToken stop)
var isAutoUpdate = UpdateSettings.Instance.AutoUpdateNiceHashMiner;
var hasNewVersion = VersionState.Instance.IsNewVersionAvailable;
// prevent sleep check

bool isUpdater = IsNHMInstalled() && IsRunningInstalledApp();
if (hasNewVersion)
{
AvailableNotifications.CreateNhmUpdateInfo(isUpdater);
}

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}");
}
await StartUpdateProcess(isUpdater);
}
}
}
Expand All @@ -133,7 +91,66 @@ private static async Task NhmAutoUpdateCheckLoop(CancellationToken stop)
}
}

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}");
}
}

public static async Task DownloadUpdaterAsync(Progress<int> downloadProgress)
{
Expand Down
4 changes: 2 additions & 2 deletions src/NiceHashMiner/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]

[assembly: AssemblyVersion("3.0.0.7")]
[assembly: AssemblyFileVersion("3.0.0.7")]
[assembly: AssemblyVersion("3.0.0.8")]
[assembly: AssemblyFileVersion("3.0.0.8")]
1 change: 0 additions & 1 deletion src/NiceHashMiner/Views/Login/LoginBrowser.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ private void Browser_Loaded(object sender, RoutedEventArgs e)
{
browser.Navigate(Links.LoginNHM);
_evalTimer = new Timer((s) => { Dispatcher.Invoke(EvalTimer_Elapsed); }, null, 100, 1000);

}

private async void EvalTimer_Elapsed()
Expand Down
6 changes: 6 additions & 0 deletions src/NiceHashMiner/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -3905,6 +3905,12 @@
},
"When checked, NiceHash Miner will show you net profit (gross profit minus electricity costs).": {
"ru": "Если отмечено, NiceHash Miner будет показывать вам чистый доход (валовой доход минус расходы на электроэнергию)."
},
"Start updater": {
"ru": "Запуск обновления"
},
"Update in progress...": {
"ru": "Выполняется обновление..."
}
}
}
4 changes: 2 additions & 2 deletions src/NiceHashMinerLauncher/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]

[assembly: AssemblyVersion("3.0.0.7")]
[assembly: AssemblyFileVersion("3.0.0.7")]
[assembly: AssemblyVersion("3.0.0.8")]
[assembly: AssemblyFileVersion("3.0.0.8")]

0 comments on commit 3723548

Please sign in to comment.