public async Task <bool> Execute() { if (sortedByGuid.Count < 2) { // to perform a transaction, there must be at least two items added return(false); } if (transactionId == default(Guid)) { transactionId = Guid.NewGuid(); ExpireTimestamp = DateTime.Now.AddMinutes(TransactionStatusPoolHelper.PURGE_PERIOD_MINUTES); } else if (DateTime.Now < ExpireTimestamp) { var transactionStatus = GrainClient.GrainFactory.GetGrain <ITransactionStatusPool>(TransactionStatusPoolHelper.GetHash(transactionId)); if (await transactionStatus.IsComplete(transactionId)) { // we know the transaction completed, don't bother retrying return(true); } } else { // perhaps throw an exception instead? Transaction has expired and cannot be retried. return(false); } // avoid deadlocks by sorting grains in a deterministic fashion var transactionitems = new List <Tuple <ITransactionGrain <TDeltaState>, TDeltaState> >(); foreach (var item in sortedByGuid.Values) { transactionitems.Add(item); } var transaction = new GrainTransactionData <TDeltaState>() { TransactionId = transactionId, Values = transactionitems }; ITransactionGrain <TDeltaState> firstgrain = transaction.Values[0].Item1; return(await firstgrain.Transact(transaction, 0)); }
public void Add(ITransactionGrain <TDeltaState> target, TDeltaState deltaState) { Guid targetId = target.GetPrimaryKey(); sortedByGuid.Add(targetId, new Tuple <ITransactionGrain <TDeltaState>, TDeltaState>(target, deltaState)); }