def __init__(self, redis_hostname, redis_port, redis_password=None):
        threading.Thread.__init__(self)

        self.pubsub = RedisPubSub(redis_hostname, redis_port)
        self.subscriptions = collections.defaultdict()

        # subscribe to dummy channel to make the thread alive
        self.pubsub.subscribe("dummy")
        print self.pubsub
Beispiel #2
0
def main():
    rsp = RedisPubSub()
    channel_name = raw_input(
        "Enter channel name to which you want to publish messages: ")

    var = 1
    while var == 1:
        new_message = raw_input("Enter message you want to publish: ")
        rsp.publish(channel_name, new_message)
    def get(self, status, count, format=json):
        method_to_call = "list_" + status + "_builds"

        rsp = RedisPubSub()
        builds = getattr(rsp, method_to_call)(count)

        if (format == "json"):
            self.write(json_encode(builds))
        elif (format == "html"):
            self.write("HTML format not support.")

        rsp.disconnect()
    def get(self, status, count, format=json):
        method_to_call = "list_"+status+"_builds"

        rsp = RedisPubSub()
        builds = getattr(rsp, method_to_call)(count)

        if (format == "json"):
            self.write(json_encode(builds))
        elif (format == "html"):
            self.write("HTML format not support.")

        rsp.disconnect()
    def get(self, *args):
        type = args[0]
        format = args[1]
        method_to_call = "get_all_" + type

        rsp = RedisPubSub()
        data = getattr(rsp, method_to_call)()
        if (format == "json"):
            self.write(json_encode(list(data)))
        elif (format == "html"):
            self.write("HTML format not support.")

        rsp.disconnect()
    def get(self, *args):
        type = args[0]
        format = args[1]
        method_to_call = "get_all_"+type

        rsp = RedisPubSub()
        data = getattr(rsp, method_to_call)()
        if (format == "json"):
            self.write(json_encode(list(data)))
        elif (format == "html"):
            self.write("HTML format not support.")

        rsp.disconnect()
    def get(self, filter_field, value, format=json):
        self.rsp = RedisPubSub()
        value = urllib.unquote(value).decode('utf-8')

        if (filter_field == "age"):
            builds = self.get_all_filtered_by_age(value)
        elif (filter_field == "host"):
            builds = self.get_all_filtered_by_host(value)
        elif (filter_field == "status"):
            builds = self.get_all_filtered_by_status(value)
        elif (filter_field == "job_name"):
            builds = self.get_builds_by_job_name_pattern(value)

        if (format == "json"):
            self.write(json_encode(builds))
        elif (format == "html"):
            self.render("all_events_sorted.html", builds=builds)

        self.rsp.disconnect()
    def __init__(self, redis_hostname, redis_port, redis_password=None):
        threading.Thread.__init__(self)

        self.pubsub = RedisPubSub(
            redis_hostname,
            redis_port
        )
        self.subscriptions = collections.defaultdict()

        # subscribe to dummy channel to make the thread alive
        self.pubsub.subscribe("dummy")
        print self.pubsub
    def get(self, filter_field, value, format=json):
        self.rsp = RedisPubSub()
        value = urllib.unquote(value).decode('utf-8')

        if (filter_field == "age"):
          builds = self.get_all_filtered_by_age(value)
        elif (filter_field == "host"):
          builds = self.get_all_filtered_by_host(value)
        elif (filter_field == "status"):
          builds = self.get_all_filtered_by_status(value)
        elif (filter_field == "job_name"):
          builds = self.get_builds_by_job_name_pattern(value)

        if (format == "json"):
            self.write(json_encode(builds))
        elif (format == "html"):
            self.render("all_events_sorted.html", builds=builds)

        self.rsp.disconnect()
    def get(self, *args):
        rsp = RedisPubSub()
        field = "build_time_in_millis"

        if (len(args) == 2 and args[0]):
            field = args[0]
            reverse = args[1] == "True"
            builds = rsp.get_all_sorted(field, reverse)
            html = self.render_string("all_events_sorted.html", builds=builds)
            self.write(html)
        else:
            hosts = rsp.get_all_hosts()
            # get builds built within one day by default
            age = 1
            builds = rsp.get_all_filtered_by_age(int(age), True)
            self.render("home.html", hosts=hosts, builds=builds)

        rsp.disconnect()
class FilterHandler(tornado.web.RequestHandler):
    def get(self, filter_field, value, format=json):
        self.rsp = RedisPubSub()
        value = urllib.unquote(value).decode('utf-8')

        if (filter_field == "age"):
            builds = self.get_all_filtered_by_age(value)
        elif (filter_field == "host"):
            builds = self.get_all_filtered_by_host(value)
        elif (filter_field == "status"):
            builds = self.get_all_filtered_by_status(value)
        elif (filter_field == "job_name"):
            builds = self.get_builds_by_job_name_pattern(value)

        if (format == "json"):
            self.write(json_encode(builds))
        elif (format == "html"):
            self.render("all_events_sorted.html", builds=builds)

        self.rsp.disconnect()

    def get_builds_by_job_name_pattern(self, job_name):
        reverse = True
        return self.rsp.get_builds_by_job_name_pattern(job_name, reverse)

    def get_all_filtered_by_age(self, age):
        reverse = True
        return self.rsp.get_all_filtered_by_age(int(age), reverse)

    def get_all_filtered_by_host(self, host):
        reverse = True
        return self.rsp.get_all_filtered_by_host(host, reverse)

    def get_all_filtered_by_status(self, status, format=json):
        # get all builds
        count = sys.maxint
        method_to_call = "list_" + status + "_builds"

        buildnames = getattr(self.rsp, method_to_call)(count)
        builds = []

        for buildname in buildnames:
            build = self.rsp.get_build_by_key(buildname[0])
            builds.append(build)

        return builds
class FilterHandler(tornado.web.RequestHandler):
    def get(self, filter_field, value, format=json):
        self.rsp = RedisPubSub()
        value = urllib.unquote(value).decode('utf-8')

        if (filter_field == "age"):
          builds = self.get_all_filtered_by_age(value)
        elif (filter_field == "host"):
          builds = self.get_all_filtered_by_host(value)
        elif (filter_field == "status"):
          builds = self.get_all_filtered_by_status(value)
        elif (filter_field == "job_name"):
          builds = self.get_builds_by_job_name_pattern(value)

        if (format == "json"):
            self.write(json_encode(builds))
        elif (format == "html"):
            self.render("all_events_sorted.html", builds=builds)

        self.rsp.disconnect()

    def get_builds_by_job_name_pattern(self, job_name):
        reverse = True
        return self.rsp.get_builds_by_job_name_pattern(job_name, reverse)

    def get_all_filtered_by_age(self, age):
        reverse = True
        return self.rsp.get_all_filtered_by_age(int(age), reverse)

    def get_all_filtered_by_host(self, host):
        reverse = True
        return self.rsp.get_all_filtered_by_host(host, reverse)

    def get_all_filtered_by_status(self, status, format=json):
        # get all builds
        count = sys.maxint
        method_to_call = "list_"+status+"_builds"

        buildnames = getattr(self.rsp, method_to_call)(count)
        builds = []

        for buildname in buildnames:
            build = self.rsp.get_build_by_key(buildname[0])
            builds.append(build)

        return builds
    def get(self, *args):
        rsp = RedisPubSub()
        field = "build_time_in_millis"

        if (len(args) == 2 and args[0]):
            field = args[0]
            reverse = args[1] == "True"
            builds = rsp.get_all_sorted(field, reverse)
            html = self.render_string("all_events_sorted.html", builds=builds)
            self.write(html)
        else:
            hosts = rsp.get_all_hosts()
            # get builds built within one day by default
            age = 1
            builds = rsp.get_all_filtered_by_age(int(age), True)
            self.render("home.html", hosts=hosts, builds=builds)

        rsp.disconnect()
    def get(self, *args):
        rsp = RedisPubSub()
        length = len(args)
        host = args[0]
        format = args[length - 1]
        data = []

        if (length == 2):
            data = rsp.get_jobs(host)
        elif (length == 3):
            job = args[1]
            data = rsp.get_builds(host, job)
        elif (length == 4):
            job = args[1]
            build = args[2]
            data = rsp.get_build(host, job, build)

        if (format == "json"):
            self.write(json_encode(data))
        elif (format == "html"):
            self.write("HTML format not support.")

        rsp.disconnect()
    def get(self, *args):
        rsp = RedisPubSub()
        length = len(args)
        host = args[0]
        format = args[length-1]
        data = []

        if (length == 2):
            data = rsp.get_jobs(host)
        elif (length == 3):
            job = args[1]
            data = rsp.get_builds(host, job)
        elif (length == 4):
            job = args[1]
            build = args[2]
            data = rsp.get_build(host, job, build)

        if (format == "json"):
            self.write(json_encode(data))
        elif (format == "html"):
            self.write("HTML format not support.")

        rsp.disconnect()
Beispiel #16
0
#!/home/y/bin/python2.6
import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '../lib'))
from redisPubSub import RedisPubSub
import threading


rsp = RedisPubSub()

def sub_listener(channel_name):
    print("subscribe to "+channel_name)
    rsp.psubscribe(channel_name)

    #print "there are active channels:"
    #print rsp.count_active_channels("*")

    for message in rsp.listen():
      print message



def main():
    channel_name = raw_input("Enter channel name you want to subscribe: ")

    redisThread = threading.Thread(target=sub_listener(channel_name))
    redisThread.daemon = True
    redisThread.start()


if __name__ == "__main__":
    main()
class RedisPubSubThread(threading.Thread):
    """
    subscribes to a redis pubsub channel and routes
    messages to subscribers

    messages have this format
    {'channel': ..., 'data': ...}
    """
    def __init__(self, redis_hostname, redis_port, redis_password=None):
        threading.Thread.__init__(self)

        self.pubsub = RedisPubSub(redis_hostname, redis_port)
        self.subscriptions = collections.defaultdict()

        # subscribe to dummy channel to make the thread alive
        self.pubsub.subscribe("dummy")
        print self.pubsub

    def subscribe(self, channel, callback):
        print "%s subscribe %s" % (self.pubsub, channel)
        self.pubsub.psubscribe(channel)
        self.subscriptions[channel] = callback

    def unsubscribe(self, channel):
        print "%s unsubscribe %s" % (self.pubsub, channel)
        self.pubsub.punsubscribe(channel)
        del self.subscriptions[channel]

    def disconnect(self):
        self.pubsub.unsubscribe("dummy")
        self.pubsub.disconnect()

    def notify(self, channel, data):
        print "notify callback..."
        #while True:
        print self.subscriptions
        try:
            cb = self.subscriptions[channel]
        except IndexError:
            #break
            pass

        if isinstance(cb, (weakref.ref, )):
            cb = cb()
        if cb is not None:
            cb(data, channel)

    def run(self):
        try:
            for message in self.pubsub.listen():
                print message
                if message['type'] == 'message' or message[
                        'type'] == 'pmessage':
                    self.notify(message['channel'], message['data'])
        except Exception as e:
            print e
class RedisPubSubThread(threading.Thread):
    """
    subscribes to a redis pubsub channel and routes
    messages to subscribers

    messages have this format
    {'channel': ..., 'data': ...}
    """

    def __init__(self, redis_hostname, redis_port, redis_password=None):
        threading.Thread.__init__(self)

        self.pubsub = RedisPubSub(
            redis_hostname,
            redis_port
        )
        self.subscriptions = collections.defaultdict()

        # subscribe to dummy channel to make the thread alive
        self.pubsub.subscribe("dummy")
        print self.pubsub

    def subscribe(self, channel, callback):
        print "%s subscribe %s" %(self.pubsub, channel)
        self.pubsub.psubscribe(channel)
        self.subscriptions[channel] = callback

    def unsubscribe(self, channel):
        print "%s unsubscribe %s" %(self.pubsub, channel)
        self.pubsub.punsubscribe(channel)
        del self.subscriptions[channel]

    def disconnect(self):
        self.pubsub.unsubscribe("dummy")
        self.pubsub.disconnect()

    def notify(self, channel, data):
        print "notify callback..."
        #while True:
        print self.subscriptions
        try:
            cb = self.subscriptions[channel]
        except IndexError:
            #break
            pass

        if isinstance(cb, (weakref.ref,)):
            cb = cb()
        if cb is not None:
            cb(data, channel)

    def run(self):
        try:
            for message in self.pubsub.listen():
                print message
                if message['type'] == 'message' or message['type'] == 'pmessage':
                    self.notify(message['channel'], message['data'])
        except Exception as e:
            print e