fix: 清理错误定时策略并支持生成cron文本4

This commit is contained in:
caoqianming 2023-08-07 01:05:11 +08:00
parent 4375c344e9
commit f892ee0460
1 changed files with 22 additions and 23 deletions

View File

@ -1,7 +1,7 @@
import locale import locale
from croniter import croniter from croniter import croniter
from cron_descriptor import get_description from cron_descriptor import get_description
from safesite.models import MySchedule, GridTaskSet, PeriodicTask from safesite.models import MySchedule, GridTaskSet, PeriodicTask, CrontabSchedule, IntervalSchedule
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
period_dict = { period_dict = {
"days": "", "days": "",
@ -14,7 +14,10 @@ def get_chinese_description(type:str = 'interval', data: dict = {}):
"""转换为汉语描述 """转换为汉语描述
""" """
if type == 'interval': if type == 'interval':
return f"每隔{data['every']}{period_dict[data['period']]}" try:
return f"每隔{data['every']}{period_dict[data['period']]}"
except KeyError:
return ''
elif type == 'crontab': elif type == 'crontab':
exp = f"{data['minute']} {data['hour']} {data['day_of_month']} {data['month_of_year']} {data['day_of_week']}" exp = f"{data['minute']} {data['hour']} {data['day_of_month']} {data['month_of_year']} {data['day_of_week']}"
try: try:
@ -26,26 +29,22 @@ def get_chinese_description(type:str = 'interval', data: dict = {}):
return '' return ''
def correct_myschedule(): def correct_myschedule():
qs = MySchedule.objects.all() cs = CrontabSchedule.objects.all()
for ind, item in enumerate(qs): for crontab in cs:
typeset = item.typeset name = get_chinese_description('crontab',
if typeset == 'interval':
interval = item.interval
item.name = get_chinese_description(typeset, {'every': interval.every, 'period': interval.period})
item.save()
elif typeset == 'crontab':
try:
crontab = item.crontab
if crontab:
item.name = get_chinese_description(typeset,
{"minute": crontab.minute, "hour": crontab.hour, "day_of_month": crontab.day_of_month, {"minute": crontab.minute, "hour": crontab.hour, "day_of_month": crontab.day_of_month,
"month_of_year": crontab.month_of_year, "day_of_week": crontab.day_of_week}) "month_of_year": crontab.month_of_year, "day_of_week": crontab.day_of_week})
if item.name == '': if name == '':
crontab.delete() MySchedule.objects.filter(crontab=crontab).delete()
item.delete() crontab.delete()
else: else:
PeriodicTask.objects.filter(gridtaskset_periodictask__myschedule=item).delete() MySchedule.objects.filter(crontab=crontab).update(name=name, typeset='crontab')
item.delete()
except ObjectDoesNotExist: ints = IntervalSchedule.objects.all()
GridTaskSet.objects.filter(myschedule=item).delete() for interval in ints:
item.delete() name = get_chinese_description('interval', {'every': interval.every, 'period': interval.period})
if name == '':
MySchedule.objects.filter(interval=interval).delete()
interval.delete()
else:
MySchedule.objects.filter(interval=interval).update(name=name, typeset='interval')