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 22, 2024
1 parent 10bad3d commit 1bc2f8f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
35 changes: 28 additions & 7 deletions gcloud/core/apis/drf/viewsets/periodic_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,17 @@ def create(self, request, *args, **kwargs):
project = Project.objects.filter(id=serializer.validated_data["project"].id).first()
if settings.PERIODIC_TASK_SHORTEST_TIME:
result = PeriodicTask().inspect_time(
request.user.is_superuser, serializer.validated_data["cron"], project.time_zone
request.user.is_superuser,
serializer.validated_data["cron"],
int(settings.PERIODIC_TASK_SHORTEST_TIME),
project.time_zone,
)
if not result:
raise ValidationException("The interval between tasks should be at least 30 minutes")
raise ValidationException(
"The interval between tasks should be at least {} minutes".format(
settings.PERIODIC_TASK_SHORTEST_TIME
)
)
try:
self._handle_serializer(request, serializer)
instance = serializer.save()
Expand All @@ -282,11 +289,18 @@ def update(self, request, *args, **kwargs):
serializer.is_valid(raise_exception=True)
project = Project.objects.filter(id=serializer.validated_data["project"].id).first()
if settings.PERIODIC_TASK_SHORTEST_TIME:
result = PeriodicTask().inspect_time(
request.user.is_superuser, serializer.validated_data["cron"], project.time_zone
result = instance.inspect_time(
request.user.is_superuser,
serializer.validated_data["cron"],
int(settings.PERIODIC_TASK_SHORTEST_TIME),
project.time_zone,
)
if not result:
raise ValidationException("The interval between tasks should be at least 30 minutes")
raise ValidationException(
"The interval between tasks should be at least {} minutes".format(
settings.PERIODIC_TASK_SHORTEST_TIME
)
)
try:
self._handle_serializer(request, serializer)
instance = PeriodicTask.objects.update(instance, **serializer.validated_data)
Expand All @@ -310,10 +324,17 @@ def partial_update(self, request, *args, **kwargs):
project = Project.objects.filter(id=serializer.validated_data["project"]).first()
if settings.PERIODIC_TASK_SHORTEST_TIME:
result = instance.inspect_time(
request.user.is_superuser, serializer.validated_data["cron"], project.time_zone
request.user.is_superuser,
serializer.validated_data["cron"],
int(settings.PERIODIC_TASK_SHORTEST_TIME),
project.time_zone,
)
if not result:
raise ValidationException("The interval between tasks should be at least 30 minutes")
raise ValidationException(
"The interval between tasks should be at least {} minutes".format(
settings.PERIODIC_TASK_SHORTEST_TIME
)
)
instance.modify_cron(serializer.validated_data["cron"], project.time_zone)
if "constants" in serializer.validated_data:
instance.modify_constants(serializer.validated_data["constants"])
Expand Down
3 changes: 1 addition & 2 deletions gcloud/periodictask/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def delete(self, using=None):
super(PeriodicTask, self).delete(using)
PeriodicTaskHistory.objects.filter(task=self).delete()

def inspect_time(self, is_superuser, cron, timezone=None):
def inspect_time(self, is_superuser, cron, shortest_time, timezone=None):
schedule, _ = DjangoCeleryBeatCrontabSchedule.objects.get_or_create(
minute=cron.get("minute", "*"),
hour=cron.get("hour", "*"),
Expand All @@ -277,7 +277,6 @@ def inspect_time(self, is_superuser, cron, timezone=None):
next_times = [schedule_iter.get_next(datetime) for _ in range(10)]
min_interval = min((next_times[i] - next_times[i - 1] for i in range(1, len(next_times))))

shortest_time = int(settings.PERIODIC_TASK_SHORTEST_TIME)
if min_interval < timedelta(minutes=shortest_time):
result = False

Expand Down
13 changes: 10 additions & 3 deletions gcloud/tests/periodictask/models/test_periodic_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,17 @@ def test_delete(self):
)

def test_inspect_time(self):
self.cron = {"day_of_month": "*", "day_of_week": "*", "hour": "*", "minute": "*/30", "month_of_year": "*"}
self.cron = {"day_of_month": "*", "day_of_week": "*", "hour": "*", "minute": "*/35", "month_of_year": "*"}
self.timezone = "Asia/Shanghai"
self.periodic_task = self.task.inspect_time(is_superuser=True, cron=self.cron, timezone=self.timezone)
self.assertTrue(self.periodic_task)
self.shortest_time = 30
self.periodic_task_true = self.task.inspect_time(
is_superuser=True, cron=self.cron, shortest_time=self.shortest_time, timezone=self.timezone
)
self.assertTrue(self.periodic_task_true)
self.periodic_task_false = self.task.inspect_time(
is_superuser=False, cron=self.cron, shortest_time=self.shortest_time, timezone=self.timezone
)
self.assertFalse(self.periodic_task_false)

def test_modify_constants(self):
expect_constants = copy.deepcopy(self.task.task.execution_data["constants"])
Expand Down

0 comments on commit 1bc2f8f

Please sign in to comment.