def __init__(self): self.tabfile = cfg.config().get("WORKER",'crontab_file') self.log_dir = cfg.config().get("DEFAULT",'job_log_dir') open(self.tabfile, 'a').close() # ensure crontab file exist self.cron = CronTab(tabfile=self.tabfile) self._logger = logging.getLogger("game.cron") self._stop_flag_timeout = 5 self.stop_flag = False self._received_signal = False
def add_rotating_file_handler(log, name): path = cfg.config().get("DEFAULT", 'job_log_dir') if not os.path.exists(path): os.mkdir(path) file_handler = handlers.RotatingFileHandler( os.path.join(path, name), mode='a', maxBytes=int(cfg.config().getint("DEFAULT", 'log_size')), backupCount=int(cfg.config().get("DEFAULT", 'log_num')), # if backupCount is 0, rollover not happend. ) formatter = logging.Formatter(FMT) file_handler.setFormatter(formatter) log.addHandler(file_handler)
def main(): parser = argparse.ArgumentParser() parser.add_argument('-p', '--port', default=8000, type=int, help="run on the give port") parser.add_argument('-a', '--address', default="127.0.0.1", type=str, help="run on the give address") parser.add_argument('-c', '--config', help='use specific config file') args = parser.parse_args() if args.config: cfg.load_config(args.config) else: cfg.load_config() log.setup() mode = cfg.config().getboolean("DEFAULT","debug") mongo_api.init_mongo_connection() tornado.escape.json_encode = json_encode app.run_api(mode, args.port, args.address)
def setup(): logging.basicConfig( format=(FMT) ) root_logger = logging.getLogger() if cfg.config().getboolean("DEFAULT", 'debug'): root_logger.setLevel(logging.DEBUG) else: root_logger.setLevel(logging.INFO) for l in ['requests', 'raven', 'urllib3', 'sh']: log = logging.getLogger(l) log.setLevel(logging.WARN) for l in ['peewee', 'huey', 'PIL']: log = logging.getLogger(l) log.setLevel(logging.INFO)
def do_alembic_command(cmd, database, *args, **kwargs): if database == 'gamecenter': folder_name = 'alembic' db_url = cfg.config().get('DB', 'sql_connection') else: raise SystemExit("database option must be one of gamecenter") configx = alembic_config.Config( os.path.join(os.path.abspath(os.path.dirname(db.__file__)), folder_name, 'alembic.ini')) configx.set_main_option('script_location', 'gamecenter.db:%s' % folder_name) configx.set_main_option('sqlalchemy.url', db_url) try: getattr(alembic_command, cmd)(configx, *args, **kwargs) except alembic_util.CommandError as e: alembic_util.err(str(e))
def init_mongo_connection(): host = cfg.config().get("MONGODB", 'mongodb_url') LOG.debug('start mongodb @ %(host)s' % {"host": host}) connect("gamecenter", "gamecenter", host=host)
def create_sdk(self): host = cfg.config().get("SDK", "host") secret_key = cfg.config().get('SDK', 'cp_game_key') return create_sdk(host, secret_key)
def delete_crontab(): tabfile = cfg.config().get("WORKER", 'crontab_file') if os.path.exists(tabfile): os.remove(tabfile)
def get_session(): maker = base.maker(cfg.config().get('DB', 'sql_connection'), False) return maker()
def delete_old_game_logs(): key_hours = cfg.config().getint("OLD_DATA", "keep_hour") api.init_mongo_connection() dead_line = int(time.time()) - 3600 * key_hours api.delete_game_logs(dead_line)