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.Mods; using StardewModdingAPI.Web.Framework.Caching.Wiki; namespace StardewModdingAPI.Web { /// A hosted service which runs background data updates. /// Task methods need to be static, since otherwise Hangfire will try to serialise the entire instance. internal class BackgroundService : IHostedService, IDisposable { /********* ** Fields *********/ /// The background task server. private static BackgroundJobServer JobServer; /// The cache in which to store wiki metadata. private static IWikiCacheRepository WikiCache; /// The cache in which to store mod data. private static IModCacheRepository ModCache; /********* ** Public methods *********/ /**** ** Hosted service ****/ /// Construct an instance. /// The cache in which to store wiki metadata. /// The cache in which to store mod data. public BackgroundService(IWikiCacheRepository wikiCache, IModCacheRepository modCache) { BackgroundService.WikiCache = wikiCache; BackgroundService.ModCache = modCache; } /// Start the service. /// Tracks whether the start process has been aborted. public Task StartAsync(CancellationToken cancellationToken) { this.TryInit(); // set startup tasks BackgroundJob.Enqueue(() => BackgroundService.UpdateWikiAsync()); BackgroundJob.Enqueue(() => BackgroundService.RemoveStaleModsAsync()); // set recurring tasks RecurringJob.AddOrUpdate(() => BackgroundService.UpdateWikiAsync(), "*/10 * * * *"); // every 10 minutes RecurringJob.AddOrUpdate(() => BackgroundService.RemoveStaleModsAsync(), "0 * * * *"); // hourly return Task.CompletedTask; } /// Triggered when the application host is performing a graceful shutdown. /// Tracks whether the shutdown process should no longer be graceful. public async Task StopAsync(CancellationToken cancellationToken) { if (BackgroundService.JobServer != null) await BackgroundService.JobServer.WaitForShutdownAsync(cancellationToken); } /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. public void Dispose() { BackgroundService.JobServer?.Dispose(); } /**** ** Tasks ****/ /// Update the cached wiki metadata. public static async Task UpdateWikiAsync() { WikiModList wikiCompatList = await new ModToolkit().GetWikiCompatibilityListAsync(); BackgroundService.WikiCache.SaveWikiData(wikiCompatList.StableVersion, wikiCompatList.BetaVersion, wikiCompatList.Mods, out _, out _); } /// Remove mods which haven't been requested in over 48 hours. public static Task RemoveStaleModsAsync() { BackgroundService.ModCache.RemoveStaleMods(TimeSpan.FromHours(48)); return Task.CompletedTask; } /********* ** Private method *********/ /// Initialise the background service if it's not already initialised. /// The background service is already initialised. private void TryInit() { if (BackgroundService.JobServer != null) throw new InvalidOperationException("The scheduler service is already started."); BackgroundService.JobServer = new BackgroundJobServer(); } } }