summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/BackgroundService.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2019-07-19 13:15:45 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2019-09-14 18:52:58 -0400
commitce6cedaf4be53d52f2e558055b91e515b92e4c83 (patch)
tree2984ef22c225e25221c080eb0b26466d99aae1b6 /src/SMAPI.Web/BackgroundService.cs
parent88110dffbf4bace738e8f29133bfb3ac4b265e2c (diff)
downloadSMAPI-ce6cedaf4be53d52f2e558055b91e515b92e4c83.tar.gz
SMAPI-ce6cedaf4be53d52f2e558055b91e515b92e4c83.tar.bz2
SMAPI-ce6cedaf4be53d52f2e558055b91e515b92e4c83.zip
add background fetch for mod compatibility list (#651)
Diffstat (limited to 'src/SMAPI.Web/BackgroundService.cs')
-rw-r--r--src/SMAPI.Web/BackgroundService.cs92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/SMAPI.Web/BackgroundService.cs b/src/SMAPI.Web/BackgroundService.cs
new file mode 100644
index 00000000..2ccfd5f7
--- /dev/null
+++ b/src/SMAPI.Web/BackgroundService.cs
@@ -0,0 +1,92 @@
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+using Hangfire;
+using Microsoft.Extensions.Hosting;
+using StardewModdingAPI.Toolkit;
+using StardewModdingAPI.Toolkit.Framework.Clients.Wiki;
+using StardewModdingAPI.Web.Framework.Caching.Wiki;
+
+namespace StardewModdingAPI.Web
+{
+ /// <summary>A hosted service which runs background data updates.</summary>
+ /// <remarks>Task methods need to be static, since otherwise Hangfire will try to serialise the entire instance.</remarks>
+ internal class BackgroundService : IHostedService, IDisposable
+ {
+ /*********
+ ** Fields
+ *********/
+ /// <summary>The background task server.</summary>
+ private static BackgroundJobServer JobServer;
+
+ /// <summary>The cache in which to store mod metadata.</summary>
+ private static IWikiCacheRepository WikiCache;
+
+
+ /*********
+ ** Public methods
+ *********/
+ /****
+ ** Hosted service
+ ****/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="wikiCache">The cache in which to store mod metadata.</param>
+ public BackgroundService(IWikiCacheRepository wikiCache)
+ {
+ BackgroundService.WikiCache = wikiCache;
+ }
+
+ /// <summary>Start the service.</summary>
+ /// <param name="cancellationToken">Tracks whether the start process has been aborted.</param>
+ public Task StartAsync(CancellationToken cancellationToken)
+ {
+ this.TryInit();
+
+ // set startup tasks
+ BackgroundJob.Enqueue(() => BackgroundService.UpdateWikiAsync());
+
+ // set recurring tasks
+ RecurringJob.AddOrUpdate(() => BackgroundService.UpdateWikiAsync(), "*/10 * * * *");
+
+ return Task.CompletedTask;
+ }
+
+ /// <summary>Triggered when the application host is performing a graceful shutdown.</summary>
+ /// <param name="cancellationToken">Tracks whether the shutdown process should no longer be graceful.</param>
+ public async Task StopAsync(CancellationToken cancellationToken)
+ {
+ if (BackgroundService.JobServer != null)
+ await BackgroundService.JobServer.WaitForShutdownAsync(cancellationToken);
+ }
+
+ /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
+ public void Dispose()
+ {
+ BackgroundService.JobServer?.Dispose();
+ }
+
+ /****
+ ** Tasks
+ ****/
+ /// <summary>Update the cached wiki metadata.</summary>
+ public static async Task UpdateWikiAsync()
+ {
+ WikiModList wikiCompatList = await new ModToolkit().GetWikiCompatibilityListAsync();
+ BackgroundService.WikiCache.SaveWikiData(wikiCompatList.StableVersion, wikiCompatList.BetaVersion, wikiCompatList.Mods, out _, out _);
+ }
+
+
+ /*********
+ ** Private method
+ *********/
+ /// <summary>Initialise the background service if it's not already initialised.</summary>
+ /// <exception cref="InvalidOperationException">The background service is already initialised.</exception>
+ private void TryInit()
+ {
+ if (BackgroundService.JobServer != null)
+ throw new InvalidOperationException("The scheduler service is already started.");
+
+ BackgroundService.JobServer = new BackgroundJobServer();
+ }
+ }
+}