Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add domain and task argument support to TaskSchedule. #5779

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dispatch to stably happen at same time every day. Until the last task of the same schedule enters a
dispatch to stably happen at the 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.2.16 on 2024-09-09 16:27

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('core', '0122_record_last_replication_timestamp'),
]

operations = [
migrations.AddField(
model_name='taskschedule',
name='pulp_domain',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='core.domain'),
),
migrations.AddField(
model_name='taskschedule',
name='task_args',
field=models.TextField(null=True),
),
]
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use here JSONField instead: https://docs.djangoproject.com/en/4.2/ref/models/fields/#jsonfield? It appears to be much more safe than calling ast.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea. Gonna work on that.


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
Loading