Exemplo n.º 1
0
def start_zeromq_broker():
    _config = read_config_yaml()
    router_port = _config['broker']['listen_port']
    # bind broker port
    context = zmq.Context()
    socket = context.socket(zmq.ROUTER)
    socket.bind("tcp://*:%s" % router_port)

    logger.debug("Start ZeroMQ broker at %s" % router_port)
    while True:
        try:
            msg = socket.recv_multipart()
            _msg_json = json.loads(msg[1].decode())

            logger.debug("recv msg --> %s" % msg[1].decode())

            # get source type : send-* or recv-*
            src = msg[0].decode()
            forward_dest = _msg_json.get("_dest")

            if src.find("send-") >= 0:
                forward_dest = "recv-" + _msg_json.get("_dest")
            elif src.find("recv-") >= 0:
                forward_dest = "send-" + _msg_json.get("_dest")

            forward_arr = [forward_dest.encode(), msg[1]]
            socket.send_multipart(forward_arr)
        except:
            logger.error(traceback.format_exc())
            continue
Exemplo n.º 2
0
def start_redis(**kwargs):
    from app.utils import read_config_yaml, is_debug

    _config = read_config_yaml()
    debug = is_debug()

    redis_port = _config['redis']['listen_port']
    os.system('redis-server --port %s 1>/dev/null 2>/dev/null' % redis_port)
Exemplo n.º 3
0
def start_FTP_manager():

    _config = read_config_yaml()
    zmq_port = _config['broker']['listen_port']
    port = _config['ftp']['listen_port']

    proxy = MessageQueueProxy(WS_TAG.FTM, router_port=zmq_port)
    proxy.register(FTPAccountEventHandler)
    proxy.listen(background=True)

    manager = FTPManager()
    manager.set_port(port)

    logger.info("This is FTP Manager, listening port %s" % port)
    manager.launch(background=False)
Exemplo n.º 4
0
def start_process_watcher():
    from .mq_events import WatcherEvents
    from .watcher import Watcher
    from app.tools.mq_proxy import WS_TAG, MessageQueueProxy
    from app.utils import is_debug, read_config_yaml

    logger.set_debug(is_debug())
    _config = read_config_yaml()
    router_port = _config['broker']['listen_port']

    proxy = MessageQueueProxy(WS_TAG.MPW, router_port=router_port)
    proxy.register(WatcherEvents)

    logger.info("This is Minecraft Process Watcher.")
    proxy.listen(background=True)

    # start event loop
    watcher = Watcher()
    watcher.launch_loop()
Exemplo n.º 5
0
def start_chaussette():
    from app import app as _app
    from app import logger, proxy
    from app.utils import read_config_yaml, is_debug
    from app.mq_events import WebsocketEventHandler
    from app.controller.global_config import GlobalConfig
    from app.controller.init_main_db import init_database

    from chaussette.backend import _backends
    from chaussette.backend._eventlet import Server as eventlet_server
    from chaussette.server import make_server

    _config = read_config_yaml()
    debug = is_debug()

    # variables
    host = _config['server']['host']
    port = int(_config['server']['listen_port'])
    use_reloader = _config['server']['use_reloader']
    circusd_end_port = _config['circus']['end_port']
    redis_port = _config['redis']['listen_port']
    zmq_port = _config['broker']['listen_port']

    logger.set_debug(debug)
    _app.config["_circusd_end_port"] = circusd_end_port
    _app.config["_zmq_port"] = zmq_port
    _app.config["_debug"] = debug

    logger.info("This is Main Server (%s)" % os.getpid())

    def init_directory():
        gc = GlobalConfig.getInstance()
        dirs = [
            gc.get("base_dir"),
            gc.get("uploads_dir"),
            gc.get("files_dir"),
            gc.get("servers_dir"),
            gc.get("lib_bin_dir"),
            gc.get("sqlite_dir"),
            # it's totally useless to store a directory's name into database
            # why not just name it?
            # 2017-2-7
        ]

        for item in dirs:
            if not os.path.isdir(item):
                os.makedirs(item)

    def init_mq_proxy():
        proxy.register(WebsocketEventHandler)
        proxy.listen(background=True)

    def wrap_socketio_server():
        import socketio
        from websocket_server.ws_conn import WSConnections

        mgr = socketio.RedisManager("redis://localhost:%s/0" % redis_port)
        sio = socketio.Server(client_manager=mgr, async_mode='eventlet')

        #init
        ws = WSConnections.getInstance(sio)
        ws.init_events()
        app = socketio.Middleware(sio, _app)

        return app

    def _make_server():
        try:
            # instill eventlet_server instance to `_backends` dict to bypass the restriction!
            _backends['eventlet'] = eventlet_server

            app = wrap_socketio_server()
            httpd = make_server(app, host=host, port=port, backend='eventlet')

            httpd.serve_forever()
        except KeyboardInterrupt:
            sys.exit(0)

    # init directories
    init_directory()

    # init message queue proxy
    init_mq_proxy()

    # init database
    init_database(logger=logger)

    if use_reloader:
        try:
            from werkzeug.serving import run_with_reloader
        except ImportError:
            logger.info("Reloader requires Werkzeug: "
                        "'pip install werkzeug'")
            sys.exit(0)
        run_with_reloader(_make_server)
    else:
        _make_server()