async Task StartProcessing(CancellationToken token) { while (!token.IsCancellationRequested) { while (toProcess.TryPeek(out var request)) { try { log.Info($"Processing request with ID {request.RequestId} and estimated processing time {request.EstimatedProcessingTime}."); request.StartedAt = DateTime.UtcNow; #region failed-scenario // emulate failure if (DateTime.UtcNow.Ticks % 2 == 0) { throw new Exception("Some exception during processing."); } #endregion // process var estimatedProcessingTime = TimeSpan.Parse(request.EstimatedProcessingTime); await Task.Delay(estimatedProcessingTime, token) .ConfigureAwait(false); log.Info($"Request with ID {request.RequestId} processed."); request.Status = Status.Finished.ToString(); request.FinishedAt = DateTime.UtcNow; await table.ExecuteAsync(TableOperation.Merge(request), token) .ConfigureAwait(false); toProcess.TryDequeue(out request); var processingFinished = new LongProcessingFinished { Id = request.RequestId }; await endpoint.Publish(processingFinished) .ConfigureAwait(false); } catch (Exception ex) { log.Info($"Request with ID {request.RequestId} threw an exception."); request.Status = Status.Failed.ToString(); await table.ExecuteAsync(TableOperation.Merge(request), token) .ConfigureAwait(false); toProcess.TryDequeue(out request); var processingFailed = new LongProcessingFailed { Id = request.RequestId, Reason = ex.Message }; await endpoint.Publish(processingFailed) .ConfigureAwait(false); } } } }
public Task Handle(LongProcessingFailed message, IMessageHandlerContext context) { MarkAsComplete(); return(Task.FromResult(0)); }