public static async Task<StateManagerLease> GetOrCreateAsync(
     IReliableStateManager StateManager,
     IReliableDictionary<string, string> StateDictionary,
     string EntryName,
     string partitionId)
 {
     using (ITransaction tx = StateManager.CreateTransaction())
     {
         StateManagerLease lease;
         // if something has been saved before load it
         ConditionalResult<string> cResults = await StateDictionary.TryGetValueAsync(tx, EntryName);
         if (cResults.HasValue)
         {
             lease = FromJsonString(cResults.Value);
             lease.m_EntryName = EntryName;
             lease.m_StateDictionary = StateDictionary;
             lease.m_StateManager = StateManager;
         }
         else
         {
             // if not create new
             lease = new StateManagerLease(StateManager, StateDictionary, EntryName, partitionId);
         }
         await tx.CommitAsync();
         return lease;
     }
 }
Пример #2
0
        private static async Task AddOrUpdatePrivateAsync <TKey>(
            IReliableDictionary <TKey, byte[]> dictionary,
            IReliableStateManager stateManager,
            TKey key,
            byte[] value,
            CancellationToken cancellationToken) where TKey : IComparable <TKey>, IEquatable <TKey>
        {
            using (ITransaction tx = stateManager.CreateTransaction())
            {
                await dictionary.AddOrUpdateAsync(tx, key, value, (k, v) => value, FASConstants.ReliableDictionaryTimeout, cancellationToken).ConfigureAwait(false);

                await tx.CommitAsync().ConfigureAwait(false);
            }
        }
Пример #3
0
        public async Task <IEnumerable <Product> > GetAllProducts()
        {
            var products = await stateManager.GetOrAddAsync <IReliableDictionary <Guid, Product> >("products");

            var list = new List <Product>();

            using (var t = stateManager.CreateTransaction())
            {
                var e = await products.CreateEnumerableAsync(t);

                using (var en = e.GetAsyncEnumerator())
                {
                    while (await en.MoveNextAsync(CancellationToken.None))
                    {
                        var p = en.Current.Value;
                        list.Add(p);
                    }
                }
                await t.CommitAsync();
            }

            return(list);
        }
        public async Task Consume(ConsumeContext <ICountRequest> context)
        {
            var statsDictionary = await _stateManager.GetOrAddAsync <IReliableDictionary <string, long> >("statsDictionary").ConfigureAwait(false);

            using (var tx = _stateManager.CreateTransaction())
            {
                var result = await statsDictionary.TryGetValueAsync(tx, "Number of Words Processed").ConfigureAwait(false);

                if (result.HasValue)
                {
                    await context.RespondAsync <ICountResponse>(new { Count = result.Value }).ConfigureAwait(false);
                }
            }
        }
        public static async Task <TResult> ExecuteWithRetry <TResult>(
            IReliableStateManager stateManager,
            Func <ITransaction, CancellationToken, object, Task <TResult> > operation,
            object state = null,
            CancellationToken cancellationToken = default(CancellationToken),
            int maxAttempts       = DefaultMaxAttempts,
            TimeSpan?initialDelay = null)
        {
            if (stateManager == null)
            {
                throw new ArgumentNullException(nameof(stateManager));
            }
            if (operation == null)
            {
                throw new ArgumentNullException(nameof(operation));
            }
            if (maxAttempts <= 0)
            {
                maxAttempts = DefaultMaxAttempts;
            }
            if (initialDelay == null || initialDelay.Value < MinimumDelay)
            {
                initialDelay = InitialDelay;
            }

            Func <CancellationToken, object, Task <TResult> > wrapped = async(token, st) =>
            {
                TResult result;
                using (var tran = stateManager.CreateTransaction())
                {
                    try
                    {
                        result = await operation(tran, cancellationToken, state);

                        await tran.CommitAsync();
                    }
                    catch (TimeoutException)
                    {
                        tran.Abort();
                        throw;
                    }
                }
                return(result);
            };

            var outerResult = await ExecuteWithRetry(wrapped, state, cancellationToken, maxAttempts, initialDelay);

            return(outerResult);
        }
        public async Task MergeSurveyAnswerToAnalysisAsync([FromBody] SlotModel model)
        {
            try
            {
                var reliableDictionary = await _stateManager.GetOrAddAsync <IReliableDictionary <string, int> >("reliableDictionary");

                using (var tx = _stateManager.CreateTransaction())
                {
                    var conditional = await reliableDictionary.TryGetValueAsync(tx, model.Slot, LockMode.Update);

                    var accumulated = 0;

                    if (!conditional.HasValue)
                    {
                        accumulated += 1;
                    }
                    else
                    {
                        accumulated = conditional.Value + 1;
                    }

                    ServiceEventSource.Current.Message($"Slug name:{model.Slot}|Total answers:{accumulated}");

                    await reliableDictionary.AddOrUpdateAsync(tx, model.Slot, accumulated, (key, element) => {
                        return(accumulated);
                    });

                    await tx.CommitAsync();
                }
            }
            catch (Exception ex)
            {
                ServiceEventSource.Current.ServiceRequestFailed(ex.ToString());
                throw;
            }
        }
        public async Task <IActionResult> Get()
        {
            var ct = new CancellationToken();

            var result = new List <KeyValuePair <int, Character> >();

            try
            {
                var characterDictionary = await _stateManager.GetOrAddAsync <IReliableDictionary <int, Character> >(CharactersName);

                using (ITransaction tx = _stateManager.CreateTransaction())
                {
                    var list = await characterDictionary.CreateEnumerableAsync(tx);

                    var enumerator = list.GetAsyncEnumerator();


                    while (await enumerator.MoveNextAsync(ct))
                    {
                        result.Add(enumerator.Current);
                    }

                    // init
                    if (!result.Any())
                    {
                        result.AddRange(await Init.Populate(characterDictionary, tx));
                    }

                    return(Json(result));
                }
            }
            catch
            {
                return(new BadRequestResult());
            }
        }
Пример #8
0
        private static async Task RemoveKeyFromReliableDictionaryPrivateAsync(
            this IReliableStateManager stateManager,
            string dictionaryName,
            string keyToRemove,
            CancellationToken ctok)
        {
            IReliableDictionary <string, byte[]> dictionary = await stateManager.GetOrAddAsync <IReliableDictionary <string, byte[]> >(dictionaryName).ConfigureAwait(false);

            using (var tx = stateManager.CreateTransaction())
            {
                await dictionary.TryRemoveAsync(tx, keyToRemove).ConfigureAwait(false);

                await tx.CommitAsync().ConfigureAwait(false);
            }
        }
Пример #9
0
        public static async Task <long> AppendToStreamAsync(this IReliableStateManager stateManager,
                                                            string streamName, long version, IEvent[] events, Func <IEvent, EventData> serializer,
                                                            string queueName = defaultOutBoxQueueName, string streamsName = defaultStreamsName)
        {
            using (var tx = stateManager.CreateTransaction())
            {
                var result = await stateManager.AppendAndMapToStreamAsync(tx, streamName, version, events, serializer, streamsName);

                await stateManager.EnqueuAsync(tx, result.Item1.Select(x => x.ToEventData()), queueName);

                await tx.CommitAsync();

                return(result.Item2);
            }
        }
Пример #10
0
        private static async Task <IEnumerable <object> > EnumerateState <T>(IReliableStateManager stateManager, IReliableQueue <T> collection, CancellationToken cancellationToken)
        {
            var list = new List <object>();

            using (var tx = stateManager.CreateTransaction())
            {
                var enumerable = await collection.CreateEnumerableAsync(tx);

                var enumerator = enumerable.GetAsyncEnumerator();
                while (await enumerator.MoveNextAsync(cancellationToken))
                {
                    list.Add(enumerator.Current);
                }
            }
            return(list);
        }
Пример #11
0
        public async Task Receive()
        {
            try
            {
                using (ITransaction tx = stateManager.CreateTransaction())
                {
                    ConditionalValue <string> offsetResult = await offsetDictionary.TryGetValueAsync(tx, "offset", LockMode.Default);

                    ConditionalValue <long> epochResult = await epochDictionary.TryGetValueAsync(tx, "epoch", LockMode.Update);

                    long newEpoch = epochResult.HasValue ? epochResult.Value + 1 : 0;
                    await epochDictionary.SetAsync(tx, "epoch", newEpoch);

                    PartitionReceiver partitionReceiver;

                    if (offsetResult.HasValue)
                    {
                        partitionReceiver = eventHubClient.CreateEpochReceiver(PartitionReceiver.DefaultConsumerGroupName,
                                                                               "3",    //Convert.ToString(this.servicePartitionKey),
                                                                               offsetResult.Value,
                                                                               newEpoch);
                    }
                    else
                    {
                        partitionReceiver = eventHubClient.CreateEpochReceiver(PartitionReceiver.DefaultConsumerGroupName,
                                                                               Convert.ToString(this.servicePartitionKey),
                                                                               "-1",
                                                                               newEpoch);
                    }

                    ServiceEventSource.Current.ServiceMessage(this.serviceContext,
                                                              $"Partition Id: {servicePartitionKey}; offset: {offsetResult.Value}; Epoch: {newEpoch}");
                    var ehEvents = await partitionReceiver.ReceiveAsync(1);
                    await GenerateActors(ehEvents, tx);

                    await tx.CommitAsync();
                }
            }
            catch (EventHubsException ex)
            {
                ServiceEventSource.Current.ServiceMessage(this.serviceContext, ex.Message);
            }
            catch (Exception ex)
            {
                ServiceEventSource.Current.ServiceMessage(this.serviceContext, ex.Message);
            }
        }
Пример #12
0
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLifetime, IReliableStateManager reliableStateManager)
        {
            appLifetime.ApplicationStopping.Register(() => {
                var reliableDictionary = reliableStateManager.GetOrAddAsync <IReliableDictionary <string, string> >("Connections").GetAwaiter().GetResult();
                using (ITransaction tx = reliableStateManager.CreateTransaction())
                {
                    foreach (var connectionUserId in ConnectionManagementHub.ConnectedUserIds)
                    {
                        reliableDictionary.TryRemoveAsync(tx, connectionUserId).GetAwaiter().GetResult();
                    }

                    tx.CommitAsync().GetAwaiter().GetResult();
                }
            });


            app.UseCors(Strings.CorsPolicy);

            app.UseAuthentication();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseMvc();

            if (UseAzureSignalR)
            {
                app.UseAzureSignalR(routes =>
                {
                    routes.MapHub <ConnectionManagementHub>("/hubs/connectionManagement");
                });
            }
            else
            {
                app.UseSignalR(routes =>
                {
                    routes.MapHub <ConnectionManagementHub>("/hubs/connectionManagement");
                });
            }
        }
Пример #13
0
        public async Task <IList <UserItem> > GetAllUsersAsync()
        {
            var users = await _stateManager.GetOrAddAsync <IReliableDictionary <ItemId, UserItem> >(DictionaryName);

            var list = new List <UserItem>();

            using (var tx = _stateManager.CreateTransaction())
            {
                var enumerator       = (await users.CreateEnumerableAsync(tx)).GetAsyncEnumerator();
                CancellationToken ct = default(CancellationToken);
                while (await enumerator.MoveNextAsync(ct))
                {
                    list.Add(enumerator.Current.Value);
                }
            }
            return(list);
        }
Пример #14
0
        private static async Task SetUtcTimeStampPrivateAsync(
            this IReliableStateManager stateManager,
            string timeStampDictionaryName,
            string timeStampKey,
            DateTime timeStamp,
            CancellationToken cancellationToken)
        {
            IReliableDictionary <string, byte[]> timeStampRD = await stateManager.GetOrAddAsync <IReliableDictionary <string, byte[]> >(timeStampDictionaryName).ConfigureAwait(false);

            using (var tx = stateManager.CreateTransaction())
            {
                byte[] timestampInBytes = BitConverter.GetBytes(timeStamp.Ticks);
                await timeStampRD.AddOrUpdateAsync(tx, timeStampKey, timestampInBytes, (k, v) => timestampInBytes, FASConstants.ReliableDictionaryTimeout, cancellationToken).ConfigureAwait(false);

                await tx.CommitAsync().ConfigureAwait(false);
            }
        }
        public static async Task <bool> ReadStoppedNodeStateInnerAsync(IReliableStateManager stateManager, IReliableDictionary <string, bool> stoppedNodeTable, string nodeName, CancellationToken cancellationToken)
        {
            using (var tx = stateManager.CreateTransaction())
            {
                var data = await stoppedNodeTable.TryGetValueAsync(tx, nodeName, TimeSpan.FromSeconds(4), cancellationToken).ConfigureAwait(false);

                if (data.HasValue)
                {
                    TestabilityTrace.TraceSource.WriteInfo(TraceType, "ReadStoppedNodeState:  table has value");
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
Пример #16
0
        public async Task <IEventHubPartitionState> GetOrCreateAsync(string PartitionId)
        {
            if (null == mStateManager)
            {
                throw new InvalidOperationException("assigned state manager is null");
            }



            if (null == StateStore)
            {
                StateStore = await mStateManager.GetOrAddAsync <IReliableDictionary <string, DefaultPartitionState> >(ReliableDictionaryName);
            }


            if (null == StateStore)
            {
                throw new InvalidOperationException("could not create a reliable dictionary to store partition state");
            }


            var entryname = string.Concat(EntriesPrefix, PartitionId);
            DefaultPartitionState partitionState = null;

            using (var tx = mStateManager.CreateTransaction())
            {
                var result = await StateStore.TryGetValueAsync(tx, entryname);

                if (result.HasValue)
                {
                    partitionState = result.Value;
                }
                else
                {
                    partitionState             = new DefaultPartitionState();
                    partitionState.PartitionId = PartitionId;
                }
                await tx.CommitAsync();
            }
            partitionState.EntryName    = entryname;
            partitionState.StateStore   = StateStore;
            partitionState.StateManager = mStateManager;


            return(partitionState);
        }
        public async Task<IActionResult> Put(UserDto userDto)
        {
            //IReliableDictionary<string, int> votesDictionary = await _stateManager.GetOrAddAsync<IReliableDictionary<string, int>>("counts");

            if (ModelState.IsValid)
            {
                using (ITransaction tx = _stateManager.CreateTransaction())
                {
                    await _ndDbContext.AddAsync<User>(_mapper.Map<User>(userDto));
                    await _ndDbContext.SaveChangesAsync();
                    await tx.CommitAsync();
                    return new OkResult();
                }
            }

            return BadRequest(ModelState);

        }
Пример #18
0
        public async Task AddStoryPointData(perStoryData perStoryData)
        {
            IReliableDictionary <Guid, perStoryData> storypointdata = await _stateManager
                                                                      .GetOrAddAsync <IReliableDictionary <Guid, perStoryData> >("storypointdata");

            using (ITransaction tx = _stateManager.CreateTransaction())
            {
                await storypointdata
                .AddOrUpdateAsync(tx, perStoryData.Id, perStoryData, (id, value) => perStoryData);

                await tx.CommitAsync();
            }
        }
        public void TestInitialize()
        {
            userDictionaryManager = new MockReliableStateManager();

            IReliableDictionary2 <UserName, Basic.Common.UserProfile> users =
                userDictionaryManager.GetOrAddAsync <IReliableDictionary2 <UserName, Basic.Common.UserProfile> >("users").Result;
            var indexed_users = userDictionaryManager.GetOrAddIndexedAsync <UserName, Basic.Common.UserProfile>("indexed_users",
                                                                                                                FilterableIndex <UserName, Basic.Common.UserProfile, string> .CreateQueryableInstance("Email"),
                                                                                                                FilterableIndex <UserName, Basic.Common.UserProfile, int> .CreateQueryableInstance("Age")).Result;

            for (int i = 0; i < 5; i++)
            {
                using (var tx = userDictionaryManager.CreateTransaction())
                {
                    var user = new Basic.Common.UserProfile
                    {
                        Name = new UserName
                        {
                            First = $"First{i}",
                            Last  = $"Last{i}",
                        },
                        Email   = $"user-{i}@example.com",
                        Age     = 20 + i / 3,
                        Address = new Basic.Common.Address
                        {
                            AddressLine1 = $"1{i} Main St.",
                            City         = "Seattle",
                            State        = "WA",
                            Zipcode      = 98117,
                        },
                    };


                    users.SetAsync(tx, user.Name, user, TimeSpan.FromSeconds(4), new CancellationToken());
                    indexed_users.SetAsync(tx, user.Name, user, TimeSpan.FromSeconds(4), new CancellationToken());
                    tx.CommitAsync();
                }
            }

            Assert.IsTrue(userDictionaryManager.TryGetAsync <IReliableDictionary2 <UserName, Basic.Common.UserProfile> >("users").Result.HasValue);
            Assert.IsTrue(userDictionaryManager.TryGetIndexedAsync <UserName, Basic.Common.UserProfile>("indexed_users",
                                                                                                        FilterableIndex <UserName, Basic.Common.UserProfile, string> .CreateQueryableInstance("Email"),
                                                                                                        FilterableIndex <UserName, Basic.Common.UserProfile, int> .CreateQueryableInstance("Age")).Result.HasValue);
        }
        public async Task WriteAsync(VisualObject state, CancellationToken cancellationToken)
        {
            IReliableStateManager stateManager = ReliableCollectionsExtensions.GetReliableStateManager();

            if (stateManager == null)
            {
                Console.WriteLine($"Failed to get StateManager");
                return;
            }

            using (var txn = stateManager.CreateTransaction())
            {
                var visualObjectStore = await stateManager.GetOrAddAsync <IReliableDictionary <string, VisualObject> >(VISUAL_OBJECT_STORE);

                await visualObjectStore.AddOrUpdateAsync(txn, state.Name, state, (k, v) => state);

                await txn.CommitAsync();
            }
        }
Пример #21
0
        public static async Task <List <ExternalResource> > GetExternalResourcesAsync(IReliableStateManager _stateManager)
        {
            var dict = await StateManagerHelper.GetExternalResourceDictAsync(_stateManager);

            List <ExternalResource> listResources = new List <ExternalResource>();

            using (ITransaction tx = _stateManager.CreateTransaction())
            {
                Microsoft.ServiceFabric.Data.IAsyncEnumerable <KeyValuePair <string, ExternalResource> > list = await dict.CreateEnumerableAsync(tx, EnumerationMode.Ordered)
                                                                                                                .ConfigureAwait(false);

                var asyncEnumerator = list.GetAsyncEnumerator();
                while (await asyncEnumerator.MoveNextAsync(CancellationToken.None).ConfigureAwait(false))
                {
                    listResources.Add(asyncEnumerator.Current.Value);
                }
            }
            return(listResources);
        }
Пример #22
0
        public async Task <ICollection <Customer> > GetAllCustomers(CancellationToken cancellationToken = default)
        {
            var customers = await stateManager.GetOrAddAsync <IReliableDictionary <Guid, Customer> >("Customer");

            var results = new List <Customer>();

            using (var tx = stateManager.CreateTransaction())
            {
                var enumerable = await customers.CreateEnumerableAsync(tx);

                var enumerator = enumerable.GetAsyncEnumerator();
                while (await enumerator.MoveNextAsync(cancellationToken) && !cancellationToken.IsCancellationRequested)
                {
                    results.Add(enumerator.Current.Value);
                }
            }

            return(results);
        }
        // This is a seperate method because we now know PropertyType, so want to not use reflection in above method
        public static async Task <IEnumerable <TKey> > FilterHelper <TKey, TValue, TFilter>(IReliableIndexedDictionary <TKey, TValue> dictionary, TFilter constant, BinaryOperatorKind strategy, bool notIsApplied, CancellationToken cancellationToken, IReliableStateManager stateManager, string propertyName)
            where TFilter : IComparable <TFilter>, IEquatable <TFilter>
            where TKey : IComparable <TKey>, IEquatable <TKey>
        {
            using (var tx = stateManager.CreateTransaction())
            {
                IEnumerable <TKey> result;
                // Equals
                if ((strategy == BinaryOperatorKind.Equal && !notIsApplied) ||
                    (strategy == BinaryOperatorKind.NotEqual && notIsApplied))
                {
                    result = await dictionary.FilterKeysOnlyAsync(tx, propertyName, constant, TimeSpan.FromSeconds(4), cancellationToken);
                }
                else if ((strategy == BinaryOperatorKind.GreaterThan && !notIsApplied) ||
                         (strategy == BinaryOperatorKind.LessThan && notIsApplied))
                {
                    result = await dictionary.RangeFromFilterKeysOnlyAsync(tx, propertyName, constant, RangeFilterType.Exclusive, TimeSpan.FromSeconds(4), cancellationToken);
                }
                else if ((strategy == BinaryOperatorKind.GreaterThanOrEqual && !notIsApplied) ||
                         (strategy == BinaryOperatorKind.LessThanOrEqual && notIsApplied))
                {
                    result = await dictionary.RangeFromFilterKeysOnlyAsync(tx, propertyName, constant, RangeFilterType.Inclusive, TimeSpan.FromSeconds(4), cancellationToken);
                }
                else if ((strategy == BinaryOperatorKind.LessThan && !notIsApplied) ||
                         (strategy == BinaryOperatorKind.GreaterThan && notIsApplied))
                {
                    result = await dictionary.RangeToFilterKeysOnlyAsync(tx, propertyName, constant, RangeFilterType.Exclusive, TimeSpan.FromSeconds(4), cancellationToken);
                }
                else if ((strategy == BinaryOperatorKind.LessThanOrEqual && !notIsApplied) ||
                         (strategy == BinaryOperatorKind.GreaterThanOrEqual && notIsApplied))
                {
                    result = await dictionary.RangeToFilterKeysOnlyAsync(tx, propertyName, constant, RangeFilterType.Inclusive, TimeSpan.FromSeconds(4), cancellationToken);
                }
                else
                {
                    // Bad State, should never hit
                    throw new NotSupportedException("Does not support Add, Subtract, Modulo, Multiply, Divide operations.");
                }
                await tx.CommitAsync();

                return(result);
            }
        }
        public async Task <ConditionalValue <IReliableDictionary <TKey, TValue> > > TryCopyToReliableDictionary <TKey, TValue>(string sourceKey, string targetKey, IReliableStateManager stateManager) where TKey : IComparable <TKey>, IEquatable <TKey>
        {
            var conditionalValue = await reliableStateManagerHelper.TryGetAsync <IReliableDictionary <TKey, TValue> >(stateManager, sourceKey);

            if (!conditionalValue.HasValue)
            {
                return(new ConditionalValue <IReliableDictionary <TKey, TValue> >(false, null));
            }

            IReliableDictionary <TKey, TValue> source = conditionalValue.Value;

            conditionalValue = await reliableStateManagerHelper.TryGetAsync <IReliableDictionary <TKey, TValue> >(stateManager, targetKey);

            if (!conditionalValue.HasValue)
            {
                return(new ConditionalValue <IReliableDictionary <TKey, TValue> >(false, null));
            }

            IReliableDictionary <TKey, TValue> target = conditionalValue.Value;

            using (ITransaction tx = stateManager.CreateTransaction())
            {
                await target.ClearAsync();

                var asyncEnumerable = await source.CreateEnumerableAsync(tx);

                var asyncEnumerator = asyncEnumerable.GetAsyncEnumerator();
                CancellationTokenSource tokenSource = new CancellationTokenSource();

                var tasks = new List <Task>();

                while (await asyncEnumerator.MoveNextAsync(tokenSource.Token))
                {
                    var currentEntry = asyncEnumerator.Current;
                    tasks.Add(target.SetAsync(tx, currentEntry.Key, currentEntry.Value));
                }

                Task.WaitAll(tasks.ToArray());
                await tx.CommitAsync();
            }

            return(new ConditionalValue <IReliableDictionary <TKey, TValue> >(true, target));
        }
        private async Task <List <ServiceMessage> > GetResultsAsync()
        {
            var messages = new List <ServiceMessage>();
            var storage  = await this._manager.GetOrAddAsync <IReliableDictionary <string, ServiceMessage> >("storage");

            using (var tx = _manager.CreateTransaction())
            {
                var messageEnumerable = await storage.CreateEnumerableAsync(tx, EnumerationMode.Ordered);

                using (Microsoft.ServiceFabric.Data.IAsyncEnumerator <KeyValuePair <string, ServiceMessage> > enumerator = messageEnumerable.GetAsyncEnumerator())
                {
                    while (await enumerator.MoveNextAsync(CancellationToken.None))
                    {
                        messages.Add(enumerator.Current.Value);
                    }
                }
            }
            return(messages);
        }
Пример #26
0
        public async static Task RunAsync(
            CancellationToken cancellationToken,
            IGitHubClient gitHubClient,
            IReliableStateManager stateManager,
            IUserRepoSearchActorProvider userRepoSearchActorProvider)
        {
            IReliableConcurrentQueue <ScrapingTask> firstTaskQueue =
                await GetFirstTaskQueue(stateManager);

            IReliableConcurrentQueue <ScrapingTask> secondTaskQueue =
                await GetSecondTaskQueue(stateManager);

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();
                if (firstTaskQueue.Count > 0 || secondTaskQueue.Count > 0)
                {
                    using (var tx = stateManager.CreateTransaction())
                    {
                        var dequeued = await firstTaskQueue.TryDequeueAsync(tx, cancellationToken);

                        if (!dequeued.HasValue)
                        {
                            dequeued = await secondTaskQueue.TryDequeueAsync(tx, cancellationToken);
                        }

                        await ProcessScrapingTask(
                            scrapingTask : dequeued.Value,
                            gitHubClient : gitHubClient, tx : tx,
                            firstTaskQueue : firstTaskQueue,
                            secondTaskQueue : secondTaskQueue,
                            userRepoSearchActorProvider : userRepoSearchActorProvider);

                        await tx.CommitAsync();
                    }
                }
                else
                {
                    await Task.Delay(Constants.EMPTY_DELAY, cancellationToken);
                }
            }
        }
        public async Task <Dictionary <TKey, TValue> > GetDataCopyAsync()
        {
            Dictionary <TKey, TValue> copy = new Dictionary <TKey, TValue>();

            Microsoft.ServiceFabric.Data.IAsyncEnumerable <KeyValuePair <TKey, TValue> > asyncEnumerable;

            using (ITransaction tx = stateManager.CreateTransaction())
            {
                asyncEnumerable = await reliableDictionary.CreateEnumerableAsync(tx);
            }

            var asyncEnumerator = asyncEnumerable.GetAsyncEnumerator();
            CancellationTokenSource tokenSource = new CancellationTokenSource();

            while (await asyncEnumerator.MoveNextAsync(tokenSource.Token))
            {
                var currentEntry = asyncEnumerator.Current;
                copy.Add(currentEntry.Key, currentEntry.Value);
            }

            return(copy);
        }
        public async Task <TConfig> GetAsync(TKey id)
        {
            var collection = await _reliableStateManager.GetOrAddAsync <IReliableDictionary <TKey, TConfig> >("ConfigStore");

            using (var tran = _reliableStateManager.CreateTransaction())
            {
                if (await collection.ContainsKeyAsync(tran, id))
                {
                    return((await collection.TryGetValueAsync(tran, id)).Value);
                }

                var config = await this.GetByIdAsync(id);

                await collection.AddAsync(tran, id, config);

                await tran.CommitAsync();

                return(config);
            }
        }
Пример #29
0
        public async static Task <Result> RequestUserInfoScrapingAsync(
            IReliableStateManager stateManager, RequestUserInfoScrapingInput input)
        {
            var firstTaskQueue = await GetFirstTaskQueue(stateManager);

            using (var tx = stateManager.CreateTransaction())
            {
                await firstTaskQueue.EnqueueAsync(tx, new ScrapingTask
                {
                    AuthToken = input.AuthToken,
                    UserLogin = input.UserLogin,
                    Type      = ScrapingTaskType.UserInfo
                });

                await tx.CommitAsync();
            }

            return(new Result {
                Success = true
            });
        }
Пример #30
0
        public async Task <IEnumerable <Product> > GetAllProducts()
        {
            List <Product> result = new List <Product>();

            IReliableDictionary <Guid, Product> products = await _stateManager.GetOrAddAsync <IReliableDictionary <Guid, Product> >(PRODUCTS_COLLECTION);

            using (ITransaction tx = _stateManager.CreateTransaction())
            {
                IAsyncEnumerable <KeyValuePair <Guid, Product> > allProducts = await products.CreateEnumerableAsync(tx, EnumerationMode.Ordered);

                using (IAsyncEnumerator <KeyValuePair <Guid, Product> > enumerator = allProducts.GetAsyncEnumerator())
                {
                    while (await enumerator.MoveNextAsync(CancellationToken.None))
                    {
                        KeyValuePair <Guid, Product> current = enumerator.Current;
                        result.Add(current.Value);
                    }
                }
            }

            return(result);
        }
Пример #31
0
        public virtual async Task <IEnumerable <TEntity> > FindAsync(Func <TEntity, bool> predicate)
        {
            var reliableDictionary = await GetReliableDictionary();

            var entities = new BlockingCollection <TEntity>();

            using (var tx = _stateManager.CreateTransaction())
            {
                var enumerator = (await reliableDictionary.CreateEnumerableAsync(tx))
                                 .GetAsyncEnumerator();
                while (await enumerator.MoveNextAsync(CancellationToken.None))
                {
                    if (predicate(enumerator.Current.Value))
                    {
                        entities.Add(enumerator.Current.Value);
                    }
                }
            }
            return(entities);
        }