Ejemplo n.º 1
0
def pub():
    import simplejson as json
    from juggernaut import Juggernaut
    jug = Juggernaut()
    d = {'foo': 'bar'}

    jug.publish('switch', json.dumps(d))
Ejemplo n.º 2
0
class JuggernautEventedStruct(EventedValidatingStruct):
    __EVENT_DISPATCHER__ = EventDispatcher

    def __init__(self, **kwargs):
        super(JuggernautEventedStruct, self).__init__(**kwargs)
        self.__JUGGERNAUT__ = Juggernaut()

    def on(self, signal_name, callback):
        self.__EVENT_DISPATCHER__.subscribe(signal_name, callback, self)

    def emit(self, signal_name, **kwargs):
        self.__JUGGERNAUT__.publish(signal_name, kwargs)
Ejemplo n.º 3
0
class EventDispatcher(object):
    __SIGNALS__ = SignalStorage

    def __init__(self):
        self.__JUGGERNAUT__ = Juggernaut()

    @classmethod
    def subscribe(cls, signal_name, callback, caller):
        try:
            signal_model = cls.__SIGNALS__.get(signal_name=signal_name)
        except NotFoundException:
            signal_model = cls.__SIGNALS__(signal_name=signal_name)
            signal_model.callback_list = []
        callback_model = Callback(
                           caller_id=caller._id,
                           caller_class=caller.__class__.__name__,
                           callback=callback.__name__
                         )
        callback_model.save()
        signal_model.callback_list.append(callback_model._id)
        signal_model.save()

    def main_loop(self):
        for signal_name, data in self.__JUGGERNAUT__.subscribe_listen():
            for callback_model in self.__SIGNALS__.callbacks(signal_name):
                caller_class = globals().get(callback_model.caller_class)
                caller_object = caller_class.get(_id=callback_model.caller_id)
                getattr(caller_object, callback_model.callback)(**data)
Ejemplo n.º 4
0
from datetime import datetime
from flask import Flask, request, url_for, redirect, g, session, flash, \
     abort, render_template
from flask_sqlalchemy import SQLAlchemy
from flask_oauthlib.client import OAuth
from juggernaut import Juggernaut


app = Flask(__name__)
app.config.from_pyfile('config.cfg')
db = SQLAlchemy(app)
oauth = OAuth()
jug = Juggernaut()

facebook = oauth.remote_app('facebook',
    base_url='https://graph.facebook.com/',
    request_token_url=None,
    access_token_url='/oauth/access_token',
    authorize_url='https://www.facebook.com/dialog/oauth',
    consumer_key='188477911223606',
    consumer_secret='621413ddea2bcc5b2e83d42fc40495de',
    request_token_params={'scope': 'email'}
)


def url_for_other_page(page):
    args = request.view_args.copy()
    args['page'] = page
    return url_for(request.endpoint, **args)
app.jinja_env.globals['url_for_other_page'] = url_for_other_page
Ejemplo n.º 5
0
 def process_IN_MODIFY(self, event):
     if any([True for ext in EXTENSIONS if event.pathname.endswith(ext)]):
         msg = "Modified: %s" % (event.pathname)
         print msg
         jug = Juggernaut(REDIS)
         jug.publish("channel1", msg)
Ejemplo n.º 6
0
Archivo: 1.py Proyecto: woto/EPC
from juggernaut import Juggernaut
import pdb

pdb.set_trace()
jug = Juggernaut()

jug.publish('chat', '123')
Ejemplo n.º 7
0
def pull_marta_realtime_data():
    logger = pull_marta_realtime_data.get_logger()
    logger.info("Starting MARTA realtime pull")
    stale = set(Bus.objects.filter(is_stale=False).values_list("id"))

    try:
        data = json.loads(requests.get(app.config["MARTA_ENDPOINT"]).content)
        socket = Juggernaut()
        queue = app.config.get("MARTA_SOCKET_QUEUE", "marta")
        push = []

        for info in data:
            defaults = {
                "id": info["VEHICLE"],
                "direction": info["DIRECTION"],
                "route": info["ROUTE"],
                "location": (float(info["LATITUDE"]), float(info["LONGITUDE"])),
                "status_time": parse_date(info["MSGTIME"]),
                "timepoint": info["TIMEPOINT"],
                "stop_id": info.get("STOPID", ""),
                "is_stale": False,
            }

            try:
                defaults["adherence"] = int(info["ADHERENCE"])
            except (ValueError, KeyError):
                defaults["adherence"] = 0

            bus, created = Bus.objects.get_or_create(id=defaults["id"], defaults=defaults)

            if not created:
                stale.discard(bus.id)
                current = defaults["status_time"] > bus.status_time
                updated = bus.update_maybe(**defaults)

                if updated and current:
                    bus.save()
                    push.append(bus.to_json())
            else:
                push.append(bus.to_json())

        # Notify
        try:
            # Push new updates
            if push:
                logger.info("Publishing %s bus updates to queue %s" % (len(push), queue))
                socket.publish(queue, push)

            # Notify of staleness
            if stale:
                stale = list(stale)
                logger.info("Publishing %s stale bus notices to %s/stale" % (len(stale), queue))
                socket.publish("%s/stale" % queue, stale)
                Bus.objects.filter(id__in=stale).update(set__is_stale=True)

        except:
            logger.exception("Can't push realtime MARTA notifications")
    except:
        logger.exception("Failed to pull MARTA realtime data")
    else:
        logger.info("MARTA realtime pull finished")
def publish():
    jug = Juggernaut()
    jug.publish('channel', 'The message')
Ejemplo n.º 9
0
def publish():
    jug = Juggernaut()
    jug.publish('channel', 'The message')
Ejemplo n.º 10
0
Archivo: 2.py Proyecto: woto/EPC
import pdb
from juggernaut import Juggernaut
import redis

r = redis.Redis('192.168.2.3')
jug = Juggernaut(r)
pdb.set_trace()
for event, data in jug.subscribe_listen():
    print event
    if event == 'subscribe':
        print 'subscribe'
    elif event == 'unsubscribe':
        print 'unsubscribe'
    elif event == 'custom':
        print 'custom_event'
Ejemplo n.º 11
0
 def __init__(self, **kwargs):
     super(JuggernautEventedStruct, self).__init__(**kwargs)
     self.__JUGGERNAUT__ = Juggernaut()
Ejemplo n.º 12
0
 def __init__(self):
     self.__JUGGERNAUT__ = Juggernaut()