Search code examples
c#polly

Polly inside task times out


I'm calling an endpoint which I want to retry if it returns 404 or fail immediatly on other exceptions.

Works fine for shorter timespans, but when I run with an exponential backoff I see "Scheduling attempt 10/11 in at [date]" but no subsequent calls are made.

I want to run this in a task because I want to dequeue it from the main thread and process it in the background. Could the problem be related to the task being disposed?

var settingsPage = _contentQueries.GetComponent<SettingsPage>();
var retryCount = 11;
var token = _authorizationService.GetBearerToken();

Task.Factory.StartNew(currentContext => {
    HttpContext.Current = (HttpContext) currentContext;

    var captureResult = Policy.HandleResult<HttpResponse>(r => r.Status == (int)HttpStatusCode.NotFound).WaitAndRetry(retryCount, retryAttempt =>
    {
        var nextAttemptInSeconds = Math.Pow(3, retryAttempt);
        _logger.Log($"Scheduling retry attempt {retryAttempt}/{retryCount} at {DateTime.Now.AddSeconds(nextAttemptInSeconds)}", activity, Level.Debug);
        return TimeSpan.FromSeconds(nextAttemptInSeconds);
    }).ExecuteAndCapture(() =>
    {
        var result = MakeWebClientGetRequest();

        if (RemoteErrorOccurred(result))
        {
            throw new Exception(result.Response);
        }

        return result;
    });


    if (captureResult.Outcome == OutcomeType.Successful)
    {
        _logger.Log("Process successful", activity, Level.Debug);
    }
    else
    {
        if (captureResult.FinalException != null)
        {
            _logger.Log($"Failed to process: {captureResult.FinalException}", activity, Level.Debug);
        }
        else
        {
            _logger.Log($"Failed to process after {retryCount} attempts", activity, Level.Debug);
        }
    }

    _databaseFactory.Dispose();
}, HttpContext.Current);
private static bool RemoteErrorOccurred(HttpResponse result)
{
    return result.Exception != null && result.Status != (int) HttpStatusCode.NotFound;
}

Solution

  • I ended up using Hangfire's BackgroundJob.Enqueue in favor of Task.Factory.StartNew. The app pool recycle prevents such long running tasks as in my question.