Пример #1
0
def need_to_restart():
    """ auto_restart=True而且长时间没有touchdb的
    :return:
    """
    # 超时没有touch db的
    processes = Process.select().where(
        Process.status == STATUS.RUNNING, Process.auto_restart == 1,
        Process.timeout_timestamp < int(time.time()))
    for process in processes:
        if Process.update(status=STATUS.EXITED) \
                .where(Process.id == process.id,
                       Process.status == STATUS.RUNNING,
                       Process.auto_restart == 1,
                       Process.timeout_timestamp < int(time.time())) \
                .execute() == 1:
            logger.info("重启进程%s" % process.id)
            restart_process(program_id=process.id)

    # 处于exited状态的
    processes = Process.select().where(Process.status == STATUS.EXITED,
                                       Process.auto_restart == 1)
    for process in processes:
        if Process.update(status=STATUS.EXITED) \
                .where(Process.id == process.id,
                       Process.status == STATUS.EXITED,
                       Process.auto_restart == 1) \
                .execute() == 1:
            logger.info("重启进程%s" % process.id)
            restart_process(program_id=process.id)
Пример #2
0
def need_to_reset_status():
    cnt = Process.update(status=STATUS.RUNNING) \
        .where(Process.status == STATUS.STOPPING,
               Process.update_time + 60 < int(time.time())) \
        .execute()
    if cnt:
        logger.info("重置了%s个进程的状态为RUNNING" % cnt)

    cnt = Process.update(status=STATUS.STOPPED) \
        .where(Process.status == STATUS.STARTING,
               Process.update_time + 60 < int(time.time())) \
        .execute()
    if cnt:
        logger.info("重置了%s个进程的状态为STOPPED" % cnt)
Пример #3
0
def touch_db(program_id, touch_timeout):
    logger.info("进程%s运行中..." % program_id)
    timeout_timestamp = int(time.time() + touch_timeout)
    # 如果程序处于STARTING或STOPPING状态,无需touch db
    try:
        process = ProcessModel.select() \
            .where(ProcessModel.id == program_id) \
            .get()
    except DoesNotExist:
        logger.warning("touch_db失败,没有这条记录,%s可能已停止" % program_id)
        return False

    if process.status in (STATUS.STARTING, STATUS.STOPPING):
        return True

    # noinspection PyBroadException
    try:
        ret = ProcessModel.update(timeout_timestamp=timeout_timestamp) \
            .where(ProcessModel.id == program_id,
                   ProcessModel.status == STATUS.RUNNING) \
            .execute()
    except Exception:
        logger.exception("touch_db异常")
        return False

    if ret == 0:
        logger.warning("touch_db失败,没有这条记录,%s可能已停止" % program_id)
        return False

    return True
Пример #4
0
def update_program(program_id, **fields):
    """ 更新一条进程
    :param program_id:
    :return:
        True  - 更新成功
        False - 更新失败
    """
    try:
        Process.select().where(Process.id == program_id).get()
    except DoesNotExist:
        msg = "程序id=%s的配置不存在" % program_id
        logger.error(msg)
        raise exceptions.NoConfigException(msg)

    Process.update(**fields) \
        .where(Process.id == program_id) \
        .execute()
    return True
Пример #5
0
def change_status(program_id, from_status, to_status):
    """ 修改程序状态
    :param program_id: 程序ID
    :param from_status: 修改前的状态元组。程序状态不在from_status中时,修改失败
    :param to_status: 修改后的状态
    :return:
        True  - 修改成功
        False - 修改失败
    """
    if isinstance(from_status, int):
        from_status = (from_status, )

    fields = {"status": to_status, "update_time": tools.get_now_time()}
    try:
        process = Process.select().where(Process.id == program_id).get()
    except DoesNotExist:
        logger.error("程序%s不存在" % program_id)
        return False
    except DatabaseError:
        logger.exception("查询进程%s时,数据库发生异常" % program_id)
        return False

    if process.status not in from_status:
        logger.error("程序%s的状态%s不是%s" %
                     (program_id, process.status, from_status))
        return False

    try:
        ret_code = Process.update(fields) \
            .where(Process.id == program_id,
                   Process.status << from_status).execute()
    except DatabaseError:
        logger.exception("程序%s的状态%s=>%s更新时,数据库发生异常" %
                         (program_id, from_status, to_status))
        return False

    if ret_code == 0:
        logger.warning("程序%s的状态%s=>%s更新失败,ID或状态不匹配" %
                       (program_id, from_status, to_status))
        return False

    return True