Skip to content

Commit e0a106e

Browse files
authored
Simplify task crontab expression validation (#733)
1 parent 016361b commit e0a106e

File tree

3 files changed

+16
-52
lines changed

3 files changed

+16
-52
lines changed

backend/app/task/service/scheduler_service.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,7 @@ async def create(*, obj: CreateTaskSchedulerParam) -> None:
6565
if task_scheduler:
6666
raise errors.ConflictError(msg='任务调度已存在')
6767
if obj.type == TaskSchedulerType.CRONTAB:
68-
crontab_split = obj.crontab.split(' ')
69-
if len(crontab_split) != 5:
70-
raise errors.RequestError(msg='Crontab 表达式非法')
71-
crontab_verify('m', crontab_split[0])
72-
crontab_verify('h', crontab_split[1])
73-
crontab_verify('dow', crontab_split[2])
74-
crontab_verify('dom', crontab_split[3])
75-
crontab_verify('moy', crontab_split[4])
68+
crontab_verify(obj.crontab)
7669
await task_scheduler_dao.create(db, obj)
7770

7871
@staticmethod
@@ -92,14 +85,7 @@ async def update(*, pk: int, obj: UpdateTaskSchedulerParam) -> int:
9285
if await task_scheduler_dao.get_by_name(db, obj.name):
9386
raise errors.ConflictError(msg='任务调度已存在')
9487
if task_scheduler.type == TaskSchedulerType.CRONTAB:
95-
crontab_split = obj.crontab.split(' ')
96-
if len(crontab_split) != 5:
97-
raise errors.RequestError(msg='Crontab 表达式非法')
98-
crontab_verify('m', crontab_split[0])
99-
crontab_verify('h', crontab_split[1])
100-
crontab_verify('dow', crontab_split[2])
101-
crontab_verify('dom', crontab_split[3])
102-
crontab_verify('moy', crontab_split[4])
88+
crontab_verify(obj.crontab)
10389
count = await task_scheduler_dao.update(db, pk, obj)
10490
return count
10591

backend/app/task/utils/schedulers.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,21 +188,12 @@ async def to_model_schedule(name: str, task: str, schedule: schedules.schedule |
188188
if not obj:
189189
obj = TaskScheduler(**CreateTaskSchedulerParam(task=task, **spec).model_dump())
190190
elif isinstance(schedule, schedules.crontab):
191-
crontab_minute = schedule._orig_minute if crontab_verify('m', schedule._orig_minute, False) else '*'
192-
crontab_hour = schedule._orig_hour if crontab_verify('h', schedule._orig_hour, False) else '*'
193-
crontab_day_of_week = (
194-
schedule._orig_day_of_week if crontab_verify('dom', schedule._orig_day_of_week, False) else '*'
195-
)
196-
crontab_day_of_month = (
197-
schedule._orig_day_of_month if crontab_verify('dom', schedule._orig_day_of_month, False) else '*'
198-
)
199-
crontab_month_of_year = (
200-
schedule._orig_month_of_year if crontab_verify('moy', schedule._orig_month_of_year, False) else '*'
201-
)
191+
crontab = f'{schedule._orig_minute} {schedule._orig_hour} {schedule._orig_day_of_week} {schedule._orig_day_of_month} {schedule._orig_month_of_year}' # noqa: E501
192+
crontab_verify(crontab)
202193
spec = {
203194
'name': name,
204195
'type': TaskSchedulerType.CRONTAB.value,
205-
'crontab': f'{crontab_minute} {crontab_hour} {crontab_day_of_week} {crontab_day_of_month} {crontab_month_of_year}', # noqa: E501
196+
'crontab': crontab,
206197
}
207198
stmt = select(TaskScheduler).filter_by(**spec)
208199
query = await db.execute(stmt)

backend/app/task/utils/tzcrontab.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33
from datetime import datetime
4-
from typing import Literal
54

65
from celery import schedules
76
from celery.schedules import ParseException, crontab_parser
@@ -53,34 +52,22 @@ def __reduce__(self) -> tuple[type, tuple[str, str, str, str, str], None]:
5352
)
5453

5554

56-
def crontab_verify(filed: Literal['m', 'h', 'dow', 'dom', 'moy'], value: str, raise_exc: bool = True) -> bool:
55+
def crontab_verify(crontab: str) -> None:
5756
"""
5857
验证 Celery crontab 表达式
5958
60-
:param filed: 验证的字段
61-
:param value: 验证的值
62-
:param raise_exc: 是否抛出异常
59+
:param crontab: 计划表达式
6360
:return:
6461
"""
65-
valid = True
62+
crontab_split = crontab.split(' ')
63+
if len(crontab_split) != 5:
64+
raise errors.RequestError(msg='Crontab 表达式非法')
6665

6766
try:
68-
match filed:
69-
case 'm':
70-
crontab_parser(60, 0).parse(value)
71-
case 'h':
72-
crontab_parser(24, 0).parse(value)
73-
case 'dow':
74-
crontab_parser(7, 0).parse(value)
75-
case 'dom':
76-
crontab_parser(31, 1).parse(value)
77-
case 'moy':
78-
crontab_parser(12, 1).parse(value)
79-
case _:
80-
raise errors.ServerError(msg=f'无效字段:{filed}')
67+
crontab_parser(60, 0).parse(crontab_split[0]) # minute
68+
crontab_parser(24, 0).parse(crontab_split[1]) # hour
69+
crontab_parser(7, 0).parse(crontab_split[2]) # day_of_week
70+
crontab_parser(31, 1).parse(crontab_split[3]) # day_of_month
71+
crontab_parser(12, 1).parse(crontab_split[4]) # month_of_year
8172
except ParseException:
82-
valid = False
83-
if raise_exc:
84-
raise errors.RequestError(msg=f'crontab 值 {value} 非法')
85-
86-
return valid
73+
raise errors.RequestError(msg='Crontab 表达式非法')

0 commit comments

Comments
 (0)