Skip to content

Commit

Permalink
Add domain and task argument support to TaskSchedule.
Browse files Browse the repository at this point in the history
Closes pulp#5778
  • Loading branch information
decko committed Sep 9, 2024
1 parent f26482c commit 357e61e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES/5778.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added domain and task arguments when using TaskSchedule to run a task.
2 changes: 1 addition & 1 deletion docs/dev/learn/other/task-scheduling.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Pulp supports scheduling of tasks. Scheduled tasks will be dispatched shortly after their
`next_dispatch` time, and be rescheduled one `dispatch_interval` after that, if the latter is
set. By specifying the `dispatch_interval` as `time_delta(days=1)` you can expect the task
dispatch to stabily happen at same time every day. Until the last task of the same schedule enters a
dispatch to stably happen at same time every day. Until the last task of the same schedule enters a
final state, a new task will not be dispatched. Scheduling is done by the worker processes,
therefore scheduled task dispatching will be missed if all workers are offline. After an outage
window, overdue schedules will dispatch at most one task, but down to timing, they may be
Expand Down
3 changes: 3 additions & 0 deletions pulpcore/app/models/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from pulpcore.app.models import (
AutoAddObjPermsMixin,
BaseModel,
Domain,
GenericRelationModel,
)
from pulpcore.app.models.status import BaseAppStatus
Expand Down Expand Up @@ -371,6 +372,8 @@ class TaskSchedule(BaseModel):
dispatch_interval = models.DurationField(null=True)
task_name = models.TextField()
last_task = models.ForeignKey(Task, null=True, on_delete=models.SET_NULL)
pulp_domain = models.ForeignKey(Domain, null=True, on_delete=models.CASCADE)
task_args = models.TextField(null=True)

class Meta:
permissions = [
Expand Down
7 changes: 7 additions & 0 deletions pulpcore/tasking/_util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ast
import asyncio
import importlib
import logging
Expand Down Expand Up @@ -209,9 +210,15 @@ def dispatch_scheduled_tasks():
# Do not schedule in the past
task_schedule.next_dispatch += task_schedule.dispatch_interval
set_guid(generate_guid())
if task_schedule.task_args:
task_args = ast.literal_eval(task_schedule.task_args)
if not isinstance(task_args, tuple) or not isinstance(task_args, list):
raise Exception("Task args could not been interpreted as a tuple or a list.")

with transaction.atomic():
task_schedule.last_task = dispatch(
task_schedule.task_name,
task_schedule.task_args
)
task_schedule.save(update_fields=["next_dispatch", "last_task"])

Expand Down

0 comments on commit 357e61e

Please sign in to comment.