araldo-redis / araldo_redis / endpoints / redis_endpoint.py

""" araldo Endpoint for communication with Redis PubSub
"""

import gevent
from gevent.monkey import patch_all

patch_all()

from gevent.queue import Queue
import logging
import redis
from araldo.endpoints import InBoundBase, EndPointBase, PluginException



class InBound(InBoundBase):
    def __init__(self, config, gevent_queue, redis_server=None):
        """ Creates an InBound instance for relaying Redis messages
            :param config: configuration object
            :param gevent_queue: queue for forwarding incoming messages
        """
        self._channel_name = config["channel"] 
        if not redis_server:
            self._server = redis.Redis(
                host=config["host"], 
                port=config["port"])
        else:
            self._server = redis_server
        self._client = self._server.pubsub()
        self._client.subscribe(self._channel_name)
        InBoundBase.__init__(self, config, gevent_queue)

    @staticmethod
    def plugin_id():
        return "inbound-redis"

    def _run(self):
        """ Enter infinite gevent loop to receive messages from 
            Redis pubsub queue and push it to gevent queue
        """
        messages = self._client.listen()
        while True:
            message = messages.next()
            print "%s: received: %s" % (self, message)
            self.gevent_queue.put((self._channel_name, message))
            gevent.sleep(0)


    def check(self):
        """ implementation of abstract method
        """
        try:
            response = self._server.echo("x")
            if response != "x":
                raise PluginException("Redis echo failed")
        except Exception as error:
            raise PluginException("Redis connection problem: %s" % str(error))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.