Skip to content

Commit

Permalink
init caches using queue, gate using filters
Browse files Browse the repository at this point in the history
  • Loading branch information
goaaats committed Apr 15, 2024
1 parent f6dd251 commit 02a842f
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 12 deletions.
1 change: 1 addition & 0 deletions XLWebServices/Controllers/Dalamud/AssetController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace XLWebServices.Controllers;

[ApiController]
[Route("Dalamud/Asset/[action]")]
[TypeFilter(typeof(AssetCacheService.AssetCacheAvailabilityFilter), IsReusable = true)]
public class AssetController : ControllerBase
{
private readonly FallibleService<AssetCacheService> assetCache;
Expand Down
1 change: 1 addition & 0 deletions XLWebServices/Controllers/Dalamud/ReleaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace XLWebServices.Controllers;
[ApiController]
[EnableCors("GithubAccess")]
[Route("Dalamud/Release/[action]")]
[TypeFilter(typeof(DalamudReleaseDataService.DalamudReleaseDataAvailabilityFilter), IsReusable = true)]
public class ReleaseController : ControllerBase
{
private readonly FallibleService<DalamudReleaseDataService> releaseCache;
Expand Down
1 change: 1 addition & 0 deletions XLWebServices/Controllers/GitHubProxyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace XLWebServices.Controllers;

[ApiController]
[Route("Proxy/[action]")]
[TypeFilter(typeof(LauncherReleaseDataService.LauncherReleaseDataAvailabilityFilter), IsReusable = true)]
public class GitHubProxyController: ControllerBase
{
private readonly ILogger<GitHubProxyController> _logger;
Expand Down
1 change: 1 addition & 0 deletions XLWebServices/Controllers/LauncherController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace XLWebServices.Controllers;

[ApiController]
[Route("Launcher/[action]")]
[TypeFilter(typeof(LauncherReleaseDataService.LauncherReleaseDataAvailabilityFilter), IsReusable = true)]
public class LauncherController : ControllerBase
{
private readonly ILogger<GitHubProxyController> _logger;
Expand Down
3 changes: 3 additions & 0 deletions XLWebServices/Controllers/PlogonController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ public class StagedPluginInfo
[HttpPost]
public async Task<IActionResult> CommitStagedPlugins()
{
if (_data.HasFailed || !_data.Get()!.HasInitialData)
return StatusCode(500, "Precondition failed");

if (!CheckAuthHeader())
return Unauthorized();

Expand Down
3 changes: 3 additions & 0 deletions XLWebServices/Controllers/PluginController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text.Json;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.EntityFrameworkCore;
using Prometheus;
using XLWebServices.Data;
Expand All @@ -13,6 +14,8 @@ namespace XLWebServices.Controllers;
[ApiController]
[EnableCors("GithubAccess")]
[Route("[controller]/[action]")]
[TypeFilter(typeof(PluginDataService.PluginDataAvailabilityFilter), IsReusable = true)]
[TypeFilter(typeof(DalamudReleaseDataService.DalamudReleaseDataAvailabilityFilter), IsReusable = true)]
public class PluginController : ControllerBase
{
private readonly ILogger<PluginController> logger;
Expand Down
37 changes: 28 additions & 9 deletions XLWebServices/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,36 @@
}

app.Services.GetRequiredService<GitHubService>();
var queue = app.Services.GetRequiredService<IBackgroundTaskQueue>();

var acs = app.Services.GetRequiredService<FallibleService<AssetCacheService>>();
await acs.RunFallibleAsync(s => s.ClearCache());

var drs = app.Services.GetRequiredService<FallibleService<DalamudReleaseDataService>>();
await drs.RunFallibleAsync(s => s.ClearCache());
_ = Task.Run(async () =>
{
await queue.QueueBackgroundWorkItemAsync(async (_, _) =>
{
var rds = app.Services.GetRequiredService<FallibleService<LauncherReleaseDataService>>();
await rds.RunFallibleAsync(s => s.ClearCache());
});
await queue.QueueBackgroundWorkItemAsync(async (_, _) =>
{
var acs = app.Services.GetRequiredService<FallibleService<AssetCacheService>>();
await acs.RunFallibleAsync(s => s.ClearCache());
});
var pds = app.Services.GetRequiredService<FallibleService<PluginDataService>>();
await pds.RunFallibleAsync(s => s.ClearCache());
await queue.QueueBackgroundWorkItemAsync(async (_, _) =>
{
var drs = app.Services.GetRequiredService<FallibleService<DalamudReleaseDataService>>();
await drs.RunFallibleAsync(s => s.ClearCache());
});
var rds = app.Services.GetRequiredService<FallibleService<LauncherReleaseDataService>>();
await rds.RunFallibleAsync(s => s.ClearCache());
await queue.QueueBackgroundWorkItemAsync(async (_, _) =>
{
var pds = app.Services.GetRequiredService<FallibleService<PluginDataService>>();
await pds.RunFallibleAsync(s => s.ClearCache());
await pds.RunFallibleAsync(s => s.PostProcessD17Masters());
});
logger.LogInformation("Queued cache clear jobs");
});

app.Run();
26 changes: 23 additions & 3 deletions XLWebServices/Services/AssetCacheService.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
using System.Net.Http.Headers;
using Newtonsoft.Json;
using Octokit;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace XLWebServices.Services;

public class AssetCacheService
{
public class AssetCacheAvailabilityFilter : IAsyncActionFilter
{
private readonly FallibleService<AssetCacheService> _assetCache;

public AssetCacheAvailabilityFilter(FallibleService<AssetCacheService> assetCache)
{
_assetCache = assetCache;
}

public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
if (_assetCache.HasFailed || _assetCache.Get()?.Assets == null)
{
context.Result = new StatusCodeResult(503);
return;
}

await next();
}
}

private readonly IConfiguration config;
private readonly FileCacheService cache;
private readonly GitHubService github;
Expand Down
23 changes: 23 additions & 0 deletions XLWebServices/Services/DalamudReleaseDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Octokit;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
Expand All @@ -10,6 +12,27 @@ namespace XLWebServices.Services;

public class DalamudReleaseDataService
{
public class DalamudReleaseDataAvailabilityFilter : IAsyncActionFilter
{
private readonly FallibleService<DalamudReleaseDataService> _dalamudReleaseData;

public DalamudReleaseDataAvailabilityFilter(FallibleService<DalamudReleaseDataService> dalamudReleaseData)
{
_dalamudReleaseData = dalamudReleaseData;
}

public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
if (_dalamudReleaseData.HasFailed || _dalamudReleaseData.Get()?.DalamudVersions == null)
{
context.Result = new StatusCodeResult(503);
return;
}

await next();
}
}

private readonly IConfiguration config;
private readonly FileCacheService cache;
private readonly ILogger<DalamudReleaseDataService> logger;
Expand Down
23 changes: 23 additions & 0 deletions XLWebServices/Services/LauncherReleaseDataService.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
using System.Diagnostics;
using System.Text.RegularExpressions;
using LibGit2Sharp;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Octokit;
using Repository = LibGit2Sharp.Repository;

namespace XLWebServices.Services;

public class LauncherReleaseDataService
{
public class LauncherReleaseDataAvailabilityFilter : IAsyncActionFilter
{
private readonly FallibleService<LauncherReleaseDataService> _launcherReleaseData;

public LauncherReleaseDataAvailabilityFilter(FallibleService<LauncherReleaseDataService> launcherReleaseData)
{
_launcherReleaseData = launcherReleaseData;
}

public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
if (_launcherReleaseData.HasFailed || this._launcherReleaseData.Get()?.CachedReleasesList == null)
{
context.Result = new StatusCodeResult(503);
return;
}

await next();
}
}

private readonly ILogger<LauncherReleaseDataService> _logger;
private readonly GitHubService _github;
private readonly IConfiguration _configuration;
Expand Down
27 changes: 27 additions & 0 deletions XLWebServices/Services/PluginData/PluginDataService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Diagnostics;
using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Octokit;
using Tomlyn;
using XLWebServices.Data;
Expand All @@ -9,6 +11,27 @@ namespace XLWebServices.Services.PluginData;

public class PluginDataService
{
public class PluginDataAvailabilityFilter : IAsyncActionFilter
{
private readonly FallibleService<PluginDataService> _pluginData;

public PluginDataAvailabilityFilter(FallibleService<PluginDataService> pluginData)
{
_pluginData = pluginData;
}

public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
if (_pluginData.HasFailed || !_pluginData.Get()!.HasInitialData)
{
context.Result = new StatusCodeResult(503);
return;
}

await next();
}
}

private readonly ILogger<PluginDataService> _logger;
private readonly GitHubService _github;
private readonly IConfiguration _configuration;
Expand All @@ -28,6 +51,8 @@ public class PluginDataService

public DateTime LastUpdate { get; private set; }

public bool HasInitialData { get; private set; } = false;

public PluginDataService(
ILogger<PluginDataService> logger,
GitHubService github,
Expand Down Expand Up @@ -116,6 +141,8 @@ public async Task PostProcessD17Masters()

plugin.Changelog = version?.Changelog;
}

HasInitialData = true;
}

private async Task<(Dictionary<string, List<PluginManifest>> Manifests, string Sha)> ClearCacheD17(List<PluginManifest> pluginMaster)
Expand Down

0 comments on commit 02a842f

Please sign in to comment.