-
Notifications
You must be signed in to change notification settings - Fork 56
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
Improve the django experience #363
Comments
#745 enhances the docs about Django integration. Also, I was thinking in creating a |
Yep, I think you're on the right track |
I have some thoughts to share regarding enhancing procrastinate's Django experience. In summary, I think the best Django experience would be something like:
To implement this experience I think procrastinate needs to:
For this to work, I think we need to be able to define a task without instantiating the procrastinate Let's say we define these two functions and expose them as def task(func): # Needs lots of enhancements to be more like `App.task`, of course
func._procrastinate_task = True
return func
def is_task(func):
return getattr(func, "_procrastinate_task", None) == True And inside from procrastinate import task
@task
def double(number: int):
return number ** 2
@task
def hello(name: str):
return f"Hello, {name}" Note that I'm not attaching a task to an We can then implement a function to auto discover tasks by inspecting all Django apps: # Some module inside procrastinate.contrib.django
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
# TODO: what if my project settings file is not inside `project/`?
# XXX: we may need to force the user to set `DJANGO_SETTINGS_MODULE` env var to use procrastinate with Django, so we
# don't need the `os.environ.setdefault` above, since it will not work for all the projects.
import django
django.setup()
import importlib
import inspect
from dataclasses import dataclass
from typing import Callable
from django.apps import apps
from procrastinate import is_task # as defined in the code section above
@dataclass
class AppTask:
name: str
func: Callable
app_name: str
def autodiscover_tasks():
for app_config in apps.get_app_configs():
app_name = app_config.name
try:
tasks_module = importlib.import_module(f"{app_name}.tasks")
except ModuleNotFoundError:
continue
for name, func in inspect.getmembers(tasks_module):
if is_task(func):
yield AppTask(name=name, func=func, app_name=app_name) Finally, try it: from procrastinate.contrib.django import autodiscover_tasks
for app_task in autodiscover_tasks():
print(app_task) With the |
I believe the lib is now in a state where it's reasonable to start tackling this. @turicas is this something you'd like to contribute ? Or would you rather I do it and you'd give some feedback ? Last option, we could try and work together on that if it's something that works for you :) |
Ok, so I'm all for making it easy to get the app defined for you if you want, but I think once the app is easy to get, we should use I'm implementing the rest. I'm wondering what would be the best place to put the app so it's easily importable from everywhere. |
@ignaciocabeza shared that the experience of integrating procrastinate in a Django project could be improved.
I think we would be wise listening to this feedback, reproduce and solve the problems, and making sure that developing with procrastinate & Django works well !
(Agate, I think you might be interested as well :) ping @EliotBerriot )
The text was updated successfully, but these errors were encountered: