Skip to content
Draft
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
21 changes: 21 additions & 0 deletions pulpcore/app/migrations/0153_taskschedule_pulp_domain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 5.2.13 on 2026-06-29 15:53

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

import pulpcore.app.util


class Migration(migrations.Migration):

dependencies = [
('core', '0152_alter_repositoryversion_content_ids'),
]

operations = [
migrations.AddField(
model_name='taskschedule',
name='pulp_domain',
field=models.ForeignKey(default=pulpcore.app.util.get_domain_pk, on_delete=django.db.models.deletion.CASCADE, to='core.domain'),
),
]
1 change: 1 addition & 0 deletions pulpcore/app/models/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ class TaskSchedule(BaseModel):
task_args = EncryptedJSONField(default=list)
task_kwargs = EncryptedJSONField(default=dict)
last_task = models.ForeignKey(Task, null=True, on_delete=models.SET_NULL)
pulp_domain = models.ForeignKey("Domain", default=get_domain_pk, on_delete=models.CASCADE)

class Meta:
permissions = [
Expand Down
6 changes: 6 additions & 0 deletions pulpcore/app/serializers/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ class TaskScheduleSerializer(ModelSerializer):
read_only=True,
view_name="tasks-detail",
)
pulp_domain = RelatedField(
help_text=_("Domain this schedule belongs to."),
read_only=True,
view_name="domains-detail",
)
Comment on lines +282 to +286

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One thing to note is that once you add a pulp_domain relationship to a model it will then be scoped to that domain and thus having a read-only field on the serializer is redundant (the domain is always present in the objects href). The other way we've added a domain relation without the object becoming a part of the domain is by naming the field domain like we did for UserRole and GroupRole.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ohhh, if anything this is not a bugfix afaics. Scheduled tasks have a very limited scope to global housework duties.
So what really is the ask? Making the task schedule itself a domain scoped object?


class Meta:
model = models.TaskSchedule
Expand All @@ -288,6 +293,7 @@ class Meta:
"dispatch_interval",
"next_dispatch",
"last_task",
"pulp_domain",
)


Expand Down
3 changes: 2 additions & 1 deletion pulpcore/tasking/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from django_guid import set_guid
from django_guid.utils import generate_guid

from pulpcore.app.contexts import with_domain
from pulpcore.app.models import Artifact, Content, ProfileArtifact, Task, TaskSchedule
from pulpcore.app.util import (
configure_analytics,
Expand Down Expand Up @@ -298,7 +299,7 @@ def dispatch_scheduled_tasks():
# Do not schedule in the past
task_schedule.next_dispatch += task_schedule.dispatch_interval
set_guid(generate_guid())
with transaction.atomic():
with with_domain(task_schedule.pulp_domain), transaction.atomic():
task_schedule.last_task = dispatch(
task_schedule.task_name,
args=task_schedule.task_args,
Expand Down
55 changes: 55 additions & 0 deletions pulpcore/tests/functional/api/test_workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import pytest

from pulpcore.app import settings

_DYNAMIC_WORKER_ATTRS = ("last_heartbeat", "current_task")
"""Worker attributes that are dynamically set by Pulp, not set by a user."""

Expand Down Expand Up @@ -124,3 +126,56 @@ def test_task_schedule(task_schedule, pulpcore_bindings):
else:
assert ts.dispatch_interval is not None
assert ts.next_dispatch is not None


@pytest.mark.parallel
@pytest.mark.skipif(not settings.DOMAIN_ENABLED, reason="Domains not enabled")
def test_task_schedule_domain(domain_factory, pulpcore_bindings):
"""Test that a scheduled task dispatches in the TaskSchedule's domain, not the default."""
domain = domain_factory()
domain_name = domain.name
name = str(uuid.uuid4())
task_name = "pulpcore.app.tasks.test.dummy_task"

schedule_commands = (
"from django.utils.timezone import now;"
"from datetime import timedelta;"
"from pulpcore.app.models import TaskSchedule, Domain;"
f"domain = Domain.objects.get(name='{domain_name}');"
"next_dispatch = now() + timedelta(seconds=5);"
"TaskSchedule("
f" name='{name}', task_name='{task_name}',"
" dispatch_interval=None, next_dispatch=next_dispatch,"
" pulp_domain=domain"
").save();"
)
process = subprocess.run(["pulpcore-manager", "shell", "-c", schedule_commands])
assert process.returncode == 0

try:
result = pulpcore_bindings.TaskSchedulesApi.list(name=name, pulp_domain=domain_name)
assert result.count == 1
ts = result.results[0]

for i in range(20):
sleep(1)
ts = pulpcore_bindings.TaskSchedulesApi.read(task_schedule_href=ts.pulp_href)
if ts.last_task is not None:
break

assert ts.last_task is not None, "Scheduled task was never dispatched"
task_pk = ts.last_task.strip("/").split("/")[-1]
task_href = f"/pulp/{domain_name}/api/v3/tasks/{task_pk}/"
task = pulpcore_bindings.TasksApi.read(task_href)
assert task.state == "completed", f"Task failed: {task.error}"
assert f"/{domain_name}/api/v3/" in task.pulp_href
finally:
subprocess.run(
[
"pulpcore-manager",
"shell",
"-c",
f"from pulpcore.app.models import TaskSchedule;"
f"TaskSchedule.objects.filter(name='{name}').delete();",
]
)
Loading