From fd18d9450e4984ebe9b3c4523cef2bdcae3c50a3 Mon Sep 17 00:00:00 2001 From: brb Date: Fri, 14 Jul 2023 14:53:20 +0200 Subject: [PATCH] updated root post to start script execution in background task --- code/app.py | 73 ++++++++++++++++++++--------------------------------- 1 file changed, 27 insertions(+), 46 deletions(-) diff --git a/code/app.py b/code/app.py index afdb33e..0cf09ce 100644 --- a/code/app.py +++ b/code/app.py @@ -1,18 +1,23 @@ import uvicorn -from fastapi import FastAPI, Request, HTTPException +from fastapi import FastAPI, Request, HTTPException, BackgroundTasks from fastapi.responses import HTMLResponse from dotenv import load_dotenv import logging import os -import shutil +import httpx -from utils import clone_repository, execute_runner_script +from utils import clone_repository, execute_script from setup_logging import setup_logging from gitea_request import GiteaRequest app = FastAPI() +async def post_callback(url: str, payload: dict): + async with httpx.AsyncClient() as client: + await client.post(url, json=payload) + + @app.get('/', response_class=HTMLResponse) async def root(): content_str = """ @@ -40,7 +45,7 @@ async def test(request: Request): @app.post('/') -def handle_event(request: GiteaRequest): +def handle_event(request: GiteaRequest, background_tasks: BackgroundTasks): """ Handle event from webhook. """ # extract information commit_msg = request.head_commit['message'] @@ -54,48 +59,23 @@ def handle_event(request: GiteaRequest): ) # clone repository local_repo_dir = clone_repository(repo_url) - # locate runner script - runner_script_path = ( - local_repo_dir / - '.gitea' / - 'gpu_runner' / - 'script.yml' + # # locate runner script + # runner_script_path = ( + # local_repo_dir / + # '.gitea' / + # 'gpu_runner' / + # 'script.yml' + # ) + # if not runner_script_path.exists(): + # raise HTTPException( + # status_code=200, + # detail=f"{runner_script_path} does not exist" + # ) + # queue runner script + background_tasks.add_task( + execute_script, + local_repo_dir ) - if not runner_script_path.exists(): - raise HTTPException( - status_code=200, - detail="no runner_script.yml in repository root" - ) - venv_dir = local_repo_dir / 'venv' - try: - # create virtualenv - os.system(f'virtualenv -p python3.10 {venv_dir}') - logging.info('created venv') - os.system(f'source {venv_dir}/bin/activate') - logging.info('activated venv') - # execute runner script - execute_runner_script(path=runner_script_path) - except: - logging.error('failed running script') - finally: - os.system('deactivate') - logging.info('deactivated venv') - shutil.rmtree(venv_dir) - logging.info('removed venv') - # # TODO: create virtualenv? - # # install requirements - # assert pipeline.requirements is not None - # requirements_path = local_repo_dir / pipeline.requirements - # os.system(f'pip install -r {requirements_path}') - # for cmd in pipeline.script: - # try: - # os.system(cmd) - # except Exception as e: - # raise HTTPException( - # status_code=503, - # detail=f'failed running {cmd}: {e}' - # ) - # send response raise HTTPException( status_code=202, @@ -113,5 +93,6 @@ if __name__ == '__main__': uvicorn.run( app=app, host=listen_ip, - port=listen_port + port=listen_port, + workers=1 )