public void Stop()
        {
            if (trackerStream != null)
            {
                trackerStream.CloseStream();
            }
            if (userStream != null)
            {
                userStream.CloseStream();
            }
            if (processingStep != null)
            {
                processingStep.Shutdown();
            }

            log.WriteLine("{0}: Exiting", DateTime.Now);
        }
示例#2
0
 private void OnCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
 {
     _cancelled = true;
     if (_currentStream != null)
     {
         _currentStream.CloseStream();
     }
 }
示例#3
0
        static void UserStreamWithTimeoutDemo(TwitterContext twitterCtx)
        {
            twitterCtx.AuthorizedClient.UseCompression = false;
            twitterCtx.Timeout = 3000;
            StreamContent strmCont = null;

            Console.WriteLine("\nStreamed Content: \n");
            int count = 0;

            // the user stream is for whoever is authenticated
            // via the Authenticator passed to TwitterContext
            (from strm in twitterCtx.UserStream
             where strm.Type == UserStreamType.User
             select strm)
            .StreamingCallback(strm =>
            {
                if (strm.Status != TwitterErrorStatus.Success)
                {
                    Console.WriteLine(strm.Error.ToString());
                    return;
                }

                strmCont = strm;
                Console.WriteLine(strm.Content + "\n");

                if (count++ >= 25)
                {
                    strm.CloseStream();
                }
            })
            .SingleOrDefault();

            while (strmCont == null)
            {
                Console.WriteLine("Waiting on stream to initialize.");

                Thread.Sleep(10000);
            }

            Console.WriteLine("Stream is initialized. Now closing...");
            strmCont.CloseStream();
        }
示例#4
0
        private void StartProcessingQueue(TwitterContext userStreamContext, TwitterContext trackerStreamContext)
        {
            var screenname = UsersCollection.PrimaryUser().TwitterScreenName;
            var queueTime  = int.Parse(ConfigurationManager.AppSettings["QueueTime"] ?? "60000");

            queueTimer = new Timer(queueTime);

            queueTimer.Elapsed += new ElapsedEventHandler((x, y) =>
            {
                queueTimer.Enabled  = false;
                bool restartTracker = false;
                try
                {
                    log.WriteLine("{0}: Processing Queue", DateTime.Now);

                    lock (queue_lock)
                    {
                        if (queue.Count == 0)
                        {
                            log.WriteLine("{0}: No Items to Process", DateTime.Now);
                            return;
                        }
                        tweets = new Tweet[queue.Count];
                        queue.CopyTo(tweets);
                        queue.Clear();
                    }

                    tweets = tweets.Distinct().ToArray();

                    log.WriteLine("{0}: Processing {1} Items from Queue", DateTime.Now, tweets.Length);

                    //Currently there is only one step but there could potentially be multiple user defined steps
                    GetIProcessingStep().ProcessItems(tweets).Wait();

                    if (processingStep is IKeywordSuggestionStep)
                    {
                        try { restartTracker = (processingStep as IKeywordSuggestionStep).HasNewKeywordSuggestions(); } catch { }
                    }

                    tweets = null;
                }
                catch (Exception ex)
                {
                    log.WriteLine("{0}: Error: {1}", DateTime.Now, ex.ToString());
                }
                finally
                {
                    try
                    {
                        if (userStream != null && Math.Abs((lastCallBackTimeUserStream - DateTime.Now).TotalSeconds) > 90) //The User Stream Stalled or was Closed
                        {
                            if (hadUserStreamFailure)
                            {
                                log.WriteLine("{0}: LinqToTwitter User Stream Was Closed Attempting to Reconnect", DateTime.Now);
                            }
                            else
                            {
                                log.WriteLine("{0}: LinqToTwitter User Stream Stalled Attempting to Restart It", DateTime.Now);
                            }

                            userStreamContext = TwitterModel.Instance.GetAuthorizedTwitterContext(screenname);
                            var task          = StartTwitterUserStream(userStreamContext);
                            task.Wait();
                            userStream = task.Result;
                        }
                    }
                    catch (Exception ex)
                    {
                        log.WriteLine("{0}: Error: {1}", DateTime.Now, ex.ToString());
                    }
                    try
                    {
                        if (restartTracker ||                                                                                      //The tracker should be restarted because we have new potential keywords to track
                            (trackerStream != null && Math.Abs((lastCallBackTimeTrackerStream - DateTime.Now).TotalSeconds) > 90)) //The Tracker Stream Stalled or was Closed
                        {
                            if (hadTrackerStreamFailure)
                            {
                                log.WriteLine("{0}: LinqToTwitter Tracker Stream was Closed Attempting to Reconnect", DateTime.Now);
                            }
                            else if (restartTracker)
                            {
                                log.WriteLine("{0}: LinqToTwitter Tracker Stream will be Restarted to Track more Keywords", DateTime.Now);
                            }
                            else
                            {
                                log.WriteLine("{0}: LinqToTwitter Tracker Stream Stalled Attempting to Restart It", DateTime.Now);
                            }

                            if (trackerStream != null)
                            {
                                trackerStream.CloseStream();
                            }

                            trackerStreamContext = TwitterModel.Instance.GetAuthorizedTwitterContext(screenname);
                            var task             = StartTwitterTrackerStream(trackerStreamContext);
                            task.Wait();
                            trackerStream = task.Result;
                        }
                    }
                    catch (Exception ex)
                    {
                        log.WriteLine("{0}: Error: {1}", DateTime.Now, ex.ToString());
                    }

                    queueTimer.Enabled = true;
                    log.WriteLine("{0}: Completed Processing Queue", DateTime.Now);
                }
            });

            log.WriteLine("{0}: Processing Queue every {1} milliseconds", DateTime.Now, queueTime);

            queueTimer.Start();
        }
示例#5
0
        static void Main(string[] args)
        {
            if (!EnsureSingleLoad())
            {
                Console.WriteLine("{0}: Another Instance Currently Runing", DateTime.Now);
                return;
            }

            Console.WriteLine("{0}: Started", DateTime.Now);
            var screenname = UsersCollection.PrimaryUser().TwitterScreenName;

            var secret = ConfigurationManager.AppSettings["TwitterCustomerSecret"];

            HubConnection hubConnection = null;
            IHubProxy     streamingHub  = null;

            while (streamingHubConnectAttempts++ < 3)
            {
                if (streamingHubConnectAttempts > 1)
                {
                    System.Threading.Thread.Sleep(5000);
                }

                Console.WriteLine("{0}: Attempting To Connect To PushURL '{1}' (Attempt: {2})", DateTime.Now, ConfigurationManager.AppSettings["PushURL"], streamingHubConnectAttempts);
                hubConnection = (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["PushURL"])) ? new HubConnection(ConfigurationManager.AppSettings["PushURL"]) : null;

                if (hubConnection != null)
                {
                    try
                    {
                        streamingHub = hubConnection.CreateProxy("streamingHub");
                        hubConnection.StateChanged += new Action <SignalR.Client.StateChange>(sc =>
                        {
                            if (sc.NewState == SignalR.Client.ConnectionState.Connected)
                            {
                                Console.WriteLine("{0}: Push Connection Established", DateTime.Now);
                                lock (queue_push_lock)
                                {
                                    if (queue_push.Count > 0)
                                    {
                                        Console.WriteLine("{0}: Pushing {1} Tweets to Web Application", DateTime.Now, queue_push.Count());
                                        streamingHub.Invoke("Send", new StreamItem()
                                        {
                                            Secret = secret, Data = queue_push
                                        }).Wait();
                                        queue_push.Clear();
                                    }
                                }
                            }
                            else if (sc.NewState == SignalR.Client.ConnectionState.Disconnected)
                            {
                                Console.WriteLine("{0}: Push Connection Lost", DateTime.Now);
                            }
                            else if (sc.NewState == SignalR.Client.ConnectionState.Reconnecting)
                            {
                                Console.WriteLine("{0}: Reestablishing Push Connection", DateTime.Now);
                            }
                            else if (sc.NewState == SignalR.Client.ConnectionState.Connecting)
                            {
                                Console.WriteLine("{0}: Establishing Push Connection", DateTime.Now);
                            }
                        });
                        var startHubTask = hubConnection.Start();
                        startHubTask.Wait();
                        if (!startHubTask.IsFaulted)
                        {
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        hubConnection = null;
                        Console.WriteLine("{0}: Error: {1}", DateTime.Now, ex.ToString());
                    }
                }
            }

            Console.WriteLine("{0}: Getting Friends for {1}", DateTime.Now, screenname);
            TwitterModel.Instance.UpdateFriendsForPrimaryUser();
            Console.WriteLine("{0}: Finished Getting Friends for {1}", DateTime.Now, screenname);

            Console.WriteLine("{0}: Listening to Stream", DateTime.Now);

            var context = TwitterModel.Instance.GetAuthorizedTwitterContext(screenname);

            stream = StartTwitterStream(context);

            queueTimer          = new Timer(60000);
            queueTimer.Elapsed += new ElapsedEventHandler((x, y) =>
            {
                queueTimer.Enabled = false;
                try
                {
                    Console.WriteLine("{0}: Processing Queue", DateTime.Now);

                    lock (queue_lock)
                    {
                        if (queue.Count == 0)
                        {
                            Console.WriteLine("{0}: No Items to Process", DateTime.Now);
                            return;
                        }
                        tweets = new Tweet[queue.Count];
                        queue.CopyTo(tweets);
                        queue.Clear();
                    }

                    Console.WriteLine("{0}: Processing {1} Items from Queue", DateTime.Now, tweets.Length);

                    var tp = new TweetProcessor(tweets, true);
                    tp.Start();

                    tweets
                    .GroupBy(t => t.User.Identifier.ScreenName)
                    .ToList()
                    .ForEach(g =>
                    {
                        Repository <Tweet> .Instance.Save(g.Key + TwitterModel.TWEETS, g.OrderBy(t => t.CreatedAt).Select(t => t).ToList());
                        Console.WriteLine("{0}: {1} Tweets Saved for {2}", DateTime.Now, g.Count(), g.Key);
                    });

                    Repository <Tweet> .Instance.FlushChanges();

                    if (hubConnection != null && streamingHub != null)
                    {
                        int retweetThreshold = UsersCollection.PrimaryUser().RetweetThreshold;
                        tweets = tweets.Where(t => t.RetweetCount >= retweetThreshold).ToArray();
                        if (hubConnection.State == SignalR.Client.ConnectionState.Connected)
                        {
                            if (tweets.Length > 0)
                            {
                                Console.WriteLine("{0}: Pushing {1} Tweets to Web Application", DateTime.Now, tweets.Count());
                                streamingHub.Invoke("Send", new StreamItem()
                                {
                                    Secret = secret, Data = tweets
                                }).Wait();
                            }
                        }
                        else
                        {
                            lock (queue_push_lock)
                            {
                                queue_push.AddRange(tweets);
                            }
                        }
                    }

                    tweets = null;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("{0}: Error: {1}", DateTime.Now, ex.ToString());
                }
                finally
                {
                    try
                    {
                        if (Math.Abs((lastCallBackTime - DateTime.Now).TotalSeconds) > 90)     //The Stream Stalled or was Closed
                        {
                            if (hadStreamFailure)
                            {
                                Console.WriteLine("{0}: LinqToTwitter UserStream Was Closed Attempting to Reconnect", DateTime.Now);
                            }
                            else
                            {
                                Console.WriteLine("{0}: LinqToTwitter UserStream Stalled Attempting to Restart It", DateTime.Now);
                            }

                            context = TwitterModel.Instance.GetAuthorizedTwitterContext(screenname);
                            stream  = StartTwitterStream(context);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("{0}: Error: {1}", DateTime.Now, ex.ToString());
                    }
                    queueTimer.Enabled = true;
                    Console.WriteLine("{0}: Completed Processing Queue", DateTime.Now);
                }
            });
            queueTimer.Start();

            /*
             * It appears like firing off the friends update while running the stream will
             * cause the stream to stop working.
             *
             * var friendTimer = new Timer(3600000);
             * friendTimer.Elapsed += new ElapsedEventHandler((x, y) =>
             *  {
             *      friendTimer.Enabled = false;
             *      try
             *      {
             *          Console.WriteLine("{0}: Getting Friends for {1}", DateTime.Now, screenname);
             *          Friends.Update();
             *          Console.WriteLine("{0}: Finished Getting Friends for {1}", DateTime.Now, screenname);
             *      }
             *      catch (Exception ex)
             *      {
             *          Console.WriteLine("{0}: Error: {1}", DateTime.Now, ex.ToString());
             *      }
             *      finally
             *      {
             *          friendTimer.Enabled = true;
             *      }
             *  });
             * friendTimer.Start();
             */

            while (Console.ReadLine() != "exit")
            {
                ;
            }
            Console.WriteLine("{0}: Exiting", DateTime.Now);
            stream.CloseStream();
        }