Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tracing around autoupdate activity #174

Merged
merged 1 commit into from
Jun 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/EventLogExpert/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ internal static class Utils
internal static async Task CheckForUpdates(bool isPrerelease, bool manualScan = false)
{
Version currentVersion = GetCurrentVersion();

Trace($"{nameof(CheckForUpdates)} was called. {nameof(isPrerelease)} is {isPrerelease}. {nameof(manualScan)} is {manualScan}. {nameof(currentVersion)} is {currentVersion}.");

GitReleaseModel? latest = null;
bool showDialog = Application.Current?.MainPage is not null;

if (currentVersion.Major <= 1)
{
_isDevBuild = true;
Trace($"{nameof(CheckForUpdates)} {nameof(_isDevBuild)}: {_isDevBuild}. Skipping update check.");
return;
}

Expand All @@ -47,23 +51,27 @@ internal static async Task CheckForUpdates(bool isPrerelease, bool manualScan =

if (response.IsSuccessStatusCode is not true)
{
Trace($"{nameof(CheckForUpdates)} Attempt to retrieve {response?.RequestMessage?.RequestUri} failed: {response?.StatusCode}. {nameof(showDialog)} is {showDialog}.");
if (showDialog)
{
await Application.Current!.MainPage!.DisplayAlert("Update Failure",
$"Unable to reach download site:\r\n{response.StatusCode}",
$"Unable to reach download site:\r\n{response?.StatusCode}",
"Ok");
}

return;
}

Trace($"{nameof(CheckForUpdates)} Attempt to retrieve {response?.RequestMessage?.RequestUri} succeeded: {response?.StatusCode}.");

try
{
var stream = await response.Content.ReadAsStreamAsync();
var content = await JsonSerializer.DeserializeAsync<IEnumerable<GitReleaseModel>>(stream);

if (content is null)
{
Trace($"{nameof(CheckForUpdates)} Failed to deserialize response stream.");
if (showDialog)
{
await Application.Current!.MainPage!.DisplayAlert("Update Failure",
Expand All @@ -78,12 +86,19 @@ internal static async Task CheckForUpdates(bool isPrerelease, bool manualScan =
// stripping the v off the Version for every release
var releases = content.OrderByDescending(x => x.ReleaseDate).ToArray();

Trace($"{nameof(CheckForUpdates)} Found the following releases:");
foreach (var release in releases)
{
Trace($"{nameof(CheckForUpdates)} Version: {release.Version} ReleaseDate: {release.ReleaseDate} IsPrerelease: {release.IsPrerelease}");
}

latest = isPrerelease ?
releases.FirstOrDefault() :
releases.FirstOrDefault(x => !x.IsPrerelease);

if (latest is null)
{
Trace($"{nameof(CheckForUpdates)} Could not find latest release.");
if (showDialog)
{
await Application.Current!.MainPage!.DisplayAlert("Update Failure",
Expand All @@ -94,9 +109,13 @@ internal static async Task CheckForUpdates(bool isPrerelease, bool manualScan =
return;
}

Trace($"{nameof(CheckForUpdates)} Could not find latest release.");

// Need to drop the v off the version number provided by GitHub
var newVersion = new Version(latest.Version.TrimStart('v'));

Trace($"{nameof(CheckForUpdates)} {nameof(newVersion)} {newVersion} equals {nameof(currentVersion)} {currentVersion}. {nameof(showDialog)} is {showDialog}. {nameof(manualScan)} is {manualScan}.");

// Setting version to equal allows rollback if a version is pulled
if (newVersion.CompareTo(currentVersion) == 0)
{
Expand All @@ -112,6 +131,7 @@ internal static async Task CheckForUpdates(bool isPrerelease, bool manualScan =

if (downloadPath is null)
{
Trace($"{nameof(CheckForUpdates)} Could not get asset download path. {nameof(showDialog)} is {showDialog}.");
if (showDialog)
{
await Application.Current!.MainPage!.DisplayAlert("Update Failure",
Expand All @@ -131,12 +151,16 @@ internal static async Task CheckForUpdates(bool isPrerelease, bool manualScan =
"Yes", "No");
}

Trace($"{nameof(CheckForUpdates)} {nameof(shouldReboot)} is {shouldReboot} after possible dialog. {nameof(showDialog)} was {showDialog}.");

PackageManager packageManager = new();

IAsyncOperationWithProgress<DeploymentResult, DeploymentProgress> deployment;

if (shouldReboot)
{
Trace($"{nameof(CheckForUpdates)} Calling {nameof(NativeMethods.RegisterApplicationRestart)}.");

uint res = NativeMethods.RegisterApplicationRestart(null, NativeMethods.RestartFlags.NONE);

if (res != 0) { return; }
Expand All @@ -149,6 +173,8 @@ internal static async Task CheckForUpdates(bool isPrerelease, bool manualScan =
}
else
{
Trace($"{nameof(CheckForUpdates)} Calling {nameof(packageManager.AddPackageByUriAsync)}.");

deployment = packageManager.AddPackageByUriAsync(new Uri(downloadPath),
new AddPackageOptions
{
Expand Down Expand Up @@ -241,7 +267,7 @@ internal static void InitTracing()
};
}

internal static void Trace(string message) => System.Diagnostics.Trace.WriteLine($"{DateTime.Now:o} {message}");
internal static void Trace(string message) => System.Diagnostics.Trace.WriteLine($"{DateTime.Now:o} {Environment.CurrentManagedThreadId} {message}");

internal static void UpdateAppTitle(string? logName = null)
{
Expand Down