mirror of
https://github.com/fastapi-practices/fastapi_best_architecture.git
synced 2025-08-20 16:40:10 +08:00

* Replace APScheduler to Celery task * black format * Add celery to run the script * Update celery usage to README * Update test task * Add celery rabbitmq broker * Fix dockerfiles * Add task interface access authorization * Update celery deploy run * Fix dockerfiles * Fix supervisor conf * Update celery broker default is redis * Force the pro env to use rabbitmq * Update the task interface * Add celery beat README description * Update warning text style * Revoke the default config comment content of the supervisor
38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
from typing import Annotated
|
|
|
|
from fastapi import APIRouter, Path, Body
|
|
|
|
from backend.app.common.jwt import DependsJwtAuth
|
|
from backend.app.common.rbac import DependsRBAC
|
|
from backend.app.common.response.response_code import CustomResponseCode
|
|
from backend.app.common.response.response_schema import response_base
|
|
from backend.app.services.task_service import TaskService
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get('', summary='获取所有可执行任务模块', dependencies=[DependsJwtAuth])
|
|
async def get_all_tasks():
|
|
tasks = TaskService.gets()
|
|
return await response_base.success(data=tasks)
|
|
|
|
|
|
@router.get('/{pk}', summary='获取任务结果', dependencies=[DependsJwtAuth])
|
|
async def get_task_result(pk: str = Path(description='任务ID')):
|
|
task = TaskService.get(pk)
|
|
if not task:
|
|
return await response_base.fail(res=CustomResponseCode.HTTP_204, data=pk)
|
|
return await response_base.success(data=task.result)
|
|
|
|
|
|
@router.post('/{module}', summary='执行任务', dependencies=[DependsRBAC])
|
|
async def run_task(
|
|
module: Annotated[str, Path(description='任务模块')],
|
|
args: Annotated[list | None, Body()] = None,
|
|
kwargs: Annotated[dict | None, Body()] = None,
|
|
):
|
|
task = TaskService.run(module=module, args=args, kwargs=kwargs)
|
|
return await response_base.success(data=task.result)
|