private void OnTransactionComplete(RedisTransaction transaction, bool success, IList <Message> messages, TaskCompletionSource <object> tcs) { if (success) { _trace.TraceVerbose("Transaction completed successfully"); tcs.TrySetResult(null); } else { _trace.TraceVerbose("Transaction failed. Retrying..."); _trace.TraceVerbose("Transaction disposed"); // Dispose the transaction transaction.Dispose(); SendImpl(messages, tcs); } }
private void SendImpl(IList <Message> messages, TaskCompletionSource <object> tcs) { go: Task <long?> task = _connection.Strings.GetInt64(_db, _key); RedisTransaction transaction = null; // Dispose the transaction after everything runs tcs.Task.Finally(state => { if (transaction != null) { _trace.TraceVerbose("Transaction disposed"); transaction.Dispose(); } }, null); if (task.IsCompleted) { try { task.Wait(); _trace.TraceVerbose("CreateTransaction({0})", task.Result); transaction = _connection.CreateTransaction(); Task <bool> transactionTask = ExecuteTransaction(transaction, task.Result, messages); if (transactionTask.IsCompleted) { transactionTask.Wait(); bool success = transactionTask.Result; if (success) { OnTransactionComplete(transaction, success, messages, tcs); } else { _trace.TraceVerbose("Transaction failed. Retrying..."); _trace.TraceVerbose("Transaction disposed"); transaction.Dispose(); goto go; } } else { OnTransactionCompleting(transaction, transactionTask, messages, tcs); } } catch (OperationCanceledException) { tcs.TrySetCanceled(); } catch (Exception ex) { tcs.TrySetException(ex); } } else { task.Then(oldId => { _trace.TraceVerbose("CreateTransaction({0})", oldId); transaction = _connection.CreateTransaction(); Task <bool> transactionTask = ExecuteTransaction(transaction, oldId, messages); OnTransactionCompleting(transaction, transactionTask, messages, tcs); }) .ContinueWithNotComplete(tcs); } }