fixed cron schedule again, error handling scrobble

scrobble request and mapping
This commit is contained in:
andy 2022-03-03 17:38:49 +00:00
parent 349d62b5cc
commit 12842c240b
3 changed files with 55 additions and 39 deletions

View File

@ -124,7 +124,7 @@ namespace Selector.CLI
public const string Key = "Scrobble";
public bool Enabled { get; set; } = true;
public string FullScrobbleCron { get; set; } = "0 0 2 * * *";
public string FullScrobbleCron { get; set; } = "0 0 2 * * ?";
public TimeSpan InterJobDelay { get; set; } = TimeSpan.FromMinutes(5);
public TimeSpan InterRequestDelay { get; set; } = TimeSpan.FromMilliseconds(100);
public DateTime? From { get; set; } = DateTime.UtcNow.AddDays(-14);

View File

@ -45,29 +45,37 @@ namespace Selector
currentTask = searchClient.Item(new (QueryType, Query));
currentTask.ContinueWith(async t =>
{
netTime.Stop();
logger.LogTrace("Network request took {:n} ms", netTime.ElapsedMilliseconds);
try
{
netTime.Stop();
logger.LogTrace("Network request took {:n} ms", netTime.ElapsedMilliseconds);
if (t.IsCompletedSuccessfully)
{
HandleResponse(t);
OnSuccess();
AggregateTaskSource.SetResult();
}
else
{
if(t.Exception.InnerException is APITooManyRequestsException ex)
if (t.IsCompletedSuccessfully)
{
logger.LogError("Spotify search request too many requests, waiting for {}", ex.RetryAfter);
await Task.Delay(ex.RetryAfter.Add(TimeSpan.FromSeconds(1)));
await Execute();
HandleResponse(t);
OnSuccess();
AggregateTaskSource.SetResult();
}
else
{
logger.LogError("Spotify search request task faulted, {}", t.Exception);
AggregateTaskSource.SetException(t.Exception);
if (t.Exception.InnerException is APITooManyRequestsException ex)
{
logger.LogError("Spotify search request too many requests, waiting for {}", ex.RetryAfter);
await Task.Delay(ex.RetryAfter.Add(TimeSpan.FromSeconds(1)));
await Execute();
}
else
{
logger.LogError("Spotify search request task faulted, {}", t.Exception);
AggregateTaskSource.SetException(t.Exception);
}
}
}
catch (Exception e)
{
logger.LogError(e, "Error while mapping Last.fm {} ({}) to Spotify on attempt {}", Query, QueryType, Attempts);
Succeeded = false;
}
});
Attempts++;

View File

@ -56,39 +56,47 @@ namespace Selector
currentTask = userClient.GetRecentScrobbles(username, pagenumber: pageNumber, count: pageSize, from: from, to: to);
currentTask.ContinueWith(async t =>
{
netTime.Stop();
logger.LogTrace("Network request took {:n} ms", netTime.ElapsedMilliseconds);
if (t.IsCompletedSuccessfully)
try
{
var result = t.Result;
Succeeded = result.Success;
netTime.Stop();
logger.LogTrace("Network request took {:n} ms", netTime.ElapsedMilliseconds);
if (Succeeded)
if (t.IsCompletedSuccessfully)
{
Scrobbles = result.Content.ToArray();
TotalPages = result.TotalPages;
OnSuccess();
AggregateTaskSource.SetResult();
}
else
{
if(Attempts < MaxAttempts)
var result = t.Result;
Succeeded = result.Success;
if (Succeeded)
{
logger.LogDebug("Request failed for {}, #{} by {}: {}, retrying ({} of {})", username, pageNumber, pageSize, result.Status, Attempts + 1, MaxAttempts);
await Execute();
Scrobbles = result.Content.ToArray();
TotalPages = result.TotalPages;
OnSuccess();
AggregateTaskSource.SetResult();
}
else
{
logger.LogDebug("Request failed for {}, #{} by {}: {}, max retries exceeded {}, not retrying", username, pageNumber, pageSize, result.Status, MaxAttempts);
AggregateTaskSource.SetCanceled();
if (Attempts < MaxAttempts)
{
logger.LogDebug("Request failed for {}, #{} by {}: {}, retrying ({} of {})", username, pageNumber, pageSize, result.Status, Attempts + 1, MaxAttempts);
await Execute();
}
else
{
logger.LogDebug("Request failed for {}, #{} by {}: {}, max retries exceeded {}, not retrying", username, pageNumber, pageSize, result.Status, MaxAttempts);
AggregateTaskSource.SetCanceled();
}
}
}
else
{
logger.LogError("Scrobble request task faulted, {}", t.Exception);
AggregateTaskSource.SetException(t.Exception);
}
}
else
catch(Exception e)
{
logger.LogError("Scrobble request task faulted, {}", t.Exception);
AggregateTaskSource.SetException(t.Exception);
logger.LogError(e, "Error while making scrobble request #{} for {} by {} from {} to {} on attempt {}", pageNumber, username, pageSize, from, to, Attempts);
Succeeded = false;
}
});