https://github.com/bibenga/barn-py
Lightweight scheduler and worker for Django using a database
https://github.com/bibenga/barn-py
database django postgresql python
Last synced: 6 months ago
JSON representation
Lightweight scheduler and worker for Django using a database
- Host: GitHub
- URL: https://github.com/bibenga/barn-py
- Owner: bibenga
- License: mit
- Created: 2024-06-19T12:08:18.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-20T12:03:21.000Z (almost 2 years ago)
- Last Synced: 2025-09-22T20:26:56.679Z (9 months ago)
- Topics: database, django, postgresql, python
- Language: Python
- Homepage:
- Size: 183 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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 want a billion messages per second, get out of here.**
### Examples
#### Use generic models
```python
from django.http import HttpRequest, HttpResponse
from django.db import transaction
from barn.decorators import task
@transaction.atomic
def index(request: HttpRequest) -> HttpResponse:
html = "Hello"
# 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.
```python
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...
```