Skip to content

Commit 00ffb23

Browse files
author
wang.qiyuan
committed
fix: 优化Crontab表达式验证逻辑,使用Celery内置crontab类替代手动解析
1 parent 22f8850 commit 00ffb23

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

backend/app/task/utils/tzcrontab.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from datetime import datetime
44

55
from celery import schedules
6-
from celery.schedules import ParseException, crontab_parser
6+
from celery.schedules import ParseException, crontab_parser, crontab
77

88
from backend.common.exception import errors
99
from backend.utils.timezone import timezone
@@ -52,22 +52,17 @@ def __reduce__(self) -> tuple[type, tuple[str, str, str, str, str], None]:
5252
)
5353

5454

55-
def crontab_verify(crontab: str) -> None:
55+
def crontab_verify(crontab_str: str) -> None:
5656
"""
5757
验证 Celery crontab 表达式
5858
59-
:param crontab: 计划表达式
59+
:param crontab_str: 计划表达式
6060
:return:
6161
"""
62-
crontab_split = crontab.split(' ')
62+
crontab_split = crontab_str.split(' ')
6363
if len(crontab_split) != 5:
6464
raise errors.RequestError(msg='Crontab 表达式非法')
65-
6665
try:
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
66+
crontab(*crontab_split)
7267
except ParseException:
7368
raise errors.RequestError(msg='Crontab 表达式非法')

0 commit comments

Comments
 (0)