-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix celery service functions error (#462)
- Loading branch information
Showing
6 changed files
with
68 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,53 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
from celery.exceptions import NotRegistered | ||
from celery.result import AsyncResult | ||
from starlette.concurrency import run_in_threadpool | ||
|
||
from backend.app.task.celery import celery_app | ||
from backend.app.task.schema.task import RunParam | ||
from backend.common.dataclasses import TaskResult | ||
from backend.common.exception.errors import NotFoundError | ||
|
||
|
||
class TaskService: | ||
@staticmethod | ||
def get_list(): | ||
filtered_tasks = [] | ||
tasks = celery_app.tasks | ||
for key, value in tasks.items(): | ||
if not key.startswith('celery.'): | ||
filtered_tasks.append({key, value}) | ||
return filtered_tasks | ||
|
||
@staticmethod | ||
def get(): | ||
return celery_app.current_worker_task | ||
async def get_list(): | ||
registered_tasks = await run_in_threadpool(celery_app.control.inspect().registered) | ||
tasks = list(registered_tasks.values())[0] | ||
return tasks | ||
|
||
@staticmethod | ||
def get_status(uid: str): | ||
def get_detail(*, tid: str): | ||
try: | ||
task_result = AsyncResult(id=uid, app=celery_app) | ||
result = AsyncResult(id=tid, app=celery_app) | ||
except NotRegistered: | ||
raise NotFoundError(msg='任务不存在') | ||
return task_result.status | ||
return TaskResult( | ||
result=result.result, | ||
traceback=result.traceback, | ||
status=result.state, | ||
name=result.name, | ||
args=result.args, | ||
kwargs=result.kwargs, | ||
worker=result.worker, | ||
retries=result.retries, | ||
queue=result.queue, | ||
) | ||
|
||
@staticmethod | ||
def get_result(uid: str): | ||
def revoke(*, tid: str): | ||
try: | ||
task_result = AsyncResult(id=uid, app=celery_app) | ||
result = AsyncResult(id=tid, app=celery_app) | ||
except NotRegistered: | ||
raise NotFoundError(msg='任务不存在') | ||
return task_result.result | ||
result.revoke(terminate=True) | ||
|
||
@staticmethod | ||
def run(*, name: str, args: list | None = None, kwargs: dict | None = None): | ||
task = celery_app.send_task(name=name, args=args, kwargs=kwargs) | ||
return task | ||
def run(*, obj: RunParam): | ||
task: AsyncResult = celery_app.send_task(name=obj.name, args=obj.args, kwargs=obj.kwargs) | ||
return task.task_id | ||
|
||
|
||
task_service: TaskService = TaskService() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters