public async Task <bool> Prepare() { bool success; try { success = await contractProvider.Prepare(); } catch (Exception ex) { Logger.LogError($"{baseLogString} Prepare => Exception: {ex.Message}", ex); success = false; } if (success) { Logger.LogInformation($"{baseLogString} Prepare => Preparation on NMS Transaction actor SUCCESSFULLY finished."); } else { Logger.LogInformation($"{baseLogString} Prepare => Preparation on NMS Transaction actor UNSUCCESSFULLY finished."); } return(success); }
private async Task <bool> InvokePreparationOnActors(string transactionName) { bool success; var enumerableActiveTransactions = await ActiveTransactions.GetEnumerableDictionaryAsync(); if (!enumerableActiveTransactions.ContainsKey(transactionName)) { string errorMessage = $"{baseLogString} InvokePreparationOnActors => transaction '{transactionName}' not found in '{ReliableDictionaryNames.ActiveTransactions}'."; Logger.LogError(errorMessage); throw new Exception(errorMessage); } var result = await TransactionEnlistmentLedger.TryGetValueAsync(transactionName); if (!result.HasValue) { string errorMessage = $"{baseLogString} InvokePreparationOnActors => Transaction '{transactionName}' not found in '{ReliableDictionaryNames.TransactionEnlistmentLedger}'."; Logger.LogError(errorMessage); throw new Exception(errorMessage); } var transactionLedger = result.Value; if (!enumerableActiveTransactions[transactionName].SetEquals(transactionLedger)) { string errorMessage = $"{baseLogString} InvokePreparationOnActors => not all actors have enlisted for the transaction '{transactionName}'."; Logger.LogError(errorMessage); throw new NotAllActorsEnlistedException(errorMessage); } List <Task <Tuple <string, bool> > > tasks = new List <Task <Tuple <string, bool> > >(); foreach (var transactionActorName in transactionLedger) { var task = Task.Run(async() => { ITransactionActorContract transactionActorClient = TransactionActorClient.CreateClient(transactionActorName); var prepareSuccess = await transactionActorClient.Prepare(); Logger.LogInformation($"{baseLogString} InvokePreparationOnActors => Prepare invoked on Transaction actor: {transactionActorName}, Success: {prepareSuccess}."); return(new Tuple <string, bool>(transactionActorName, prepareSuccess)); }); tasks.Add(task); } var taskResults = await Task.WhenAll(tasks); success = true; foreach (var taskResult in taskResults) { var actorUri = taskResult.Item1; var prepareSuccess = taskResult.Item2; success = success && prepareSuccess; if (success) { Logger.LogInformation($"{baseLogString} InvokePreparationOnActors => Preparation on Transaction actor: {actorUri} finsihed SUCCESSFULLY."); } else { Logger.LogInformation($"{baseLogString} InvokePreparationOnActors => Preparation on Transaction actor: {actorUri} finsihed UNSUCCESSFULLY."); break; } } return(success); }