Skip to content

Commit

Permalink
Fix monthly checks for UpdaterPlugin (#1065)
Browse files Browse the repository at this point in the history
Previously, using a monthly check for updates would cause an integer overflow. 

This was because of the maximum `dueDate` in the `Timer` class. This has been resolved by triggering the event daily and verify whether a check can occur on that date.
  • Loading branch information
dalyIsaac authored Nov 2, 2024
1 parent 3b91392 commit 2fadf98
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 73 deletions.
27 changes: 0 additions & 27 deletions src/Whim.Updater.Tests/UpdaterConfigTests.cs

This file was deleted.

33 changes: 4 additions & 29 deletions src/Whim.Updater/UpdaterConfig.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Timers;

namespace Whim.Updater;

/// <summary>
Expand Down Expand Up @@ -32,44 +29,22 @@ public enum UpdateFrequency
/// <summary>
/// Check for updates daily.
/// </summary>
Daily,
Daily = 1,

/// <summary>
/// Check for updates weekly.
/// </summary>
Weekly,
Weekly = 7,

/// <summary>
/// Check for updates monthly.
/// </summary>
Monthly,
Monthly = 28,

/// <summary>
/// Never check for updates.
/// </summary>
Never,
}

/// <summary>
/// Extensions for <see cref="UpdateFrequency"/>.
/// </summary>
public static class UpdateFrequencyExtensions
{
/// <summary>
/// Gets a <see cref="double"/> value <see cref="Timer.Interval"/> for the given
/// <see cref="UpdateFrequency"/>.
/// </summary>
/// <param name="frequency"></param>
/// <returns></returns>
public static double? GetInterval(this UpdateFrequency frequency) =>
frequency switch
{
UpdateFrequency.Daily => TimeSpan.FromDays(1).TotalMilliseconds,
UpdateFrequency.Weekly => TimeSpan.FromDays(7).TotalMilliseconds,
UpdateFrequency.Monthly => TimeSpan.FromDays(30).TotalMilliseconds,
UpdateFrequency.Never => null,
_ => null,
};
Never = 0,
}

/// <summary>
Expand Down
30 changes: 16 additions & 14 deletions src/Whim.Updater/UpdaterPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,7 @@ public UpdaterPlugin(IContext context, UpdaterConfig config)
Config = config;
_releaseManager = new ReleaseManager(context, this);

if (config.UpdateFrequency.GetInterval() is double interval)
{
_timer.Interval = interval;
}
else
{
_timer.Enabled = false;
}
_timer.Interval = TimeSpan.FromDays(1).TotalMilliseconds;
}

/// <inheritdoc />
Expand All @@ -94,16 +87,25 @@ private void OnDeferUpdateNotificationReceived(AppNotificationActivatedEventArgs
/// <inheritdoc />
public void PostInitialize()
{
#if !DEBUG
CheckForUpdates(false).ConfigureAwait(true);
#endif

_timer.Elapsed += Timer_Elapsed;
_timer.Start();
}

private async void Timer_Elapsed(object? sender, ElapsedEventArgs e) =>
await CheckForUpdates().ConfigureAwait(true);
private async void Timer_Elapsed(object? sender, ElapsedEventArgs e)
{
int updateFrequency = (int)Config.UpdateFrequency;
if (updateFrequency <= 0)
{
Logger.Error($"Invalid update frequency: {Config.UpdateFrequency}");
return;
}

DateTime nextCheckDate = LastCheckedForUpdates?.AddDays(updateFrequency) ?? DateTime.Today;
if (DateTime.Today >= nextCheckDate)
{
await CheckForUpdates().ConfigureAwait(true);
}
}

/// <inheritdoc />
public void SkipRelease(string? tagName = null)
Expand Down
6 changes: 3 additions & 3 deletions src/Whim/Template/whim.config.csx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.UI;
using Microsoft.UI.Xaml.Markup;
using Microsoft.UI.Xaml.Media;
using Whim;
using Whim.Bar;
using Whim.CommandPalette;
Expand All @@ -31,9 +34,6 @@ using Whim.TreeLayout.CommandPalette;
using Whim.Updater;
using Whim.Yaml;
using Windows.Win32.UI.Input.KeyboardAndMouse;
using Microsoft.UI;
using Microsoft.UI.Xaml.Markup;
using Microsoft.UI.Xaml.Media;

/// <summary>
/// This is what's called when Whim is loaded.
Expand Down

0 comments on commit 2fadf98

Please sign in to comment.