예제 #1
0
            pass
        else:
            self.conn.poll()
            # It's for postgres < 9, without payload, so noftifies are just ONE
            # trigger
            self.fetch_events()
            while self.conn.notifies:
                self.conn.notifies.pop()

    def on_event(self, evt):
        raise NotImplementedError()


if __name__ == '__main__':

    class TestWorker(PgEventWorker):
        def on_event(self, evt):
            print "###### Event", self.pid, evt.channel, evt.payload

    conf = {
        "dsn": "dbname=pubsub",
        "channel": "test",
    }

    specs = [
        (TestWorker, 30, "worker", {}, "listener"),
    ]

    arbiter = Arbiter(conf, specs)
    arbiter.run()
def _run_server(args):
    logger.info("Serving on port %d..." % args.port)

    # loading the rbr to get the workers/arbiters
    from redbarrel.dsl import build_ast
    from redbarrel.dsl.runners import resolve_runner

    workers = {}
    arbiters = {}

    for path in args.path:
        with open(path) as f:
            ast = build_ast(f.read())

        for definition in ast:
            type_ = definition.type
            if type_ not in ('worker', 'arbiter'):
                continue

            name = definition[1]
            logger.info('Loading %s %r' % (type_, name))
            runner = resolve_runner(definition[2])
            if type_ == 'worker':
                workers[name] = runner
            else:
                arbiters[name] = runner

    # default arbiters and workers
    from redbarrel.server.arbiters import ARBITERS
    arbiters.update(ARBITERS)

    from redbarrel.server.workers import WORKERS
    workers.update(WORKERS)

    # now loading the workers and arbiters, given a config
    specs = []
    from pistil.arbiter import Arbiter

    conf = {
        "address": ("127.0.0.1", args.port),
        "debug": True,
        "memory": True,
        "num_workers": 1,  #args.workers,
        "path": args.path,
        "timeout": 9000  # XXXX
    }

    for name, num in args.workers:
        # is it an arbiter or a worker ?
        if name in arbiters:
            klass = arbiters[name]
            type_ = 'supervisor'
        elif name in workers:
            klass = workers[name]
            type_ = 'worker'
        else:
            raise ValueError(name)

        timeout = 90000  # XXX to be defined
        specs.append((klass, timeout, type_, {}, name))
        # XXX conf ?
        #

    arbiter = Arbiter(conf, specs)
    arbiter.run()