Skip to content

Commit

Permalink
fix: 修复逻辑问题 --story=120737215
Browse files Browse the repository at this point in the history
  • Loading branch information
guohelu committed Nov 27, 2024
1 parent dea7476 commit 61a60d7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
2 changes: 1 addition & 1 deletion env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
36 changes: 13 additions & 23 deletions gcloud/core/apis/drf/serilaziers/periodic_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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

0 comments on commit 61a60d7

Please sign in to comment.