Active Job for Django
Project description
django-generic-tasks
Active Job for Django
Example usage
from django.core.mail import send_mail
from pydantic import BaseModel
import django_generic_tasks as tasks
# define task params as a Pydantic BaseModel
class EmailNotificationParams(BaseModel):
subject: str
content: str
recipients: list[str]
# subclass Task and specify params type as a generic type argument and implement the run method
class EmailNotificationTask(tasks.Task[EmailNotificationParams]):
def run(self):
send_mail(
subject=self.params.subject,
message=self.params.content,
from_email=None,
recipient_list=self.params.recipients,
)
if __name__ == "__main__":
params = EmailNotificationParams(
subject="Hello",
content="Have a nice day",
recipients=["alice@example.com", "bob@example.com"],
)
task = EmailNotificationTask(params)
# run a task synchronously
task.run()
# run a task asynchronously using settings.TASKS_BACKEND
task.start()
Registering tasks
Similar to signals, tasks have
to be implicitly registered by ensuring they are imported during application startup. This can be done in the ready
method in an application's AppConfig
.
from django.apps import AppConfig
class MyAppConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "my_app"
def ready(self):
from . import tasks # noqa: F401
HTTP endpoints
django-generic-tasks
uses django-ninja
to automatically expose API endpoints for running tasks. Each defined task
gets its own API
endpoint and uses the specified Pydantic BaseModel
for parameter verification.
Example configuration
Create your own NinjaAPI
instance and mount django_generic_tasks.api.router
.
django_generic_tasks_example/api.py
from ninja import NinjaAPI
import django_generic_tasks.api
api = NinjaAPI()
api.add_router("/tasks/", django_generic_tasks.api.router)
Add your NinjaAPI
instance in urls.py
:
django_generic_tasks_example/urls.py
from django.contrib import admin
from django.urls import path
from .api import api
urlpatterns = [
path("admin/", admin.site.urls),
path("api/", api.urls),
]
Supported backends
ThreadingBackend
Runs tasks in a new threading.Thread
.
CloudTasksBackend
Runs tasks using Cloud Tasks HTTP Target tasks.
Configuration
TASKS_API_AUTHENTICATION
Specifies what authentication should be required to run tasks via HTTP.
Type: Optional[str]
Supported values:
"oidc"
- Require API requests to contain a valid Google-issued OIDC tokenNone
(default) - No authentication
TASKS_BACKEND
The default backend to use to run tasks asynchronously.
Type: any class which implements the django_generic_tasks.backends.Backend
protocol
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-generic-tasks-0.3.2.tar.gz
.
File metadata
- Download URL: django-generic-tasks-0.3.2.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.11 CPython/3.10.2 Darwin/21.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e8e3398b6f69fc9bf66669ec547f8ec112b0d2642f3197f59c037c0b8f384bde |
|
MD5 | 9640c8511acf70bf07658e68bcc4030d |
|
BLAKE2b-256 | f83d938b40e57a902dfe8df5da33dc5e24880e3e5a605ebd308217c5eadce8f5 |
File details
Details for the file django_generic_tasks-0.3.2-py3-none-any.whl
.
File metadata
- Download URL: django_generic_tasks-0.3.2-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.11 CPython/3.10.2 Darwin/21.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 135ab27c10f70e809226419242e500bdbadf5e6ab1c61bbd06dfd59a124f84ed |
|
MD5 | 3196022c11ff28e32148a0b8bfef1690 |
|
BLAKE2b-256 | a128bdac1d5e6dc643bd69320347fe80dd18381d7194db8362b8ea8af1e88e6e |