예제 #1
0
def init():
    """
    入口函数
    :return:
    """
    tasks = TaskQueueModel.select().limit(1)
    for task in tasks:
        handle_task(task)
    return TaskQueueModel.select().count()
예제 #2
0
파일: task_execute.py 프로젝트: Chanran/app
def init():
    """
    入口函数
    :return:
    """
    tasks = TaskQueueModel.select().limit(1)
    for task in tasks:
        handle_task(task)
    return TaskQueueModel.select().count()
예제 #3
0
파일: user.py 프로젝트: richardy2012/app
def deployment_app():
    """
    重新部署应用
    :return:
    """

    user_id = get_login_user_id()
    app_id = request.forms.app_id
    # 防止越权操作
    AppModel.select().where(AppModel.user_id == user_id).get()

    if TaskQueueModel.select().where(TaskQueueModel.app_id == app_id, TaskQueueModel.command_code == 3).count():
        return {"code": -1}

    obj = TaskQueueModel(app_id=app_id, user_id=user_id, command_code=3, command_content='{}', create_time=datetime.datetime.now())
    obj.save()

    return {"code": 0}
예제 #4
0
def deployment_app():
    """
    重新部署应用
    :return:
    """

    user_id = get_login_user_id()
    app_id = request.forms.app_id
    # 防止越权操作
    AppModel.select().where(AppModel.user_id == user_id).get()

    if TaskQueueModel.select().where(TaskQueueModel.app_id == app_id,
                                     TaskQueueModel.command_code == 3).count():
        return {"code": -1}

    obj = TaskQueueModel(app_id=app_id,
                         user_id=user_id,
                         command_code=3,
                         command_content='{}',
                         create_time=datetime.datetime.now())
    obj.save()

    return {"code": 0}
예제 #5
0
def handle_task(task):
    """
    处理每一个任务
    :param task: 任务对象
    :return:
    """
    if task.command_code == 1:
        # 添加一个容器
        r, obj = add_one_container(task)

        if r:
            # 保存容器服务器申请到的容器
            dao = AppContainModel(
                app_id=task.app_id,
                host=obj['host'],
                port=obj['r']['result']['port'],
                container_id=obj['r']['result']['containerId'],
                create_time=datetime.datetime.now(),
                api_url=obj['api_url'])
            dao.save()
            login_log(
                "common", "申请容器成功,触发应用id:%d,申请到的容器域名:%s,端口:%d,id:%s" %
                (task.app_id, obj['host'], obj['r']['result']['port'],
                 obj['r']['result']['containerId']))
        else:
            login_log("error", "申请容器失败,触发应用id:%d" % task.app_id)
    elif task.command_code == 2:
        # 移除一个容器
        remove_some_container(task, 1)
    elif task.command_code == 3:
        # 移除所有容器
        count = AppContainModel.select().where(
            AppContainModel.app_id == task.app_id).count()
        remove_some_container(task, count)
    else:
        # 未定义任务码
        login_log("error",
                  "未定义任务码:%d,触发应用id:%d" % (task.command_code, task.app_id))

    build_nginx_config(task.app_id)

    # 删除任务队列
    dao = TaskQueueModel.delete().where(TaskQueueModel.id == task.id)
    dao.execute()
예제 #6
0
파일: task_execute.py 프로젝트: Chanran/app
def handle_task(task):
    """
    处理每一个任务
    :param task: 任务对象
    :return:
    """
    if task.command_code == 1:
        # 添加一个容器
        r, obj = add_one_container(task)

        if r:
            # 保存容器服务器申请到的容器
            dao = AppContainModel(app_id=task.app_id, host=obj['host'], port=obj['r']['result']['port'],
                                  container_id=obj['r']['result']['containerId'], create_time=datetime.datetime.now(),
                                  api_url=obj['api_url'])
            dao.save()
            login_log("common", "申请容器成功,触发应用id:%d,申请到的容器域名:%s,端口:%d,id:%s" % (task.app_id,
                                                                              obj['host'], obj['r']['result']['port'],
                                                                              obj['r']['result']['containerId']))
        else:
            login_log("error", "申请容器失败,触发应用id:%d"%task.app_id)
    elif task.command_code == 2:
        # 移除一个容器
        remove_some_container(task, 1)
    elif task.command_code == 3:
        # 移除所有容器
        count = AppContainModel.select().where(AppContainModel.app_id == task.app_id).count()
        remove_some_container(task, count)
    else:
        # 未定义任务码
        login_log("error", "未定义任务码:%d,触发应用id:%d" % (task.command_code, task.app_id))

    build_nginx_config(task.app_id)

    # 删除任务队列
    dao = TaskQueueModel.delete().where(TaskQueueModel.id == task.id)
    dao.execute()
예제 #7
0
def handle_app(app):
    """
    处理每一个APP
    :param app: app对象
    :return:
    """
    container_nums = AppContainModel.select().where(
        AppContainModel.app_id == app.id).count()

    cpu, memory = get_app_avg_message(app.id)

    # 判断是否需要增加容器
    if container_nums < app.min_container_number:
        add_sign = 1
    else:
        if cpu > 5.0 and memory > 95.5 and container_nums < app.max_container_number:
            add_sign = 1
        else:
            add_sign = 0

    # 判断是否需要减少容器
    if container_nums > app.max_container_number:
        add_sign = 0
        reduce_sign = -1
    else:
        if cpu < 1.5 and memory < 90 and container_nums > app.min_container_number:
            reduce_sign = -1
        else:
            reduce_sign = 0

    if container_nums >= app.max_container_number:
        add_sign = 0

    if container_nums <= app.min_container_number:
        reduce_sign = 0

    sign = add_sign + reduce_sign
    if sign == 0:
        return None

    obj = TaskQueueModel(user_id=0,
                         app_id=app.id,
                         create_time=datetime.datetime.now())
    obj.command_content = '{}'
    if sign == 1:
        obj.command_code = 1  # 增加容器
    elif sign == -1:
        obj.command_code = 2  # 减少容器

    # 判断命令有没有重复
    if TaskQueueModel.select().where(
            TaskQueueModel.user_id == obj.user_id,
            TaskQueueModel.app_id == obj.app_id,
            TaskQueueModel.command_code == obj.command_code).count():
        return None

    # 删除同应用不同操作
    dao = TaskQueueModel.delete().where(TaskQueueModel.user_id == obj.user_id,
                                        TaskQueueModel.app_id == obj.app_id)
    dao.execute()

    obj.save()
예제 #8
0
def handle_app(app):
    """
    处理每一个APP
    :param app: app对象
    :return:
    """
    container_nums = AppContainModel.select().where(AppContainModel.app_id == app.id).count()

    cpu, memory = get_app_avg_message(app.id)

    # 判断是否需要增加容器
    if container_nums < app.min_container_number:
        add_sign = 1
    else:
        if cpu > 5.0 and memory > 95.5 and container_nums < app.max_container_number:
            add_sign = 1
        else:
            add_sign = 0

    # 判断是否需要减少容器
    if container_nums > app.max_container_number:
        add_sign = 0
        reduce_sign = -1
    else:
        if cpu < 1.5 and memory < 90 and container_nums > app.min_container_number:
            reduce_sign = -1
        else:
            reduce_sign = 0

    if container_nums >= app.max_container_number:
        add_sign = 0

    if container_nums <= app.min_container_number:
        reduce_sign = 0

    sign = add_sign + reduce_sign
    if sign == 0:
        return None

    obj = TaskQueueModel(user_id=0, app_id=app.id, create_time=datetime.datetime.now())
    obj.command_content = "{}"
    if sign == 1:
        obj.command_code = 1  # 增加容器
    elif sign == -1:
        obj.command_code = 2  # 减少容器

    # 判断命令有没有重复
    if (
        TaskQueueModel.select()
        .where(
            TaskQueueModel.user_id == obj.user_id,
            TaskQueueModel.app_id == obj.app_id,
            TaskQueueModel.command_code == obj.command_code,
        )
        .count()
    ):
        return None

    # 删除同应用不同操作
    dao = TaskQueueModel.delete().where(TaskQueueModel.user_id == obj.user_id, TaskQueueModel.app_id == obj.app_id)
    dao.execute()

    obj.save()