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.1.1.tar.gz
(19.1 kB
view details)
Built Distribution
task_barn-0.1.1-py3-none-any.whl
(13.3 kB
view details)
File details
Details for the file task_barn-0.1.1.tar.gz
.
File metadata
- Download URL: task_barn-0.1.1.tar.gz
- Upload date:
- Size: 19.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9831b12c1ff928249e426aba39091ac81dd758bb8fc6068289a631484c889844 |
|
MD5 | bf6a782ce89da3a47620ac9f05b39a4c |
|
BLAKE2b-256 | c28062159a869bf369d04ddfbf9aa72cd22912c0974a70d6a1c7cca5586f50a9 |
File details
Details for the file task_barn-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: task_barn-0.1.1-py3-none-any.whl
- Upload date:
- Size: 13.3 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 | a1202bc8a5c3d76c63c3e97c0c437643357ad281b1424f6026c9622f04123f2c |
|
MD5 | 82a4df80d0c8632841ae610d034510f9 |
|
BLAKE2b-256 | 69599b23930b4bf09863daa1cc0d80651316affebadb869e325bc230ead79df8 |