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

Autocomplete jobs

Open cunla opened this issue 8 months ago • 1 comments

Currently, all methods decorated with @job(..) are stored JOB_METHODS_LIST.

Change the admin form for Task so it will be an autocomplete field with options from this list or other.

cunla avatar Apr 14 '25 19:04 cunla

Currently, all methods decorated with @job(..) are stored JOB_METHODS_LIST.

Change the admin form for Task so it will be an autocomplete field with options from this list or other.

I tried implementing this today, but ran into an issue. Unlike myapp/models.py, myapp/tasks.py isn’t dynamic - during runserver startup, tasks.py is never automatically loaded, while models.py is. So I’m not sure how to proceed here.

For example, in the debugger (during django runserver startup):

(Pdb) JOB_METHODS_LIST
['testproject.views.long_running_func']

This happens because the task long_running_func is defined inside testproject/views.py and is only called via Django's URL resolver (urls.pyviews.py). That’s why it appears in the list, but not all user-defined jobs are included in JOB_METHODS_LIST.

On the Django admin side, I was thinking of using a Select2 searchable dropdown for picking a callable task name instead of manually typing something like myapp.tasks.some_demo_task.

The problem is that JOB_METHODS_LIST only contains jobs whose files have already been loaded at least once by the Python interpreter.

Basically, once Python encounters a job definition like this:

from scheduler.decorators import job

@job("default")
def demo_task():
    print("Demo task +++")

the __call__ method of the job decorator runs, which does:

JOB_METHODS_LIST.append(f"{f.__module__}.{f.__name__}")

So the list only grows for jobs that have been executed or imported at least once. That's why relying on JOB_METHODS_LIST for the admin dropdown can be tricky.

DhavalGojiya avatar Oct 23 '25 14:10 DhavalGojiya