mirror of
https://github.com/django-q2/django-q2.git
synced 2026-10-04 13:48:11 +08:00
Rerun successful tasks (#99)
* Move logic that resubmits a task and then deletes the original into an action function that can be used for both failure and success resubmission. * Use the resubmit_task action in the FailAdmin. * Add the resubmit_task action to the TaskAdmin class. * Update resubmit failure test to reflect using the new resubmit action. * Add a test for resubmitting successful tasks. --------- Co-authored-by: Scott Pashley
This commit is contained in:
co-authored by
Scott Pashley
parent
f8321e7f54
commit
23c06a38eb
+20
-12
@@ -10,10 +10,29 @@ from django_q.models import Failure, OrmQ, Schedule, Success, Task
|
|||||||
from django_q.tasks import async_task
|
from django_q.tasks import async_task
|
||||||
|
|
||||||
|
|
||||||
|
def resubmit_task(model_admin, request, queryset):
|
||||||
|
"""Submit selected tasks back to the queue."""
|
||||||
|
for task in queryset:
|
||||||
|
async_task(
|
||||||
|
task.func,
|
||||||
|
*task.args or (),
|
||||||
|
hook=task.hook,
|
||||||
|
group=task.group,
|
||||||
|
cluster=task.cluster,
|
||||||
|
**task.kwargs or {},
|
||||||
|
)
|
||||||
|
if isinstance(model_admin, FailAdmin):
|
||||||
|
task.delete()
|
||||||
|
|
||||||
|
|
||||||
|
resubmit_task.short_description = _("Resubmit selected tasks to queue")
|
||||||
|
|
||||||
|
|
||||||
class TaskAdmin(admin.ModelAdmin):
|
class TaskAdmin(admin.ModelAdmin):
|
||||||
"""model admin for success tasks."""
|
"""model admin for success tasks."""
|
||||||
|
|
||||||
list_display = ("name", "group", "func", "cluster", "started", "stopped", "time_taken")
|
list_display = ("name", "group", "func", "cluster", "started", "stopped", "time_taken")
|
||||||
|
actions = [resubmit_task]
|
||||||
|
|
||||||
def has_add_permission(self, request):
|
def has_add_permission(self, request):
|
||||||
"""Don't allow adds."""
|
"""Don't allow adds."""
|
||||||
@@ -33,17 +52,6 @@ class TaskAdmin(admin.ModelAdmin):
|
|||||||
return list(self.readonly_fields) + [field.name for field in obj._meta.fields]
|
return list(self.readonly_fields) + [field.name for field in obj._meta.fields]
|
||||||
|
|
||||||
|
|
||||||
def retry_failed(FailAdmin, request, queryset):
|
|
||||||
"""Submit selected tasks back to the queue."""
|
|
||||||
for task in queryset:
|
|
||||||
async_task(task.func, *task.args or (), hook=task.hook,
|
|
||||||
group=task.group, cluster=task.cluster, **task.kwargs or {})
|
|
||||||
task.delete()
|
|
||||||
|
|
||||||
|
|
||||||
retry_failed.short_description = _("Resubmit selected tasks to queue")
|
|
||||||
|
|
||||||
|
|
||||||
class FailAdmin(admin.ModelAdmin):
|
class FailAdmin(admin.ModelAdmin):
|
||||||
"""model admin for failed tasks."""
|
"""model admin for failed tasks."""
|
||||||
|
|
||||||
@@ -53,7 +61,7 @@ class FailAdmin(admin.ModelAdmin):
|
|||||||
"""Don't allow adds."""
|
"""Don't allow adds."""
|
||||||
return False
|
return False
|
||||||
|
|
||||||
actions = [retry_failed]
|
actions = [resubmit_task]
|
||||||
search_fields = ("name", "func", "group")
|
search_fields = ("name", "func", "group")
|
||||||
list_filter = ("group", "cluster")
|
list_filter = ("group", "cluster")
|
||||||
readonly_fields = []
|
readonly_fields = []
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ def test_admin_views(admin_client, monkeypatch):
|
|||||||
|
|
||||||
# resubmit the failure
|
# resubmit the failure
|
||||||
url = reverse("admin:django_q_failure_changelist")
|
url = reverse("admin:django_q_failure_changelist")
|
||||||
data = {"action": "retry_failed", "_selected_action": [f.pk]}
|
data = {"action": "resubmit_task", "_selected_action": [f.pk]}
|
||||||
response = admin_client.post(url, data)
|
response = admin_client.post(url, data)
|
||||||
assert response.status_code == 302
|
assert response.status_code == 302
|
||||||
assert Failure.objects.filter(name=f.id).exists() is False
|
assert Failure.objects.filter(name=f.id).exists() is False
|
||||||
@@ -84,3 +84,10 @@ def test_admin_views(admin_client, monkeypatch):
|
|||||||
data = {"post": "yes"}
|
data = {"post": "yes"}
|
||||||
response = admin_client.post(url, data)
|
response = admin_client.post(url, data)
|
||||||
assert response.status_code == 302
|
assert response.status_code == 302
|
||||||
|
# Resubmit a successful task.
|
||||||
|
url = reverse("admin:django_q_success_changelist")
|
||||||
|
data = {"action": "resubmit_task", "_selected_action": [t.pk]}
|
||||||
|
initial_queue_count = OrmQ.objects.count()
|
||||||
|
response = admin_client.post(url, data)
|
||||||
|
assert response.status_code == 302
|
||||||
|
assert OrmQ.objects.count() > initial_queue_count
|
||||||
|
|||||||
Reference in New Issue
Block a user