From 61a60d7b2d22250a67ed85f0e2734b5612727649 Mon Sep 17 00:00:00 2001 From: guohelu <19503896967@163.com> Date: Wed, 27 Nov 2024 19:24:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20--story=3D120737215?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- env.py | 2 +- .../apis/drf/serilaziers/periodic_task.py | 36 +++++++------------ 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/env.py b/env.py index a0ca1c81b..b97c77251 100644 --- a/env.py +++ b/env.py @@ -150,7 +150,7 @@ # 周期任务消息通知类型 PERIODIC_TASK_REMINDER_NOTIFY_TYPE = json.loads(os.getenv("PERIODIC_TASK_REMINDER_NOTIFY_TYPE", '["email"]')) -# 周期任务最短时间间隔,以分钟为单位,例如:30 +# 周期任务最短时间间隔,以分钟为单位,例如:30,0 表示无限制 PERIODIC_TASK_SHORTEST_TIME = int(os.getenv("PERIODIC_TASK_SHORTEST_TIME", 0)) # 周期任务迭代次数 PERIODIC_TASK_ITERATION = int(os.getenv("PERIODIC_TASK_ITERATION", 10)) diff --git a/gcloud/core/apis/drf/serilaziers/periodic_task.py b/gcloud/core/apis/drf/serilaziers/periodic_task.py index e337d9e22..3240f4687 100644 --- a/gcloud/core/apis/drf/serilaziers/periodic_task.py +++ b/gcloud/core/apis/drf/serilaziers/periodic_task.py @@ -157,16 +157,20 @@ def check_cron_params(cron, project): class CronFieldSerializer(serializers.Serializer): cron = serializers.DictField(write_only=True, help_text="周期", required=False) - def validate_inspect_cron(self, value): - minute = value.get("minute", "*") - hour = value.get("hour", "*") - day_of_month = value.get("day_of_month", "*") - month = value.get("month", "*") - day_of_week = value.get("day_of_week", "*") + def inspect_cron(self, cron): + minute = cron.get("minute", "*") + hour = cron.get("hour", "*") + day_of_month = cron.get("day_of_month", "*") + month = cron.get("month", "*") + day_of_week = cron.get("day_of_week", "*") cron_expression = f"{minute} {hour} {day_of_month} {month} {day_of_week}" - return cron_expression + result = inspect_time(cron_expression, settings.PERIODIC_TASK_SHORTEST_TIME, settings.PERIODIC_TASK_ITERATION) + if not result: + raise serializers.ValidationError( + "The interval between tasks should be at least {} minutes".format(settings.PERIODIC_TASK_SHORTEST_TIME) + ) class CreatePeriodicTaskSerializer(CronFieldSerializer, serializers.ModelSerializer): @@ -210,14 +214,7 @@ def validate_project(self, value): def validate(self, attrs): check_cron_params(attrs.get("cron"), attrs.get("project")) if settings.PERIODIC_TASK_SHORTEST_TIME and not self.context["request"].user.is_superuser: - cron_str = super().validate_inspect_cron(attrs.get("cron")) - result = inspect_time(cron_str, settings.PERIODIC_TASK_SHORTEST_TIME, settings.PERIODIC_TASK_ITERATION) - if not result: - raise serializers.ValidationError( - "The interval between tasks should be at least {} minutes".format( - settings.PERIODIC_TASK_SHORTEST_TIME - ) - ) + super().inspect_cron(attrs.get("cron")) return attrs class Meta: @@ -233,12 +230,5 @@ class PatchUpdatePeriodicTaskSerializer(CronFieldSerializer, serializers.Seriali def validate(self, attrs): check_cron_params(attrs.get("cron"), attrs.get("project")) if settings.PERIODIC_TASK_SHORTEST_TIME and not self.context["request"].user.is_superuser: - cron_str = super().validate_inspect_cron(attrs.get("cron")) - result = inspect_time(cron_str, settings.PERIODIC_TASK_SHORTEST_TIME, settings.PERIODIC_TASK_ITERATION) - if not result: - raise serializers.ValidationError( - "The interval between tasks should be at least {} minutes".format( - settings.PERIODIC_TASK_SHORTEST_TIME - ) - ) + super().inspect_cron(attrs.get("cron")) return attrs