Example #1
0
    def handle(self, credentials_name=None, *args, **options):

        # The suggested time between hearbeats
        poll_interval = options.get('poll_interval', 10)

        # First expire any old stream process records that have failed
        # to report in for a while
        timeout_seconds = 3 * poll_interval
        StreamProcess.expire_timed_out()

        stream_process = StreamProcess.create(timeout_seconds=3 *
                                              poll_interval)

        def stop(signum, frame):
            """
            Handle sudden stops
            """
            logger.debug("Stopping because of signal")

            if stream_process:
                stream_process.status = StreamProcess.STREAM_STATUS_STOPPED
                stream_process.heartbeat()

            raise SystemExit()

        def install_signal_handlers():
            """
            Installs signal handlers for handling SIGINT and SIGTERM
            gracefully.
            """

            signal.signal(signal.SIGINT, stop)
            signal.signal(signal.SIGTERM, stop)

        install_signal_handlers()

        try:
            credentials = get_credentials(credentials_name)

            logger.info("Using credentials for %s", credentials.name)
            stream_process.credentials = credentials
            stream_process.save()

            auth = tweepy.OAuthHandler(credentials.api_key,
                                       credentials.api_secret)
            auth.set_access_token(credentials.access_token,
                                  credentials.access_token_secret)

            listener = QueueStreamListener()
            checker = FeelsTermChecker(queue_listener=listener,
                                       stream_process=stream_process)

            # Start and maintain the streaming connection...
            stream = DynamicTwitterStream(auth, listener, checker)

            while checker.ok():
                try:
                    stream.start(poll_interval)
                except Exception as e:
                    checker.error(e)
                    time.sleep(3)  # to avoid craziness

            logger.error("Stopping because term checker not ok")
            stream_process.status = StreamProcess.STREAM_STATUS_STOPPED
            stream_process.heartbeat()

        except Exception as e:
            logger.error(e)

        finally:
            stop(None, None)
Example #2
0
    def handle(self, credentials_name=None, *args, **options):

        # The suggested time between hearbeats
        poll_interval = options.get('poll_interval', 10)

        # First expire any old stream process records that have failed
        # to report in for a while
        timeout_seconds = 3 * poll_interval
        StreamProcess.expire_timed_out()

        stream_process = StreamProcess.create(
            timeout_seconds=3 * poll_interval
        )


        def stop(signum, frame):
            """
            Handle sudden stops
            """
            logger.debug("Stopping because of signal")

            if stream_process:
                stream_process.status = StreamProcess.STREAM_STATUS_STOPPED
                stream_process.heartbeat()

            raise SystemExit()

        def install_signal_handlers():
            """
            Installs signal handlers for handling SIGINT and SIGTERM
            gracefully.
            """

            signal.signal(signal.SIGINT, stop)
            signal.signal(signal.SIGTERM, stop)

        install_signal_handlers()

        try:
            credentials = get_credentials(credentials_name)

            logger.info("Using credentials for %s", credentials.name)
            stream_process.credentials = credentials
            stream_process.save()

            auth = tweepy.OAuthHandler(credentials.api_key, credentials.api_secret)
            auth.set_access_token(credentials.access_token, credentials.access_token_secret)

            listener = QueueStreamListener()
            checker = FeelsTermChecker(queue_listener=listener,
                                       stream_process=stream_process)

            # Start and maintain the streaming connection...
            stream = DynamicTwitterStream(auth, listener, checker)

            while checker.ok():
                try:
                    stream.start(poll_interval)
                except Exception as e:
                    checker.error(e)
                    time.sleep(3)  # to avoid craziness

            logger.error("Stopping because term checker not ok")
            stream_process.status = StreamProcess.STREAM_STATUS_STOPPED
            stream_process.heartbeat()

        except Exception as e:
            logger.error(e)

        finally:
            stop(None, None)