django-background-tasks icon indicating copy to clipboard operation
django-background-tasks copied to clipboard

Run tasks individually

Open ISilviu opened this issue 3 years ago • 0 comments

Hi,

I'd like to add a new action (to the admin UI) that could used to run tasks individually, without affecting their schedule time.

On background_task/admin.py, I have created a start method and added it to the list of possible actions.

...
from background_task.tasks import tasks

...
# the start method
def start(modeladmin, request, queryset):
    for task in queryset:
        tasks.run_task(task)
start.short_description = "Start desired tasks"


class TaskAdmin(admin.ModelAdmin):
    display_filter = ['task_name']
    search_fields = ['task_name', 'task_params', ]
    list_display = ['task_name', 'task_params', 'run_at', 'priority', 'attempts', 'has_error', 'locked_by', 'locked_by_pid_running', ]
    #added the start method
    actions = [inc_priority, dec_priority, start]
...

However, when executing the run_task method, the _tasks internal dictionary of the tasks object is empty. I would have expected to have it already initialized with all my @background annotated methods.

Manually registering the task before running it seems unnecessary to me.

Could you help me?

ISilviu avatar Aug 24 '21 08:08 ISilviu