async Task ProcessFileAndComplete(IAcceptanceTestTransportTransaction transaction, string filePath, string messageId) { try { await ProcessFile(transaction, messageId) .ConfigureAwait(false); } finally { if (log.IsDebugEnabled) { log.Debug($"Completing processing for {filePath}({transaction.FileToProcess})."); } try { var wasCommitted = transaction.Complete(); if (wasCommitted) { File.Delete(Path.Combine(bodyDir, messageId + BodyFileSuffix)); } } catch (Exception ex) { log.Debug($"Failure while trying to complete receive transaction for {filePath}({transaction.FileToProcess})" + filePath, ex); } concurrencyLimiter.Release(); } }
async Task ProcessFile(IAcceptanceTestTransportTransaction transaction, string messageId) { var message = await AsyncFile.ReadText(transaction.FileToProcess) .ConfigureAwait(false); var bodyPath = Path.Combine(bodyDir, $"{messageId}{BodyFileSuffix}"); var headers = HeaderSerializer.Deserialize(message); if (headers.TryGetValue(AcceptanceTestTransportHeaders.TimeToBeReceived, out var ttbrString)) { headers.Remove(AcceptanceTestTransportHeaders.TimeToBeReceived); var ttbr = TimeSpan.Parse(ttbrString); //file.move preserves create time var sentTime = File.GetCreationTimeUtc(transaction.FileToProcess); var utcNow = DateTime.UtcNow; if (sentTime + ttbr < utcNow) { await transaction.Commit() .ConfigureAwait(false); log.InfoFormat("Dropping message '{0}' as the specified TimeToBeReceived of '{1}' expired since sending the message at '{2:O}'. Current UTC time is '{3:O}'", messageId, ttbrString, sentTime, utcNow); return; } } var tokenSource = new CancellationTokenSource(); var body = await AsyncFile.ReadBytes(bodyPath, cancellationToken) .ConfigureAwait(false); var transportTransaction = new TransportTransaction(); if (transactionMode == TransportTransactionMode.SendsAtomicWithReceive) { transportTransaction.Set(transaction); } var messageContext = new MessageContext(messageId, headers, body, transportTransaction, tokenSource, new ContextBag()); try { await onMessage(messageContext) .ConfigureAwait(false); } catch (Exception exception) { transaction.ClearPendingOutgoingOperations(); var processingFailures = retryCounts.AddOrUpdate(messageId, id => 1, (id, currentCount) => currentCount + 1); headers = HeaderSerializer.Deserialize(message); headers.Remove(AcceptanceTestTransportHeaders.TimeToBeReceived); var errorContext = new ErrorContext(exception, headers, messageId, body, transportTransaction, processingFailures); ErrorHandleResult actionToTake; try { actionToTake = await onError(errorContext) .ConfigureAwait(false); } catch (Exception ex) { criticalError.Raise($"Failed to execute recoverability policy for message with native ID: `{messageContext.MessageId}`", ex); actionToTake = ErrorHandleResult.RetryRequired; } if (actionToTake == ErrorHandleResult.RetryRequired) { transaction.Rollback(); return; } } if (tokenSource.IsCancellationRequested) { transaction.Rollback(); return; } await transaction.Commit() .ConfigureAwait(false); }