-
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update the celery configuration and tasks (#458)
* Update the celery configuration and tasks * fix message notifications
- Loading branch information
Showing
13 changed files
with
408 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
from celery import Task | ||
from sqlalchemy.exc import SQLAlchemyError | ||
|
||
from backend.app.task.conf import task_settings | ||
from backend.common.socketio.actions import task_notification | ||
|
||
|
||
class TaskBase(Task): | ||
"""任务基类""" | ||
|
||
autoretry_for = (SQLAlchemyError,) | ||
max_retries = task_settings.CELERY_TASK_MAX_RETRIES | ||
|
||
async def before_start(self, task_id, args, kwargs): | ||
await task_notification(msg=f'任务 {task_id} 开始执行') | ||
|
||
async def on_success(self, retval, task_id, args, kwargs): | ||
await task_notification(msg=f'任务 {task_id} 执行成功') | ||
|
||
async def on_failure(self, exc, task_id, args, kwargs, einfo): | ||
await task_notification(msg=f'任务 {task_id} 执行失败') |
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,39 +1,19 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
from sqlalchemy.exc import SQLAlchemyError | ||
|
||
from backend.app.admin.service.login_log_service import login_log_service | ||
from backend.app.admin.service.opera_log_service import opera_log_service | ||
from backend.app.task.celery import celery_app | ||
from backend.app.task.conf import task_settings | ||
|
||
|
||
@celery_app.task( | ||
name='auto_delete_db_opera_log', | ||
bind=True, | ||
retry_backoff=True, | ||
max_retries=task_settings.CELERY_TASK_MAX_RETRIES, | ||
) | ||
async def auto_delete_db_opera_log(self) -> int: | ||
@celery_app.task(name='delete_db_opera_log') | ||
async def delete_db_opera_log() -> int: | ||
"""自动删除数据库操作日志""" | ||
try: | ||
result = await opera_log_service.delete_all() | ||
except SQLAlchemyError as exc: | ||
raise self.retry(exc=exc) | ||
result = await opera_log_service.delete_all() | ||
return result | ||
|
||
|
||
@celery_app.task( | ||
name='auto_delete_db_login_log', | ||
bind=True, | ||
retry_backoff=True, | ||
max_retries=task_settings.CELERY_TASK_MAX_RETRIES, | ||
) | ||
async def auto_delete_db_login_log(self) -> int: | ||
@celery_app.task(name='delete_db_login_log') | ||
async def delete_db_login_log() -> int: | ||
"""自动删除数据库登录日志""" | ||
|
||
try: | ||
result = await login_log_service.delete_all() | ||
except SQLAlchemyError as exc: | ||
raise self.retry(exc=exc) | ||
result = await login_log_service.delete_all() | ||
return result |
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,15 +1,11 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
import uuid | ||
|
||
from anyio import sleep | ||
|
||
from backend.app.task.celery import celery_app | ||
|
||
|
||
@celery_app.task(name='task_demo_async') | ||
async def task_demo_async() -> str: | ||
await sleep(1) | ||
uid = uuid.uuid4().hex | ||
print(f'异步任务 {uid} 执行成功') | ||
return uid | ||
await sleep(10) | ||
return 'test async' |
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
File renamed without changes.
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
Oops, something went wrong.