Exemple #1
0
class Server(object):

    def __init__(self, name, run_config):
        self.stdin_path         = run_config.get("STDIN", "/dev/null")
        self.stdout_path        = run_config.get("STDOUT", "/dev/null")
        self.stderr_path        = run_config.get("STDERR", "/dev/null")
        self.pidfile_path       = run_config.get("PID_FILE", "/var/run/%s.pid" % name)
        self.pidfile_timeout    = run_config.get("PID_FILE_TIMEOUT", 3)

        self.logfile    = run_config['LOG_FILE']
        self.loglevel   = run_config.get('LOG_LEVEL', 'info')
        self.consolelog = run_config.get('CONSOLE_LOG', False)
        self.sc         = StreamController(self.read_conf(run_config['CONF_FILE_PATH']),
                                hb_cb=self.heartbeat_cb)

    def read_conf(self, conf_path):
        f = open(conf_path, 'r')
        conf = f.read()
        f.close()
        conf = remove_json_commets(conf)
        return json.loads(conf)

    def heartbeat_cb(self, infos):
        for s in infos:
            # pprint(s)
            pass

    def init_signal_handler(self, logger=None):
        def sig_handler(signum, frame):
            if logger:
                logger.warning("Recv signal %s %s" % (signum, frame))
            self.sc.stop()

        signal.signal(signal.SIGHUP, sig_handler)
        signal.signal(signal.SIGINT, sig_handler)
        signal.signal(signal.SIGQUIT, sig_handler)
        signal.signal(signal.SIGTERM, sig_handler)

    def run(self):
        logger = init_logger(self.loglevel, self.logfile, self.consolelog)

        self.init_signal_handler(logger)

        try:
            self.sc.start()
        except Exception, e:
            logger.error("Error Occur %s " % str(e))
            [logger.error(str(s)) for s in traceback.format_exc().split('\n')]
Exemple #2
0
class Server(object):
    def __init__(self, name, run_config):
        self.stdin_path = run_config.get("STDIN", "/dev/null")
        self.stdout_path = run_config.get("STDOUT", "/dev/null")
        self.stderr_path = run_config.get("STDERR", "/dev/null")
        self.pidfile_path = run_config.get("PID_FILE",
                                           "/var/run/%s.pid" % name)
        self.pidfile_timeout = run_config.get("PID_FILE_TIMEOUT", 3)

        logger.g_logger_path = run_config['LOG_PATH']
        logger.g_logger_level = run_config.get('LOG_LEVEL', 'info')
        logger.g_console_log = run_config.get('CONSOLE_LOG', False)
        logger.g_logger_name = name

        conf = self.read_conf(run_config['CONF_FILE_PATH'])
        if not conf:
            raise Exception("can not get stream config!!!!")
        poll_time = run_config.get('STREAM_CONTROLLER_POLL_TIME', 1)
        self.stream_ctrl = StreamController(conf, poll_time=poll_time)

        logger.g_remote_logger_template = conf["RemoteLogger"]

    def read_conf(self, conf_path):
        f = open(conf_path, 'r')
        conf = f.read()
        f.close()
        conf = remove_json_commets(conf)
        return json.loads(conf)

    def init_signal_handler(self, logger=None):
        def sig_handler(signum, frame):
            if logger:
                logger.warning("Recv signal %s %s" % (signum, frame))
            self.stream_ctrl.stop()

        signal.signal(signal.SIGHUP, sig_handler)
        signal.signal(signal.SIGINT, sig_handler)
        signal.signal(signal.SIGQUIT, sig_handler)
        signal.signal(signal.SIGTERM, sig_handler)

    def run(self):
        logger.init()
        self.init_signal_handler(logger)
        self.stream_ctrl.start()
        logger.fini()