波莉内部任务超时



>我正在调用一个端点,如果它返回 404 或在其他异常时立即失败,我想重试。

适用于较短的时间跨度,但是当我以指数退避运行时,我看到"在 [date] 安排尝试 10/11",但没有进行后续调用。

我想在任务中运行它,因为我想从主线程中取消排队并在后台处理它。问题是否与正在处理的任务有关?

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;
}

我最终使用了Hangfire的BackgroundJob.Enqueue来支持Task.Factory.StartNew。应用程序池回收可防止像我的问题中那样长时间运行的任务。

相关内容

  • 没有找到相关文章

最新更新