mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-10-12 20:27:15 +00:00
71 lines
2.2 KiB
Python
71 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
from asgiref.sync import sync_to_async
|
|
|
|
from backend.app.common.exception import errors
|
|
from backend.app.common.task import scheduler
|
|
from backend.app.schemas.task import GetTask
|
|
from backend.app.utils.timezone import timezone_utils
|
|
|
|
|
|
class TaskService:
|
|
@staticmethod
|
|
@sync_to_async
|
|
def get_task_list():
|
|
tasks = []
|
|
for job in scheduler.get_jobs():
|
|
tasks.append(
|
|
GetTask(
|
|
**{
|
|
'id': job.id,
|
|
'func_name': job.func_ref,
|
|
'trigger': str(job.trigger),
|
|
'executor': job.executor,
|
|
'name': job.name,
|
|
'misfire_grace_time': job.misfire_grace_time,
|
|
'coalesce': job.coalesce,
|
|
'max_instances': job.max_instances,
|
|
'next_run_time': job.next_run_time,
|
|
}
|
|
).dict()
|
|
)
|
|
return tasks
|
|
|
|
@staticmethod
|
|
@sync_to_async
|
|
def get_task(pk: str):
|
|
job = scheduler.get_job(job_id=pk)
|
|
if not job:
|
|
raise errors.NotFoundError(msg='任务不存在')
|
|
task = GetTask(
|
|
**{
|
|
'id': job.id,
|
|
'func_name': job.func_ref,
|
|
'trigger': str(job.trigger),
|
|
'executor': job.executor,
|
|
'name': job.name,
|
|
'misfire_grace_time': job.misfire_grace_time,
|
|
'coalesce': job.coalesce,
|
|
'max_instances': job.max_instances,
|
|
'next_run_time': job.next_run_time,
|
|
}
|
|
)
|
|
return task
|
|
|
|
async def run(self, pk: str):
|
|
await self.get_task(pk=pk)
|
|
scheduler.modify_job(job_id=pk, next_run_time=timezone_utils.get_timezone_datetime())
|
|
|
|
async def pause(self, pk: str):
|
|
await self.get_task(pk=pk)
|
|
scheduler.pause_job(job_id=pk)
|
|
|
|
async def resume(self, pk: str):
|
|
await self.get_task(pk=pk)
|
|
scheduler.resume_job(job_id=pk)
|
|
|
|
async def delete(self, pk: str):
|
|
await self.get_task(pk=pk)
|
|
scheduler.remove_job(job_id=pk)
|
|
scheduler.add_job()
|