async Task ProcessFile(DirectoryBasedTransaction transaction, string messageId) { try { string[] message = File.ReadAllLines(transaction.FileToProcess); var bodyPath = message.First(); var json = string.Join("", message.Skip(1)); Dictionary <string, string> headers = HeaderSerializer.DeSerialize(json); string ttbrString; if (headers.TryGetValue(Headers.TimeToBeReceived, out ttbrString)) { var ttbr = TimeSpan.Parse(ttbrString); //file.move preserves create time var sentTime = File.GetCreationTimeUtc(transaction.FileToProcess); if (sentTime + ttbr < DateTime.UtcNow) { transaction.Commit(); return; } } var tokenSource = new CancellationTokenSource(); using (var bodyStream = new FileStream(bodyPath, FileMode.Open)) { var context = new ContextBag(); context.Set(transaction); var pushContext = new PushContext(messageId, headers, bodyStream, transaction, tokenSource, context); await pipeline(pushContext) .ConfigureAwait(false); } if (tokenSource.IsCancellationRequested) { transaction.Rollback(); return; } transaction.Commit(); } catch (Exception) { transaction.Rollback(); } }
async Task ProcessFileWithTransaction(string filePath, string messageId) { using (var transaction = new DirectoryBasedTransaction(path)) { transaction.BeginTransaction(filePath); var message = File.ReadAllLines(transaction.FileToProcess); var bodyPath = message.First(); var json = string.Join("", message.Skip(1)); var headers = HeaderSerializer.DeSerialize(json); string ttbrString; if (headers.TryGetValue(Headers.TimeToBeReceived, out ttbrString)) { var ttbr = TimeSpan.Parse(ttbrString); // file.move preserves create time var sentTime = File.GetCreationTimeUtc(transaction.FileToProcess); if (sentTime + ttbr < DateTime.UtcNow) { return; } } var body = File.ReadAllBytes(bodyPath); var transportTransaction = new TransportTransaction(); transportTransaction.Set(transaction); var shouldCommit = await HandleMessageWithRetries(messageId, headers, body, transportTransaction, 1) .ConfigureAwait(false); if (shouldCommit) { transaction.Commit(); } } }