Beispiel #1
0
def dev_libvirt():
    db = SessionLocal()
    nodes = db.query(NodeModel).all()
    for node in nodes:
        con = VirtManager(node)
        # con: libvirt.virConnect
        pprint(dir(con.node))
        for dom in con.node.listAllDomains():
            dom: libvirt.virDomain
            # dom.getHostname()
            "https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainMetadataType"
            dom.setMetadata(key="virty", metadata="<data user='******'/>", type=2, uri="aaaa")
Beispiel #2
0
def dev_patch_node_role():
    db = SessionLocal()
    model = TaskModel(request={
        "node_name": "shiori",
        "role_name": "libvirt"
    })
    patch_node_role(db=db, model=model)
Beispiel #3
0
def lost_task_cheack():
    db = SessionLocal()

    lost_tasks = db.query(TaskModel).filter(TaskModel.status != "finish")
    lost_tasks.update(
        {
            TaskModel.status: "lost",
            TaskModel.message: "Workerが再起動したので実行されませんでした"
        },
        synchronize_session=False)

    lost_tasks = lost_tasks.all()

    if len(lost_tasks) != 0:
        logger.error(f'{len(lost_tasks)}件のタスクが実行されませんでした')

    db.commit()
Beispiel #4
0
async def db_session_middleware(request: Request, call_next):
    # 処理前のログ記述
    start_time = time.time()

    # セッションを各リクエストに載せる
    request.state.db = SessionLocal()

    # 各関数で処理を行って結果を受け取る
    response = await call_next(request)

    # 処理後のログ
    process_time = (time.time() - start_time) * 1000
    formatted_process_time = '{0:.2f}'.format(process_time)
    logger.info(
        f"{request.method} {request.client.host} {response.status_code} {request.url.path} {formatted_process_time}ms"
    )

    # 結果を返す
    return response
Beispiel #5
0
def dev_domain_define():
    db = SessionLocal()
    update_network_list(db=db, model=TaskModel())
Beispiel #6
0
def dev_update_network_list():
    db = SessionLocal()
    update_network_list(db=db, model=TaskModel())
Beispiel #7
0
def dev_update_storage_list():
    db = SessionLocal()
    update_storage_list(db=db, model=TaskModel())
Beispiel #8
0
def do_task(mode="init"):
    logger.info(f"looking for a {mode} task")
    db = SessionLocal()
    # 指定された状態のタスクを取得
    tasks = db.query(TaskModel)\
        .filter(TaskModel.status==mode)\
        .order_by(desc(TaskModel.post_time))\
        .with_lockmode('update').all()

    if tasks == [] and mode == "wait":
        # 待機中のタスクもないため終了
        logger.info("task not found worker exit")
        return
    elif tasks == [] and mode == "init":
        # 待機中のタスク実施へ
        do_task(mode="wait")
        return

    # 新規タスク処理
    if mode == "init":
        task: TaskModel = tasks[0]
        logger.info(
            f'find tasks: {task.resource}.{task.object}.{task.method} {task.uuid}'
        )
        exec_task(db=db, task=task)
        do_task()
        return

    for task in tasks:
        # 依存先のuuidが実在するかチェック
        try:
            if task.dependence_uuid == None:
                raise Exception()
            depends_task:TaskModel = db.query(TaskModel)\
                .filter(TaskModel.uuid==task.dependence_uuid)\
                .with_lockmode('update').one()
        except:
            task.status = "error"
            task.message = "not found depended task"
            db.merge(task)
            db.commit()
            logger.error(
                f"not found depended task {task.uuid} => {task.dependence_uuid}"
            )
            continue

        # 親タスクが死んだ場合エラーで停止
        if depends_task.status == "error":
            task.status = "error"
            task.message = "depended task faile"
            db.merge(task)
            db.commit()
            logger.error(
                f"depended task faile {task.uuid} => {task.dependence_uuid}")
            continue

        # 親タスクが実行中の場合待機
        elif depends_task.status != "finish":
            logger.info(
                f"wait depended task. {task.uuid} => {task.dependence_uuid}")
            continue

        # 実行可能なタスクは初期化
        elif depends_task.status == "finish" and task.status == "wait":
            task.status = "init"
            db.merge(task)
            db.commit()
            logger.error(
                f"init depended task {task.uuid} => {task.dependence_uuid}")
            do_task()

    do_task()
Beispiel #9
0
def endless_eight():
    logger.info("Worker起動")
    db = SessionLocal()
    while True:
        query = db.query(TaskModel)
        query = query.filter(TaskModel.status == "init")
        query = query.order_by(desc(TaskModel.post_time))
        tasks = query.all()
        if tasks == []:
            sleep(3)
            continue

        # 開始処理
        task: TaskModel = tasks[0]
        logger.info(
            f'タスク開始: {task.resource}.{task.object}.{task.method} {task.uuid}')
        task.status = "start"
        db.merge(task)
        db.commit()

        # Model to Schemas
        task: TaskSelect = TaskSelect().from_orm(task)

        start_time = time()

        try:
            task_swicher(model=task, db=db)
        except Exception as e:
            logger.error(e, exc_info=True)
            task.status = "error"
            task.message = str(e)
        else:
            task.status = "finish"
            task.message = "finish"

        end_time = time()
        task.run_time = end_time - start_time

        logger.info(
            f'タスク終了: {task.resource}.{task.object}.{task.method} {task.uuid} {task.run_time}s'
        )

        # Schemas to Model
        db.merge(TaskModel(**task.dict()))
        db.commit()