Skip to content

Commit 1200684

Browse files
committed
give pinned versions ctor its own logger instance instead of borrowing the version cache's
1 parent 0a56972 commit 1200684

2 files changed

Lines changed: 22 additions & 20 deletions

File tree

src/Server/Services/GitLab/PinnedVersions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ namespace Ryujinx.Systems.Update.Server.Services.GitLab;
55

66
public class PinnedVersions : SafeDictionary<SupportedPlatform, SafeDictionary<SupportedArchitecture, string>>
77
{
8-
public PinnedVersions(VersionCache vcache, IConfigurationSection configSection)
8+
public PinnedVersions(ILogger<PinnedVersions> logger, IConfigurationSection configSection)
99
{
1010
foreach (var subSection in configSection.GetChildren())
1111
{
1212
if (!subSection.Key.TryParseAsSupportedPlatform(out var platform))
1313
{
14-
vcache.Logger.LogWarning("Unknown platform '{key}'; skipping", subSection.Key);
14+
logger.LogWarning("Unknown platform '{key}'; skipping", subSection.Key);
1515
continue;
1616
}
1717

@@ -20,13 +20,13 @@ public PinnedVersions(VersionCache vcache, IConfigurationSection configSection)
2020
{
2121
if (!archVersionPair.Key.TryParseAsSupportedArchitecture(out SupportedArchitecture arch))
2222
{
23-
vcache.Logger.LogWarning("Unknown platform '{key}' in '{subsectionName}'; skipping", archVersionPair.Key, subSection.Key);
23+
logger.LogWarning("Unknown platform '{key}' in '{subsectionName}'; skipping", archVersionPair.Key, subSection.Key);
2424
continue;
2525
}
2626

2727
if (archVersionPair.Value == null)
2828
{
29-
vcache.Logger.LogWarning("Version pair value for '{key}' in '{subsectionName}' was null; skipping", archVersionPair.Key, subSection.Key);
29+
logger.LogWarning("Version pair value for '{key}' in '{subsectionName}' was null; skipping", archVersionPair.Key, subSection.Key);
3030
continue;
3131
}
3232

src/Server/Services/GitLab/VersionCache.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Diagnostics;
2-
using System.Diagnostics.CodeAnalysis;
32
using Gommon;
43
using NGitLab;
54
using NGitLab.Models;
@@ -10,7 +9,7 @@ namespace Ryujinx.Systems.Update.Server.Services.GitLab;
109
public class VersionCache : SafeDictionary<string, VersionCacheEntry>
1110
{
1211
private readonly GitLabService _gl;
13-
public ILogger<VersionCache> Logger { get; }
12+
private readonly ILogger<VersionCache> _logger;
1413
private readonly PeriodicTimer? _refreshTimer;
1514

1615
private Project? _cachedProject;
@@ -23,7 +22,7 @@ public class VersionCache : SafeDictionary<string, VersionCacheEntry>
2322

2423
private string? _latestTag;
2524

26-
public PinnedVersions PinnedVersions { get; private set; }
25+
private PinnedVersions PinnedVersions { get; set; } = null!; //late-init, see Init method
2726

2827
// ReSharper disable once ReplaceWithFieldKeyword
2928

@@ -36,7 +35,7 @@ public class VersionCache : SafeDictionary<string, VersionCacheEntry>
3635
public VersionCache(IConfiguration config, GitLabService gitlabService, ILogger<VersionCache> logger)
3736
{
3837
_gl = gitlabService;
39-
Logger = logger;
38+
_logger = logger;
4039

4140
_gitlabEndpoint = config["GitLab:Endpoint"]!;
4241

@@ -75,13 +74,13 @@ public void Init(ProjectId projectId, PinnedVersions pinnedVersions) => Executor
7574
}
7675
catch (GitLabException e)
7776
{
78-
Logger.LogError(
77+
_logger.LogError(
7978
"Encountered error when getting the project ({project}) for the version cache. Aborting. Error: {errorMessage}",
8079
projectId.ValueAsString(), e.ErrorMessage);
8180
return;
8281
}
8382

84-
Logger.LogInformation("Initializing version cache for {project}", ProjectName);
83+
_logger.LogInformation("Initializing version cache for {project}", ProjectName);
8584

8685
PinnedVersions = pinnedVersions;
8786

@@ -93,14 +92,15 @@ public void Init(ProjectId projectId, PinnedVersions pinnedVersions) => Executor
9392
? $"using the {Constants.FullRouteName_Api_Admin_RefreshCache} endpoint or restarting the server."
9493
: "restarting the server. Set an admin access token in appsettings.json to enable an endpoint to do this.";
9594

96-
Logger.LogInformation(
95+
_logger.LogInformation(
9796
"Periodic version cache refreshing is disabled for {project}. It can be refreshed by {means}",
9897
ProjectName, howToRefresh);
9998
return;
10099
}
101100

102-
Logger.LogInformation("Refreshing version cache for {project} every {timePeriod} minutes.",
101+
_logger.LogInformation("Refreshing version cache for {project} every {timePeriod} minutes.",
103102
ProjectName, _refreshTimer.Period.TotalMinutes);
103+
104104
while (await _refreshTimer.WaitForNextTickAsync())
105105
{
106106
await RefreshAsync();
@@ -129,21 +129,21 @@ public void Init(ProjectId projectId, PinnedVersions pinnedVersions) => Executor
129129

130130
public async Task RefreshAsync()
131131
{
132-
Logger.LogInformation("Reloading version cache for {project}", ProjectName);
132+
_logger.LogInformation("Reloading version cache for {project}", ProjectName);
133133

134134
_latestTag = (await _gl.GetLatestReleaseAsync(ProjectId))?.TagName;
135135

136136
if (_latestTag is null)
137137
{
138-
Logger.LogWarning("Latest version for {project} was a 404, aborting.", ProjectName);
138+
_logger.LogWarning("Latest version for {project} was a 404, aborting.", ProjectName);
139139
return;
140140
}
141141

142142
var sw = Stopwatch.StartNew();
143143

144144
var releases = await _gl.PageReleases(ProjectId)
145145
.GetAllAsync(onNonSuccess:
146-
code => Logger.LogError(
146+
code => _logger.LogError(
147147
"One of the pagination requests to get all releases returned a non-success status code: {code}",
148148
Enum.GetName(code) ?? $"{(int)code}")
149149
);
@@ -197,14 +197,14 @@ public async Task RefreshAsync()
197197

198198
if (Count > 0)
199199
{
200-
Logger.LogInformation("Clearing {entryCount} version cache entries for {project}", Count,
200+
_logger.LogInformation("Clearing {entryCount} version cache entries for {project}", Count,
201201
ProjectName);
202202
Clear();
203203
}
204204

205205
foreach (var (tag, entry) in tempCacheEntries)
206206
{
207-
Logger.LogTrace("Adding version cache entry {tag} for {project}", tag, ProjectName);
207+
_logger.LogTrace("Adding version cache entry {tag} for {project}", tag, ProjectName);
208208

209209
this[tag] = entry;
210210
}
@@ -215,7 +215,7 @@ public async Task RefreshAsync()
215215

216216
_semaphore.Release();
217217

218-
Logger.LogInformation("Loaded {entryCount} version cache entries for {project}; took {time}ms.", Count,
218+
_logger.LogInformation("Loaded {entryCount} version cache entries for {project}; took {time}ms.", Count,
219219
ProjectName, sw.ElapsedMilliseconds);
220220
}
221221

@@ -232,17 +232,19 @@ public static void InitializeVersionCaches(WebApplication app)
232232

233233
var vpSection = app.Configuration.GetSection("VersionPinning");
234234

235+
var pvLogger = app.Services.Get<ILoggerFactory>().CreateLogger<PinnedVersions>();
236+
235237
var stableCache = app.Services.GetRequiredKeyedService<VersionCache>("stableCache");
236238
stableCache.Init(stableSource,
237-
new PinnedVersions(stableCache, vpSection.GetSection("Stable")));
239+
new PinnedVersions(pvLogger, vpSection.GetSection("Stable")));
238240

239241
var canarySource = versionCacheSection.GetValue<string>("Canary");
240242

241243
if (canarySource != null)
242244
{
243245
var canaryCache = app.Services.GetRequiredKeyedService<VersionCache>("canaryCache");
244246
canaryCache.Init(canarySource,
245-
new PinnedVersions(canaryCache, vpSection.GetSection("Canary")));
247+
new PinnedVersions(pvLogger, vpSection.GetSection("Canary")));
246248
}
247249
}
248250
}

0 commit comments

Comments
 (0)