# 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
```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 = "<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.
```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...
```
Raw data
{
"_id": null,
"home_page": null,
"name": "task-barn",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "django, distributed, multiprocessing, task, scheduler",
"author": null,
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/56/8f/d96815ee18e284045d147cb1747b9f95dcb758db9e6d5718de8ddc41fcb1/task_barn-0.2.0.tar.gz",
"platform": null,
"description": "# barn-py\n\nLightweight scheduler and worker for Django using a database as backend.\n\nThe task worker and scheduler use the database as a queue to store and find tasks to process\nand you can forget about using `transaction.on_commit`.\n\n**If you need a billion messages per second, please leave.**\n\n### Examples\n\n#### Use generic models\n\n```python\nfrom django.http import HttpRequest, HttpResponse\nfrom django.db import transaction\nfrom barn.decorators import task\n\n@transaction.atomic\ndef index(request: HttpRequest) -> HttpResponse:\n html = \"<html><body>Hello</body></html>\"\n # do something with database\n dummy.delay(view=\"index\", code=\"12\")\n return HttpResponse(html)\n\n\n@task\ndef dummy(view: str, code: str) -> str:\n # task called inside transaction\n return \"ok\"\n```\n\n#### Use your models (prefered)\n\nYou can specify your own model for schedule and task and this approach is preferred.\n\n```python\nfrom django.db import models\nfrom django.core.mail import send_mail\nfrom barn.models import AbstractTask\n\n\nclass EndOfTrialPeriodTask(AbstractTask):\n user = models.ForeignKey('auth.User', models.CASCADE)\n\n def process(self) -> None:\n # go somewhere and do something...\n self.user.is_active = False\n self.user.save()\n NotifyEndOfTrialPeriodTask.objects.create(user_id=self.user_id)\n\n\nclass NotifyEndOfTrialPeriodTask(AbstractTask):\n user = models.ForeignKey('auth.User', models.CASCADE)\n\n def process(self) -> None:\n if self.user.email:\n send_mail(\n \"End of trial period\",\n f\"Hello {self.user.get_full_name()}. Your trial period has ended.\",\n \"from@example.com\",\n [self.user.email],\n fail_silently=False,\n )\n\n\nclass EndOfSubscriptionTask(AbstractTask):\n user = models.ForeignKey('auth.User', models.CASCADE)\n\n def process(self) -> None:\n # go somewhere and do something...\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Lightweight scheduler and worker for Django using a database backend",
"version": "0.2.0",
"project_urls": {
"Homepage": "https://github.com/bibenga/barn-py",
"Issues": "https://github.com/bibenga/barn-py/issues"
},
"split_keywords": [
"django",
" distributed",
" multiprocessing",
" task",
" scheduler"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "77c3025a97c2161e50c2443e2dbd3d1d5b7bbf4303e50c8e9d94c5c90345876b",
"md5": "92310348b257ac3582793186f8b1bfce",
"sha256": "fe771871b637cd15740de75fc7431fea063ebcf20ecc237fee3974f369808c73"
},
"downloads": -1,
"filename": "task_barn-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "92310348b257ac3582793186f8b1bfce",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 13595,
"upload_time": "2024-07-13T10:32:02",
"upload_time_iso_8601": "2024-07-13T10:32:02.075417Z",
"url": "https://files.pythonhosted.org/packages/77/c3/025a97c2161e50c2443e2dbd3d1d5b7bbf4303e50c8e9d94c5c90345876b/task_barn-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "568fd96815ee18e284045d147cb1747b9f95dcb758db9e6d5718de8ddc41fcb1",
"md5": "a31ae839b0c6cbe209e70e30dbb78cf5",
"sha256": "06f6091aa61470bc17c9468c973767c883a21eb6e78091d3bd59221da8cdbc9a"
},
"downloads": -1,
"filename": "task_barn-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "a31ae839b0c6cbe209e70e30dbb78cf5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 19202,
"upload_time": "2024-07-13T10:32:03",
"upload_time_iso_8601": "2024-07-13T10:32:03.519302Z",
"url": "https://files.pythonhosted.org/packages/56/8f/d96815ee18e284045d147cb1747b9f95dcb758db9e6d5718de8ddc41fcb1/task_barn-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-13 10:32:03",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "bibenga",
"github_project": "barn-py",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "task-barn"
}