Lightweight scheduler and worker for Django using a database backend
Project description
barn-py
Lightweight scheduler and worker for Django using a database as backend.
The task worker and scheduler use the database as a queue to store and find tasks to process
and you can forget about using transaction.on_commit
.
If you need a billion messages per second, please leave.
Examples
Use generic models
from django.http import HttpRequest, HttpResponse
from django.db import transaction
from barn.decorators import task
@transaction.atomic
def index(request: HttpRequest) -> HttpResponse:
html = "<html><body>Hello</body></html>"
# do something with database
dummy.delay(view="index", code="12")
return HttpResponse(html)
@task
def dummy(view: str, code: str) -> str:
# task called inside transaction
return "ok"
Use your models (prefered)
You can specify your own model for schedule and task and this approach is preferred.
from django.db import models
from django.core.mail import send_mail
from barn.models import AbstractTask
class EndOfTrialPeriodTask(AbstractTask):
user = models.ForeignKey('auth.User', models.CASCADE)
def process(self) -> None:
# go somewhere and do something...
self.user.is_active = False
self.user.save()
NotifyEndOfTrialPeriodTask.objects.create(user_id=self.user_id)
class NotifyEndOfTrialPeriodTask(AbstractTask):
user = models.ForeignKey('auth.User', models.CASCADE)
def process(self) -> None:
if self.user.email:
send_mail(
"End of trial period",
f"Hello {self.user.get_full_name()}. Your trial period has ended.",
"from@example.com",
[self.user.email],
fail_silently=False,
)
class EndOfSubscriptionTask(AbstractTask):
user = models.ForeignKey('auth.User', models.CASCADE)
def process(self) -> None:
# go somewhere and do something...
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
task_barn-0.2.0.tar.gz
(19.2 kB
view details)
Built Distribution
task_barn-0.2.0-py3-none-any.whl
(13.6 kB
view details)
File details
Details for the file task_barn-0.2.0.tar.gz
.
File metadata
- Download URL: task_barn-0.2.0.tar.gz
- Upload date:
- Size: 19.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06f6091aa61470bc17c9468c973767c883a21eb6e78091d3bd59221da8cdbc9a |
|
MD5 | a31ae839b0c6cbe209e70e30dbb78cf5 |
|
BLAKE2b-256 | 568fd96815ee18e284045d147cb1747b9f95dcb758db9e6d5718de8ddc41fcb1 |
File details
Details for the file task_barn-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: task_barn-0.2.0-py3-none-any.whl
- Upload date:
- Size: 13.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe771871b637cd15740de75fc7431fea063ebcf20ecc237fee3974f369808c73 |
|
MD5 | 92310348b257ac3582793186f8b1bfce |
|
BLAKE2b-256 | 77c3025a97c2161e50c2443e2dbd3d1d5b7bbf4303e50c8e9d94c5c90345876b |