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 terminate selected tasks action in admin #449

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
23 changes: 22 additions & 1 deletion django_celery_results/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Result Task Admin interface."""

from celery import current_app as celery_app
from django.conf import settings
from django.contrib import admin
from django.contrib import admin, messages
from django.utils.translation import gettext_lazy as _

try:
Expand Down Expand Up @@ -58,6 +59,7 @@ class TaskResultAdmin(admin.ModelAdmin):
'classes': ('extrapretty', 'wide')
}),
)
actions = ['terminate_task']

def get_readonly_fields(self, request, obj=None):
if ALLOW_EDITS:
Expand All @@ -67,6 +69,25 @@ def get_readonly_fields(self, request, obj=None):
field.name for field in self.opts.local_fields
})

def terminate_task(self, request, queryset):
"""Terminate selected tasks."""
task_ids = list(queryset.values_list('task_id', flat=True))
try:
celery_app.control.terminate(task_ids)
self.message_user(
request,
f"{len(task_ids)} Task was terminated successfully.",
messages.SUCCESS,
)
except Exception as e:
self.message_user(
request,
f"Error while terminating tasks: {e}",
messages.ERROR,
)

terminate_task.short_description = "Terminate selected tasks"


admin.site.register(TaskResult, TaskResultAdmin)

Expand Down
Loading