summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/BackgroundService.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI.Web/BackgroundService.cs')
-rw-r--r--src/SMAPI.Web/BackgroundService.cs22
1 files changed, 19 insertions, 3 deletions
diff --git a/src/SMAPI.Web/BackgroundService.cs b/src/SMAPI.Web/BackgroundService.cs
index 64bd5ca5..49356f76 100644
--- a/src/SMAPI.Web/BackgroundService.cs
+++ b/src/SMAPI.Web/BackgroundService.cs
@@ -19,13 +19,17 @@ namespace StardewModdingAPI.Web
** Fields
*********/
/// <summary>The background task server.</summary>
- private static BackgroundJobServer JobServer;
+ private static BackgroundJobServer? JobServer;
/// <summary>The cache in which to store wiki metadata.</summary>
- private static IWikiCacheRepository WikiCache;
+ private static IWikiCacheRepository? WikiCache;
/// <summary>The cache in which to store mod data.</summary>
- private static IModCacheRepository ModCache;
+ private static IModCacheRepository? ModCache;
+
+ /// <summary>Whether the service has been started.</summary>
+ [MemberNotNullWhen(true, nameof(BackgroundService.JobServer), nameof(BackgroundService.WikiCache), nameof(BackgroundService.ModCache))]
+ private static bool IsStarted { get; set; }
/*********
@@ -59,6 +63,8 @@ namespace StardewModdingAPI.Web
RecurringJob.AddOrUpdate(() => BackgroundService.UpdateWikiAsync(), "*/10 * * * *"); // every 10 minutes
RecurringJob.AddOrUpdate(() => BackgroundService.RemoveStaleModsAsync(), "0 * * * *"); // hourly
+ BackgroundService.IsStarted = true;
+
return Task.CompletedTask;
}
@@ -66,6 +72,8 @@ namespace StardewModdingAPI.Web
/// <param name="cancellationToken">Tracks whether the shutdown process should no longer be graceful.</param>
public async Task StopAsync(CancellationToken cancellationToken)
{
+ BackgroundService.IsStarted = false;
+
if (BackgroundService.JobServer != null)
await BackgroundService.JobServer.WaitForShutdownAsync(cancellationToken);
}
@@ -73,6 +81,8 @@ namespace StardewModdingAPI.Web
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{
+ BackgroundService.IsStarted = false;
+
BackgroundService.JobServer?.Dispose();
}
@@ -83,6 +93,9 @@ namespace StardewModdingAPI.Web
[AutomaticRetry(Attempts = 3, DelaysInSeconds = new[] { 30, 60, 120 })]
public static async Task UpdateWikiAsync()
{
+ if (!BackgroundService.IsStarted)
+ throw new InvalidOperationException($"Must call {nameof(BackgroundService.StartAsync)} before scheduling tasks.");
+
WikiModList wikiCompatList = await new ModToolkit().GetWikiCompatibilityListAsync();
BackgroundService.WikiCache.SaveWikiData(wikiCompatList.StableVersion, wikiCompatList.BetaVersion, wikiCompatList.Mods);
}
@@ -90,6 +103,9 @@ namespace StardewModdingAPI.Web
/// <summary>Remove mods which haven't been requested in over 48 hours.</summary>
public static Task RemoveStaleModsAsync()
{
+ if (!BackgroundService.IsStarted)
+ throw new InvalidOperationException($"Must call {nameof(BackgroundService.StartAsync)} before scheduling tasks.");
+
BackgroundService.ModCache.RemoveStaleMods(TimeSpan.FromHours(48));
return Task.CompletedTask;
}