Skip to content

Commit

Permalink
Add terminate selected tasks action in admin
Browse files Browse the repository at this point in the history
  • Loading branch information
caoqianming committed Oct 18, 2024
1 parent 416551c commit b7ec3fc
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions django_celery_results/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from django.conf import settings
from django.contrib import admin
from django.utils.translation import gettext_lazy as _
from django.contrib import messages
from celery import current_app as celery_app

try:
ALLOW_EDITS = settings.DJANGO_CELERY_RESULTS['ALLOW_EDITS']
Expand Down Expand Up @@ -66,6 +68,18 @@ def get_readonly_fields(self, request, obj=None):
return list({
field.name for field in self.opts.local_fields
})

def terminate_task(self, request, queryset):
"""Terminate selected tasks."""
for task_result in queryset:
task_id = task_result.task_id
try:
celery_app.control.revoke(task_id, terminate=True)
self.message_user(request, f'Task {task_id} was terminated successfully.', messages.SUCCESS)
except Exception as e:
self.message_user(request, f'Failed to terminate task {task_id}. Error: {e}', messages.ERROR)

terminate_task.short_description = "Terminate selected tasks"


admin.site.register(TaskResult, TaskResultAdmin)
Expand Down

0 comments on commit b7ec3fc

Please sign in to comment.