Add configurable cache duration

This commit is contained in:
pcjones
2025-01-13 19:00:42 +01:00
parent e117826c6a
commit b6390c15a1
6 changed files with 62 additions and 36 deletions

View File

@@ -14,5 +14,11 @@
/// The User-Agent string used in HTTP requests.
/// </summary>
public string UserAgent { get; set; }
/// <summary>
/// The duration in minutes to cache the indexer requests.
/// </summary>
public int IndexerRequestsCacheDurationInMinutes { get; set; } = 12;
}
}

View File

@@ -22,6 +22,11 @@ internal class Program
builder.Services.AddSerilog();
// Log all configuration values
LogConfigurationValues(configuration);
// Add services to the container.
builder.Services.AddHttpClient("HttpClient").ConfigurePrimaryHttpMessageHandler(() =>
{
@@ -118,4 +123,15 @@ internal class Program
//.Enrich.With(new ApiKeyMaskingEnricher("appsettings.json")) // TODO - Not working currently
.CreateLogger();
}
private static void LogConfigurationValues(IConfiguration configuration)
{
Log.Information("Logging all configuration values at startup:");
foreach (var kvp in configuration.AsEnumerable())
{
Log.Information("{Key}: {Value}", kvp.Key, kvp.Value);
}
}
}

View File

@@ -81,7 +81,7 @@ namespace UmlautAdaptarr.Services
if (responseMessage.IsSuccessStatusCode)
{
_cache.Set(targetUri, responseMessage, TimeSpan.FromMinutes(12));
_cache.Set(targetUri, responseMessage, TimeSpan.FromMinutes(_options.IndexerRequestsCacheDurationInMinutes));
}
return responseMessage;

View File

@@ -68,7 +68,7 @@ public static class ServicesExtensions
Console.WriteLine(($"Property {failure.PropertyName } failed validation. Error was: {failure.ErrorMessage}"));
}
throw new Exception("Please fix cour environment variables and then Start UmlautAdaptarr again");
throw new Exception("Please fix your environment variables and then Start UmlautAdaptarr again");
}
var instanceState = (bool)(typeof(TOptions).GetProperty("Enabled")?.GetValue(option, null) ?? false);

View File

@@ -6,7 +6,8 @@ namespace UmlautAdaptarr.Validator;
public class GlobalInstanceOptionsValidator : AbstractValidator<GlobalInstanceOptions>
{
private readonly static HttpClient httpClient = new() {
private readonly static HttpClient httpClient = new()
{
Timeout = TimeSpan.FromSeconds(3)
};
@@ -35,38 +36,37 @@ public class GlobalInstanceOptionsValidator : AbstractValidator<GlobalInstanceOp
&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
}
private static async Task<bool> BeReachable(GlobalInstanceOptions opts, CancellationToken cancellationToken)
{
var endTime = DateTime.Now.AddMinutes(3);
var reachable = false;
var url = $"{opts.Host}/api?apikey={opts.ApiKey}";
private static async Task<bool> BeReachable(GlobalInstanceOptions opts, CancellationToken cancellationToken)
{
var endTime = DateTime.Now.AddMinutes(3);
var reachable = false;
var url = $"{opts.Host}/api?apikey={opts.ApiKey}";
while (DateTime.Now < endTime)
{
try
{
using var response = await httpClient.GetAsync(url, cancellationToken);
if (response.IsSuccessStatusCode)
{
reachable = true;
break;
}
else
{
Console.WriteLine($"Reachable check got unexpected status code {response.StatusCode}.");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
while (DateTime.Now < endTime)
{
try
{
using var response = await httpClient.GetAsync(url, cancellationToken);
if (response.IsSuccessStatusCode)
{
reachable = true;
break;
}
else
{
Console.WriteLine($"Reachable check got unexpected status code {response.StatusCode}.");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
// Wait for 15 seconds for next try
Console.WriteLine($"The URL \"{opts.Host}/api?apikey=[REDACTED]\" is not reachable. Next attempt in 15 seconds...");
Thread.Sleep(15000);
}
return reachable;
}
// Wait for 15 seconds for next try
Console.WriteLine($"The URL \"{opts.Host}/api?apikey=[REDACTED]\" is not reachable. Next attempt in 15 seconds...");
Thread.Sleep(15000);
}
return reachable;
}
}

View File

@@ -23,7 +23,6 @@ services:
- LIDARR__ENABLED=false
- LIDARR__HOST=http://localhost:8686
- LIDARR__APIKEY=APIKEY
#- IpLeakTest__Enabled=false # uncomment and set to true to enable IP leak test
### example for multiple instances of same type
#- SONARR__0__NAME=NAME 1 (optional)
#- SONARR__0__ENABLED=false
@@ -33,3 +32,8 @@ services:
#- SONARR__1__ENABLED=false
#- SONARR__1__HOST=http://localhost:8989
#- SONARR__1__APIKEY=APIKEY
### Advanced options (with default values))
#- IpLeakTest__Enabled=false
#- SETTINGS__IndexerRequestsCacheDurationInMinutes=12