public async Task EnqueueAsync(ITransaction tx, TValue value, CancellationToken cancellationToken = default, TimeSpan?timeout = null)
        {
            if (reliableConcurrentQueue == null)
            {
                await InitializeReliableQueue();
            }

            await reliableConcurrentQueue.EnqueueAsync(tx, value, cancellationToken, timeout);
        }
        private async static Task ScrapeRepositoryAsync(
            ScrapingTask scrapingTask, IGitHubClient gitHubClient,
            ITransaction tx, IUserRepoSearchActor userRepoSearchActor,
            IReliableConcurrentQueue <ScrapingTask> secondTaskQueue)
        {
            ScheduledRepository scheduledRepository =
                scrapingTask.ScheduledRepositories[0];

            Result <Repository> result = await gitHubClient
                                         .ScrapeRepositoryAsync(
                authToken : scrapingTask.AuthToken,
                owner : scheduledRepository.Owner,
                name : scheduledRepository.Name);

            if (scrapingTask.ScheduledRepositories.Count > 1)
            {
                ScrapingTask nextScrapingTask = Serializer.DeepCopy(scrapingTask);

                nextScrapingTask.ScheduledRepositories = nextScrapingTask
                                                         .ScheduledRepositories.Skip(1).ToList();

                await secondTaskQueue.EnqueueAsync(tx, nextScrapingTask);
            }

            if (result.Success)
            {
                result.Data.IsNew = false;
                await userRepoSearchActor.SetRepositoryAsync(result.Data);
            }
        }
        public async Task AddPayment(T model, CancellationToken cancellationToken)
        {
            logger.LogDebug($"Adding object: {Describe(model)} Transaction: {transactionProvider.Current.TransactionId}");
            await queue.EnqueueAsync(transactionProvider.Current, model, cancellationToken);

            logger.LogDebug($"Object {Describe(model)} of type {typeof(T)} added to cache. Transaction {transactionProvider.Current.TransactionId}.");
        }
Exemple #4
0
        private async Task ExecuteAddTradeAsync(Trade trade, IReliableConcurrentQueue <Trade> exportQueue, CancellationToken cancellationToken)
        {
            using (var tx = this.StateManager.CreateTransaction())
            {
                await exportQueue.EnqueueAsync(tx, trade, cancellationToken);

                await tx.CommitAsync();
            }
        }
        private async Task <string> ExecuteEnqueueAsync(Trade trade, IReliableConcurrentQueue <Trade> trades, CancellationToken cancellationToken)
        {
            using (var tx = this.stateManager.CreateTransaction())
            {
                await trades.EnqueueAsync(tx, trade, cancellationToken);

                await tx.CommitAsync();
            }
            return(trade.Id);
        }
Exemple #6
0
        /// <summary>
        /// enqueue incoming message
        /// </summary>
        /// <param name="type">type of message</param>
        /// <param name="message">message payload</param>
        /// <returns>async task</returns>
        public async Task IngestMessage(string type, JObject message)
        {
            InitInflightQueue();
            using (var tx = StateManager.CreateTransaction())
            {
                await _inflightQueue.EnqueueAsync(tx, new InflightMessage (message.ToString(), type ));

                await tx.CommitAsync();
            }
        }
Exemple #7
0
        public async Task StartUpdateDependenciesAsync(int buildId, int channelId)
        {
            IReliableConcurrentQueue <DependencyUpdateItem> queue =
                await StateManager.GetOrAddAsync <IReliableConcurrentQueue <DependencyUpdateItem> >("queue");

            using (ITransaction tx = StateManager.CreateTransaction())
            {
                await queue.EnqueueAsync(tx, new DependencyUpdateItem { BuildId = buildId, ChannelId = channelId });

                await tx.CommitAsync();
            }
        }
Exemple #8
0
        private async Task Populate(CancellationToken cancellationToken)
        {
            IReliableConcurrentQueue <string> list = await this.GetQueueAsync();

            using (ITransaction tx = this.stateManager.CreateTransaction())
            {
                await list.EnqueueAsync(tx, Guid.NewGuid().ToString(), cancellationToken);

                await tx.CommitAsync();
            }
            using (ITransaction tx = this.stateManager.CreateTransaction())
            {
                await list.EnqueueAsync(tx, Guid.NewGuid().ToString(), cancellationToken);

                await tx.CommitAsync();
            }
            using (ITransaction tx = this.stateManager.CreateTransaction())
            {
                await list.TryDequeueAsync(tx, cancellationToken);

                await tx.CommitAsync();
            }
        }
        public async Task PutMessagesAsync(IReadOnlyCollection <QueueMessage> messages, CancellationToken cancellationToken)
        {
            IReliableConcurrentQueue <byte[]> collection = await _stateManager.GetOrAddAsync <IReliableConcurrentQueue <byte[]> >(_queueName).ConfigureAwait(false);

            using (var tx = new ServiceFabricTransaction(_stateManager, null))
            {
                foreach (QueueMessage message in messages)
                {
                    byte[] data = message.ToByteArray();
                    await collection.EnqueueAsync(tx.Tx, data, cancellationToken, _timeout).ConfigureAwait(false);
                }

                await tx.CommitAsync().ConfigureAwait(false);
            }
        }
Exemple #10
0
        public async Task EnqueueAsync(MessageData message)
        {
            try
            {
                if (message == null)
                {
                    throw new ArgumentNullException(nameof(message));
                }

                using (var tran = StateManager.CreateTransaction())
                {
                    await _queue.EnqueueAsync(tran, message, _cancellationToken);

                    await tran.CommitAsync();
                }

                var description = $"Message recived. Queue length {_queue.Count},";

                var healthInformation = new HealthInformation(RECIVING_SOURCE_ID, RECEIVED, HealthState.Ok)
                {
                    Description = description
                };
                this.Partition.ReportReplicaHealth(healthInformation);

                //We have to remove old messages in case of queue overflow condition
                if (_queue.Count > MAX_QUEUE_LENGTH)
                {
                    await TrimAsync();
                }

                _signal.Release();
            }
            catch (Exception ex)
            {
                var description = $"Message rejected due to {ex.Message}.";

                ServiceEventSource.Current.ServiceMessage(this.Context, description);
                var healthInformation = new HealthInformation(RECIVING_SOURCE_ID, REJECTED, HealthState.Warning)
                {
                    Description = description
                };
                this.Partition.ReportReplicaHealth(healthInformation);

                throw;
            }
        }
        public async Task StartAssociatedReleasePipelinesAsync(int buildId, int channelId)
        {
            IReliableConcurrentQueue <ReleasePipelineRunnerItem> queue =
                await StateManager.GetOrAddAsync <IReliableConcurrentQueue <ReleasePipelineRunnerItem> >("queue");

            using (ITransaction tx = StateManager.CreateTransaction())
            {
                await queue.EnqueueAsync(
                    tx,
                    new ReleasePipelineRunnerItem
                {
                    BuildId   = buildId,
                    ChannelId = channelId
                });

                await tx.CommitAsync();
            }
        }
Exemple #12
0
        private async void EnqueueBuildStatusCheck(ReleasePipelineRunnerItem item, int newNumberOfRetriesMade)
        {
            await Task.Delay(TimeSpan.FromMinutes(ReleasePipelineRunner.DelayBetweenBuildStatusChecksInMinutes));

            IReliableConcurrentQueue <ReleasePipelineRunnerItem> queue =
                await StateManager.GetOrAddAsync <IReliableConcurrentQueue <ReleasePipelineRunnerItem> >("queue");

            using (ITransaction tx = StateManager.CreateTransaction())
            {
                await queue.EnqueueAsync(tx, new ReleasePipelineRunnerItem
                {
                    BuildId             = item.BuildId,
                    ChannelId           = item.ChannelId,
                    NumberOfRetriesMade = newNumberOfRetriesMade
                });

                await tx.CommitAsync();
            }
        }
        private async static Task ScrapeUserInfoAsync(
            ScrapingTask scrapingTask, IGitHubClient gitHubClient,
            ITransaction tx, IUserRepoSearchActor userRepoSearchActor,
            IReliableConcurrentQueue <ScrapingTask> secondTaskQueue)
        {
            Result <UserInfo> result = await gitHubClient
                                       .GetUserInfoAsync(scrapingTask.AuthToken);

            if (result.Success)
            {
                await userRepoSearchActor.SetUserInfoAsync(result.Data);

                await secondTaskQueue.EnqueueAsync(tx, new ScrapingTask
                {
                    Type      = ScrapingTaskType.FollowingRepositories,
                    AuthToken = scrapingTask.AuthToken,
                    UserLogin = scrapingTask.UserLogin
                });
            }
        }
        private async static Task ScrapeFollowingRepositoriesAsync(
            ScrapingTask scrapingTask,
            IGitHubClient gitHubClient,
            ITransaction tx,
            IUserRepoSearchActor userRepoSearchActor,
            IReliableConcurrentQueue <ScrapingTask> secondTaskQueue)
        {
            Result <UserInfo> userInfoResult = await gitHubClient
                                               .GetUserInfoAsync(scrapingTask.AuthToken);

            if (!userInfoResult.Success)
            {
                return;
            }

            Result <IEnumerable <Repository> > repositoriesResult = await gitHubClient
                                                                    .ScrapeFollowingRepositoriesAsync(scrapingTask.AuthToken);

            if (!repositoriesResult.Success)
            {
                return;
            }

            List <ScheduledRepository> scheduledRepositories = repositoriesResult.Data.Select(
                repository => new ScheduledRepository
            {
                Name  = repository.Name,
                Owner = repository.Owner
            }).ToList();

            await secondTaskQueue.EnqueueAsync(tx, new ScrapingTask
            {
                Type                  = ScrapingTaskType.Repository,
                AuthToken             = scrapingTask.AuthToken,
                UserLogin             = scrapingTask.UserLogin,
                ScheduledRepositories = scheduledRepositories
            });
        }
Exemple #15
0
        public async Task <bool> Process(DataPoint[] points)
        {
            // TODO: we might mark this as true, and handle "poison" messages
            if (points == null || points.Length == 0)
            {
                return(false);
            }

            if (_queue == null)
            {
                // we can make an interesting assumption here
                _queue = await this.StateManager.GetOrAddAsync <IReliableConcurrentQueue <DataPoint> >(QueueName);
            }

            var firstPoint = points[0];

            //var myDictionary = await this.StateManager.GetOrAddAsync<IReliableDictionary<string, long>>("myDictionary");
            ServiceEventSource.Current.ServiceMessage(this.Context,
                                                      "Process called in Processor, for point: {0} on session {1} through sensor type {2}",
                                                      firstPoint.Timestamp,
                                                      firstPoint.SessionId,
                                                      firstPoint.SensorType);

            using (var tx = this.StateManager.CreateTransaction())
            {
                foreach (var point in points)
                {
                    await _queue.EnqueueAsync(tx, point);
                }

                await tx.CommitAsync();
            }

            _count++;

            return(await Task.FromResult(true));
        }
Exemple #16
0
        private async Task <bool> MatchmakeOneGame(CancellationToken cancellationToken, IReliableConcurrentQueue <UserRequest> queue, IReliableDictionary <ActorInfo, PlayersInMatch> usedActors, IReliableDictionary <UserRequest, ActorInfo> matchmakedUsers)
        {
            try
            {
                ConditionalValue <UserRequest> ret;
                PlayersInMatch players = new PlayersInMatch();
                ActorInfo      actorId;

                using (var tx = this.StateManager.CreateTransaction())
                {
                    do
                    {
                        ret = await queue.TryDequeueAsync(tx, cancellationToken);

                        if (ret.HasValue)
                        {
                            players = players.AddPlayer(new UserRequest(ret.Value));
                        }
                    }while (!cancellationToken.IsCancellationRequested && ret.HasValue && players.Count < MatchSize);

                    if (cancellationToken.IsCancellationRequested || players.Count != MatchSize)
                    {
                        ServiceEventSource.Current.ServiceMessage(this.Context, cancellationToken.IsCancellationRequested ? $"Cancellation requested!" : $"Not enough players in the queue to matchmake!");
                        tx.Abort();
                        return(false);
                    }

                    // found enough players - assign them actor
                    //bool usedActor = false;
                    //do
                    //{
                    //    actorId = ActorId.CreateRandom();
                    //    usedActor = await usedActors.ContainsKeyAsync(tx, actorId);
                    //}
                    //while (!cancellationToken.IsCancellationRequested && usedActor);

                    actorId = await GetSimulationActorId(tx, cancellationToken);

                    if (cancellationToken.IsCancellationRequested)
                    {
                        ServiceEventSource.Current.ServiceMessage(this.Context, $"Cancellation requested!");
                        tx.Abort();
                        return(false);
                    }

                    bool added = await usedActors.TryAddAsync(tx, actorId, players);

                    if (!added)
                    {
                        ServiceEventSource.Current.ServiceMessage(this.Context, $"Tried to add already used actor {actorId}");
                        tx.Abort();
                        return(false);
                    }

                    var playersToAdd = players.GetList();
                    List <UserRequest> addedPlayers = new List <UserRequest>();

                    foreach (var player in playersToAdd)
                    {
                        added = await matchmakedUsers.TryAddAsync(tx, player, actorId);

                        if (added)
                        {
                            addedPlayers.Add(player);
                        }
                    }

                    if (addedPlayers.Count != playersToAdd.Count)
                    {
                        foreach (var player in addedPlayers)
                        {
                            await matchmakedUsers.TryRemoveAsync(tx, player);

                            await queue.EnqueueAsync(tx, player);
                        }
                        ServiceEventSource.Current.ServiceMessage(this.Context, $"Some duplicated requests encountered");
                    }

                    await tx.CommitAsync();
                }

                List <UserRequest> playersList = players.GetList();

                // Create actor simulation
                int index = actorId.ActorIndex;

                string           suffix          = GetSimulationActorNameSuffix(index);
                string           actorServiceUri = $"{this.context.CodePackageActivationContext.ApplicationName}/SimulationActorService{suffix}";
                ISimulationActor simulationActor = ActorProxy.Create <ISimulationActor>(actorId.ActorId, new Uri(actorServiceUri));

                bool simulated = await simulationActor.SimulateMatch(playersList, actorId);

                if (!simulated)
                {
                    ServiceEventSource.Current.Message($"Something went wrong with simulation");
                    return(false);
                }

                await simulationActor.SubscribeAsync <ISimulationEvents>(this, ResubsriptionInterval);

                // Notify clients
                IWebService webService = ServiceProxy.Create <IWebService>(new Uri($"{this.context.CodePackageActivationContext.ApplicationName}/WebService"));
                await webService.StartGame(actorId, playersList);

                StringBuilder builder = new StringBuilder();
                builder.Append($"Created new match\n ActorID: {actorId}\n");

                for (int i = 0; i < players.Count; i++)
                {
                    builder.Append($"UserID_{i}: {playersList[i]}\n");
                }

                ServiceEventSource.Current.ServiceMessage(this.Context, builder.ToString());

                return(true);
            }
            catch (Exception ex)
            {
                ServiceEventSource.Current.Message(string.Format("Exception {0}", ex));
                return(false);
            }
        }
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            IReliableConcurrentQueue <ReportProcessingStep> processQueue
                = await this.StateManager.GetOrAddAsync <IReliableConcurrentQueue <ReportProcessingStep> >(ProcessingQueueName);

            IReliableDictionary <string, ReportStatus> statusDictionary
                = await this.StateManager.GetOrAddAsync <IReliableDictionary <string, ReportStatus> >(StatusDictionaryName);

            // queue up all the processing steps and create an initial processing status if one doesn't exist already
            using (ITransaction tx = this.StateManager.CreateTransaction())
            {
                ConditionalValue <ReportStatus> tryGetResult
                    = await statusDictionary.TryGetValueAsync(tx, this.reportContext.Name, LockMode.Update);

                if (!tryGetResult.HasValue)
                {
                    foreach (string processingStep in processingSteps)
                    {
                        cancellationToken.ThrowIfCancellationRequested();

                        await processQueue.EnqueueAsync(tx, new ReportProcessingStep(processingStep));
                    }

                    await statusDictionary.AddAsync(tx, this.reportContext.Name, new ReportStatus(0, "Not started."));
                }

                await tx.CommitAsync();
            }

            // start processing and checkpoint between each step so we don't lose any progress in the event of a fail-over
            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                try
                {
                    using (ITransaction tx = this.StateManager.CreateTransaction())
                    {
                        ConditionalValue <ReportProcessingStep> dequeueResult = await processQueue.TryDequeueAsync(tx, cancellationToken);

                        if (!dequeueResult.HasValue)
                        {
                            // all done!
                            break;
                        }

                        ReportProcessingStep currentProcessingStep = dequeueResult.Value;

                        ServiceEventSource.Current.ServiceMessage(
                            this.Context,
                            $"Processing step: {currentProcessingStep.Name}");

                        // This takes a shared lock rather than an update lock
                        // because this is the only place the row is written to.
                        // If there were other writers, then this should be an update lock.
                        ConditionalValue <ReportStatus> dictionaryGetResult =
                            await statusDictionary.TryGetValueAsync(tx, this.reportContext.Name, LockMode.Default);

                        ReportStatus currentStatus = dictionaryGetResult.Value;
                        ReportStatus newStatus     = await this.ProcessReport(currentStatus, currentProcessingStep, cancellationToken);

                        await statusDictionary.SetAsync(tx, this.reportContext.Name, newStatus);

                        await tx.CommitAsync();
                    }
                }
                catch (TimeoutException)
                {
                    // transient error. Retry.
                    ServiceEventSource.Current.ServiceMessage(this.Context, "TimeoutException in RunAsync.");
                }
                catch (FabricTransientException fte)
                {
                    // transient error. Retry.
                    ServiceEventSource.Current.ServiceMessage(this.Context, "FabricTransientException in RunAsync: {0}", fte.Message);
                }
                catch (FabricNotPrimaryException)
                {
                    // not primary any more, time to quit.
                    return;
                }
                catch (FabricNotReadableException)
                {
                    // retry or wait until not primary
                    ServiceEventSource.Current.ServiceMessage(this.Context, "FabricNotReadableException in RunAsync.");
                }
                catch (Exception ex)
                {
                    // all other exceptions: log and re-throw.
                    ServiceEventSource.Current.ServiceMessage(this.Context, "Exception in RunAsync: {0}", ex.Message);

                    throw;
                }

                // delay between each to step to prevent starving other processing service instances.
                await Task.Delay(TimeSpan.FromSeconds(10), cancellationToken);
            }


            ServiceEventSource.Current.ServiceMessage(
                this.Context,
                $"Processing complete!");
        }