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