let's try to figure out this appveyor caching problem

This commit is contained in:
13xforever 2019-01-10 23:54:33 +05:00
parent 477c9dc489
commit aac4c26178
4 changed files with 48 additions and 23 deletions

View File

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
@ -64,20 +63,25 @@ namespace AppveyorClient
DownloadUrl = $"https://ci.appveyor.com/api/buildjobs/{job.JobId}/artifacts/{rpcs3Build.FileName}",
};
ResponseCache.Set(githubStatusTargetUrl, result, CacheTime);
ApiConfig.Log.Debug($"Cached item for {githubStatusTargetUrl} for {CacheTime}");
return result;
}
catch (Exception e)
{
ApiConfig.Log.Error(e);
}
ResponseCache.TryGetValue(githubStatusTargetUrl, out ArtifactInfo o);
if (ResponseCache.TryGetValue(githubStatusTargetUrl, out ArtifactInfo o))
ApiConfig.Log.Debug($"Returned cached item for {githubStatusTargetUrl}");
return o;
}
public async Task<ArtifactInfo> GetPrDownloadAsync(int prNumber, DateTime dateTimeLimit, CancellationToken cancellationToken)
{
if (ResponseCache.TryGetValue(prNumber, out ArtifactInfo result))
{
ApiConfig.Log.Debug($"Returned cached {nameof(ArtifactInfo)} for {prNumber}");
return result;
}
try
{
@ -111,12 +115,18 @@ namespace AppveyorClient
var buildInfo = await GetBuildInfoAsync(build.BuildId, cancellationToken).ConfigureAwait(false);
var job = buildInfo?.Build.Jobs?.FirstOrDefault(j => j.Status == "success");
if (string.IsNullOrEmpty(job?.JobId))
{
ApiConfig.Log.Debug($"No successful {nameof(Job.JobId)}");
return null;
}
var artifacts = await GetJobArtifactsAsync(job.JobId, cancellationToken).ConfigureAwait(false);
var rpcs3Build = artifacts?.FirstOrDefault(a => a.Name == "rpcs3");
if (rpcs3Build == null)
{
ApiConfig.Log.Debug("No rpcs3 artifacts");
return null;
}
result = new ArtifactInfo
{
@ -124,12 +134,14 @@ namespace AppveyorClient
DownloadUrl = $"https://ci.appveyor.com/api/buildjobs/{job.JobId}/artifacts/{rpcs3Build.FileName}",
};
ResponseCache.Set(prNumber, result, CacheTime);
ApiConfig.Log.Debug($"Cached {nameof(ArtifactInfo)} for {prNumber} for {CacheTime}");
return result;
}
catch (Exception e)
{
ApiConfig.Log.Error(e);
}
ApiConfig.Log.Debug($"Failed to get {nameof(ArtifactInfo)} for {prNumber}");
return null;
}
@ -152,6 +164,7 @@ namespace AppveyorClient
await response.Content.LoadIntoBufferAsync().ConfigureAwait(false);
var result = await response.Content.ReadAsAsync<BuildInfo>(formatters, cancellationToken).ConfigureAwait(false);
ResponseCache.Set(buildUrl, result, CacheTime);
ApiConfig.Log.Debug($"Cached {nameof(BuildInfo)} for {buildUrl} for {CacheTime}");
return result;
}
catch (Exception e)
@ -165,7 +178,8 @@ namespace AppveyorClient
{
ApiConfig.Log.Error(e);
}
ResponseCache.TryGetValue(buildUrl, out BuildInfo o);
if (ResponseCache.TryGetValue(buildUrl, out BuildInfo o))
ApiConfig.Log.Debug($"Returning cached {nameof(BuildInfo)} for {buildUrl}");
return o;
}
@ -184,6 +198,7 @@ namespace AppveyorClient
await response.Content.LoadIntoBufferAsync().ConfigureAwait(false);
var result = await response.Content.ReadAsAsync<List<Artifact>>(formatters, cancellationToken).ConfigureAwait(false);
ResponseCache.Set(requestUri, result, CacheTime);
ApiConfig.Log.Debug($"Caching {nameof(Artifact)} for {jobId} for {CacheTime}");
return result;
}
catch (Exception e)
@ -197,7 +212,8 @@ namespace AppveyorClient
{
ApiConfig.Log.Error(e);
}
ResponseCache.TryGetValue(requestUri, out List<Artifact> o);
if (ResponseCache.TryGetValue(requestUri, out List<Artifact> o))
ApiConfig.Log.Debug($"Returning cached {nameof(Artifact)} for {jobId}");
return o;
}

View File

@ -25,7 +25,7 @@ namespace CompatBot.Commands
[GroupCommand]
public async Task List(CommandContext ctx, [Description("Get information for specific PR number")] int pr)
{
var prInfo = await githubClient.GetPrInfoAsync(pr.ToString(), Config.Cts.Token).ConfigureAwait(false);
var prInfo = await githubClient.GetPrInfoAsync(pr, Config.Cts.Token).ConfigureAwait(false);
if (prInfo.Number == 0)
{
await ctx.ReactWithAsync(Config.Reactions.Failure, prInfo.Message ?? "PR not found").ConfigureAwait(false);

View File

@ -26,14 +26,14 @@ namespace CompatBot.Utils.ResultFormatters
if (!justAppend)
{
if (pr == "0")
pr = "PR #???";
else
if (int.TryParse(pr, out var prNum) && prNum > 0)
{
url = "https://github.com/RPCS3/rpcs3/pull/" + pr;
prInfo = await githubClient.GetPrInfoAsync(pr, Config.Cts.Token).ConfigureAwait(false);
prInfo = await githubClient.GetPrInfoAsync(prNum, Config.Cts.Token).ConfigureAwait(false);
url = prInfo?.HtmlUrl ?? "https://github.com/RPCS3/rpcs3/pull/" + pr;
pr = $"PR #{pr} by {prInfo?.User?.Login ?? "???"}";
}
else
pr = "PR #???";
}
builder = builder ?? new DiscordEmbedBuilder {Title = pr, Url = url, Description = prInfo?.Title, Color = Config.Colors.DownloadLinks};
if (!string.IsNullOrEmpty(build?.Datetime))

View File

@ -21,7 +21,6 @@ namespace GithubClient
private readonly MediaTypeFormatterCollection formatters;
private static readonly ProductInfoHeaderValue ProductInfoHeader = new ProductInfoHeaderValue("RPCS3CompatibilityBot", "2.0");
private static readonly Dictionary<string, PrInfo> prInfoCache = new Dictionary<string, PrInfo>();
private static readonly TimeSpan PrStatusCacheTime = TimeSpan.FromMinutes(1);
private static readonly MemoryCache StatusesCache = new MemoryCache(new MemoryCacheOptions { ExpirationScanFrequency = TimeSpan.FromMinutes(1) });
@ -36,10 +35,13 @@ namespace GithubClient
formatters = new MediaTypeFormatterCollection(new[] { new JsonMediaTypeFormatter { SerializerSettings = settings } });
}
public async Task<PrInfo> GetPrInfoAsync(string pr, CancellationToken cancellationToken)
public async Task<PrInfo> GetPrInfoAsync(int pr, CancellationToken cancellationToken)
{
if (prInfoCache.TryGetValue(pr, out var result))
if (StatusesCache.TryGetValue(pr, out PrInfo result))
{
ApiConfig.Log.Debug($"Returned {nameof(PrInfo)} for {pr} from cache");
return result;
}
try
{
@ -66,25 +68,23 @@ namespace GithubClient
}
if (result == null)
{
int.TryParse(pr, out var prnum);
return new PrInfo { Number = prnum };
ApiConfig.Log.Debug($"Failed to get {nameof(PrInfo)}, returning empty result");
return new PrInfo { Number = pr };
}
lock (prInfoCache)
{
if (prInfoCache.TryGetValue(pr, out var cachedResult))
return cachedResult;
prInfoCache[pr] = result;
return result;
}
StatusesCache.Set(pr, result, PrStatusCacheTime);
ApiConfig.Log.Debug($"Cached {nameof(PrInfo)} for {pr} for {PrStatusCacheTime}");
return result;
}
public async Task<List<PrInfo>> GetOpenPrsAsync(CancellationToken cancellationToken)
{
var requestUri = "https://api.github.com/repos/RPCS3/rpcs3/pulls?state=open";
if (StatusesCache.TryGetValue(requestUri, out List<PrInfo> result))
{
ApiConfig.Log.Debug("Returned list of opened PRs from cache");
return result;
}
try
{
@ -110,14 +110,20 @@ namespace GithubClient
ApiConfig.Log.Error(e);
}
if (result != null)
{
StatusesCache.Set(requestUri, result, PrStatusCacheTime);
ApiConfig.Log.Debug($"Cached list of open PRs for {PrStatusCacheTime}");
}
return result;
}
public async Task<List<StatusInfo>> GetStatusesAsync(string statusesUrl, CancellationToken cancellationToken)
{
if (StatusesCache.TryGetValue(statusesUrl, out List<StatusInfo> result))
{
ApiConfig.Log.Debug($"Returned cached item for {statusesUrl}");
return result;
}
try
{
@ -144,7 +150,10 @@ namespace GithubClient
}
if (result != null)
{
StatusesCache.Set(statusesUrl, result, PrStatusCacheTime);
ApiConfig.Log.Debug($"Cached item for {statusesUrl} for {PrStatusCacheTime}");
}
return result;
}
}