diff --git a/Selector.CLI/Options.cs b/Selector.CLI/Options.cs index eb9bb85..0e8abbe 100644 --- a/Selector.CLI/Options.cs +++ b/Selector.CLI/Options.cs @@ -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); diff --git a/Selector/Scrobble/Mapping/ScrobbleMapping.cs b/Selector/Scrobble/Mapping/ScrobbleMapping.cs index 4981014..a4d8d87 100644 --- a/Selector/Scrobble/Mapping/ScrobbleMapping.cs +++ b/Selector/Scrobble/Mapping/ScrobbleMapping.cs @@ -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++; diff --git a/Selector/Scrobble/ScrobbleRequest.cs b/Selector/Scrobble/ScrobbleRequest.cs index 0aec88e..e022e69 100644 --- a/Selector/Scrobble/ScrobbleRequest.cs +++ b/Selector/Scrobble/ScrobbleRequest.cs @@ -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; } });