Esempio n. 1
0
        /// <summary>
        /// This is the main entry point for your service replica.
        /// This method executes when this replica of your service becomes primary and has write status.
        /// </summary>
        /// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service replica.</param>
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            IReliableDictionary <string, string> myDictionary = await StateManager.GetOrAddAsync <IReliableDictionary <string, string> >("offsetDictionary");

            var            eventHubClient = EventHubClient.CreateFromConnectionString(Config.EhConnectionString, Config.EhCommentPath);
            EventHubClient publishClient  = EventHubClient.CreateFromConnectionString(Config.EhConnectionString, Config.EhAnalyticPath);

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                using (ITransaction tx = StateManager.CreateTransaction())
                {
                    StateHandler stateHandler = new StateHandler(tx, StateManager);

                    EventReader reader = new EventReader(eventHubClient, stateHandler);

                    IEnumerable <AnalysedRedditComment> comments = await reader.GetComments();

                    IEnumerable <IReadOnlyDictionary <string, AnalysedRedditComment> > batches = BatchComments(comments, BatchSize);

                    foreach (IReadOnlyDictionary <string, AnalysedRedditComment> batch in batches)
                    {
                        LanguageAnalyzer  languageAnalyzer  = new LanguageAnalyzer();
                        SentimentAnalyzer sentimentAnalyzer = new SentimentAnalyzer();
                        KeyPhraseAnalyzer keyPhraseAnalyzer = new KeyPhraseAnalyzer();

                        await languageAnalyzer.DoAnalysis(batch); // do language analysis first

                        await sentimentAnalyzer.DoAnalysis(batch);

                        await keyPhraseAnalyzer.DoAnalysis(batch);

                        ProcessMessages(batch.Values, publishClient);
                    }

                    // If an exception is thrown before calling CommitAsync, the transaction aborts, all changes are
                    // discarded, and nothing is saved to the secondary replicas.
                    await tx.CommitAsync();
                }

                await Task.Delay(TimeSpan.FromSeconds(PullDelay), cancellationToken);
            }
            // ReSharper disable once FunctionNeverReturns
        }