taskferry-django 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- taskferry_django/__init__.py +85 -0
- taskferry_django/apps.py +41 -0
- taskferry_django/backend.py +195 -0
- taskferry_django/checks.py +194 -0
- taskferry_django/config.py +116 -0
- taskferry_django/execute.py +102 -0
- taskferry_django/management/__init__.py +0 -0
- taskferry_django/management/commands/__init__.py +0 -0
- taskferry_django/management/commands/taskferry.py +76 -0
- taskferry_django/py.typed +0 -0
- taskferry_django/transaction.py +113 -0
- taskferry_django/views.py +135 -0
- taskferry_django-0.2.0.dist-info/METADATA +132 -0
- taskferry_django-0.2.0.dist-info/RECORD +16 -0
- taskferry_django-0.2.0.dist-info/WHEEL +4 -0
- taskferry_django-0.2.0.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"""The worker-side dispatcher for Django tasks.
|
|
2
|
+
|
|
3
|
+
```mermaid
|
|
4
|
+
flowchart LR
|
|
5
|
+
SPEC["TaskSpec<br/>task = execute:run_task<br/>args = (module_path, args, kwargs)"]
|
|
6
|
+
ENG["any engine"]
|
|
7
|
+
RUN["run_task()"]
|
|
8
|
+
T["django.tasks Task"]
|
|
9
|
+
FN["your function"]
|
|
10
|
+
|
|
11
|
+
SPEC --> ENG --> RUN --> T --> FN
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Why a dispatcher rather than the task's own path
|
|
15
|
+
------------------------------------------------
|
|
16
|
+
|
|
17
|
+
``@task`` replaces the decorated function with a ``django.tasks.base.Task``
|
|
18
|
+
object, which is *not* callable — it exposes ``.call()`` and keeps the original
|
|
19
|
+
function in ``.func``. So ``myapp.tasks:resize_image`` resolves to the Task, and
|
|
20
|
+
a generic worker calling it would fail.
|
|
21
|
+
|
|
22
|
+
Taskferry could special-case that, but only by teaching the core what a Django
|
|
23
|
+
Task is, which is precisely the coupling this project exists to prevent. Instead
|
|
24
|
+
the bridge does what the Procrastinate adapter already does for its own reasons:
|
|
25
|
+
route every Django task through one fixed entry point that knows how to run one.
|
|
26
|
+
|
|
27
|
+
Two useful consequences:
|
|
28
|
+
|
|
29
|
+
* **Allowlisting is trivial.** Only ``taskferry_django.execute`` needs to be
|
|
30
|
+
importable by the worker. The Django task path is data inside the payload,
|
|
31
|
+
resolved through Django's own registry, not through an arbitrary import.
|
|
32
|
+
* **Django's semantics are preserved.** ``Task.call()`` is what Django's own
|
|
33
|
+
backends invoke, so signature handling, context and validation behave exactly
|
|
34
|
+
as they do under ``ImmediateBackend``.
|
|
35
|
+
|
|
36
|
+
The cost is that ``spec.task`` is the same string for every Django task, so
|
|
37
|
+
:attr:`~taskferry.specs.ExecutionSpec.name` carries the real task name and routing
|
|
38
|
+
by ``name`` keeps working. That is stated here rather than discovered later.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
from __future__ import annotations
|
|
42
|
+
|
|
43
|
+
import importlib
|
|
44
|
+
from typing import Any
|
|
45
|
+
|
|
46
|
+
from django.tasks.base import Task
|
|
47
|
+
|
|
48
|
+
from taskferry.core.typing import JSONValue
|
|
49
|
+
from taskferry.errors import FunctionResolutionError
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def resolve_django_task(module_path: str) -> Task:
|
|
53
|
+
"""Resolve ``"myapp.tasks.resize_image"`` to the ``Task`` object.
|
|
54
|
+
|
|
55
|
+
Raises:
|
|
56
|
+
FunctionResolutionError: when the path does not resolve to a Django Task.
|
|
57
|
+
Being strict matters: this runs on a worker, on a payload that came
|
|
58
|
+
off a queue, and "import whatever this string says and call it" is
|
|
59
|
+
not something to do casually.
|
|
60
|
+
"""
|
|
61
|
+
module_name, _, attribute = module_path.rpartition(".")
|
|
62
|
+
if not module_name or not attribute:
|
|
63
|
+
raise FunctionResolutionError(
|
|
64
|
+
f"{module_path!r} is not a Django task path; expected 'package.module.task_name'"
|
|
65
|
+
)
|
|
66
|
+
try:
|
|
67
|
+
module = importlib.import_module(module_name)
|
|
68
|
+
candidate = getattr(module, attribute)
|
|
69
|
+
except (ImportError, AttributeError) as exc:
|
|
70
|
+
raise FunctionResolutionError(f"cannot resolve Django task {module_path!r}: {exc}") from exc
|
|
71
|
+
if not isinstance(candidate, Task):
|
|
72
|
+
raise FunctionResolutionError(
|
|
73
|
+
f"{module_path!r} resolved to {type(candidate).__name__}, not a django.tasks Task"
|
|
74
|
+
)
|
|
75
|
+
return candidate
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def run_task(
|
|
79
|
+
module_path: str,
|
|
80
|
+
args: list[JSONValue] | None = None,
|
|
81
|
+
kwargs: dict[str, JSONValue] | None = None,
|
|
82
|
+
) -> Any:
|
|
83
|
+
"""Run a Django task by path. The single entry point every engine calls.
|
|
84
|
+
|
|
85
|
+
Args:
|
|
86
|
+
module_path: Django's own ``Task.module_path``.
|
|
87
|
+
args: Positional arguments, JSON-shaped.
|
|
88
|
+
kwargs: Keyword arguments, JSON-shaped.
|
|
89
|
+
|
|
90
|
+
Returns:
|
|
91
|
+
Whatever the task returned. Most engines discard it; backends that
|
|
92
|
+
advertise ``RESULT`` surface it through the handle.
|
|
93
|
+
"""
|
|
94
|
+
task = resolve_django_task(module_path)
|
|
95
|
+
return task.call(*(args or []), **(kwargs or {}))
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
DISPATCH_REF = "taskferry_django.execute:run_task"
|
|
99
|
+
"""The portable reference every Django-originated `TaskSpec` points at."""
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
__all__ = ["DISPATCH_REF", "resolve_django_task", "run_task"]
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""``manage.py taskferry`` — the same CLI, with Django's settings already loaded.
|
|
2
|
+
|
|
3
|
+
```mermaid
|
|
4
|
+
flowchart LR
|
|
5
|
+
MC["manage.py taskferry doctor"]
|
|
6
|
+
CFG["settings.TASKFERRY"]
|
|
7
|
+
CLI["taskferry.cli"]
|
|
8
|
+
|
|
9
|
+
MC --> CFG --> CLI
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Taskferry's own ``taskferry`` command reads ``TASKFERRY_*`` environment variables.
|
|
13
|
+
Inside a Django project the configuration usually lives in settings instead, so
|
|
14
|
+
this command bridges the two: it loads settings, writes the resolved
|
|
15
|
+
configuration to a temporary JSON file, and hands it to the same CLI.
|
|
16
|
+
|
|
17
|
+
It is a wrapper on purpose. The CLI is the primary interface — it works in a
|
|
18
|
+
container that has never heard of Django — and duplicating its argument parsing
|
|
19
|
+
here would guarantee the two drift apart.
|
|
20
|
+
|
|
21
|
+
python manage.py taskferry doctor
|
|
22
|
+
python manage.py taskferry backends
|
|
23
|
+
python manage.py taskferry capabilities pg
|
|
24
|
+
python manage.py taskferry status task_9f2c... --backend pg
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
from __future__ import annotations
|
|
28
|
+
|
|
29
|
+
import json
|
|
30
|
+
import tempfile
|
|
31
|
+
from pathlib import Path
|
|
32
|
+
from typing import Any
|
|
33
|
+
|
|
34
|
+
from django.core.management.base import BaseCommand, CommandError
|
|
35
|
+
|
|
36
|
+
from taskferry.cli import main as cli_main
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class Command(BaseCommand):
|
|
40
|
+
help = "Run a Taskferry CLI subcommand against this project's settings."
|
|
41
|
+
|
|
42
|
+
def add_arguments(self, parser: Any) -> None:
|
|
43
|
+
parser.add_argument(
|
|
44
|
+
"args",
|
|
45
|
+
nargs="*",
|
|
46
|
+
help="Arguments passed straight through to the taskferry CLI.",
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
def handle(self, *args: Any, **options: Any) -> None:
|
|
50
|
+
argv = list(options.get("args") or [])
|
|
51
|
+
if not argv:
|
|
52
|
+
argv = ["doctor"]
|
|
53
|
+
|
|
54
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
55
|
+
config_path = Path(tmp) / "taskferry.json"
|
|
56
|
+
config_path.write_text(json.dumps(self._config_payload()), encoding="utf-8")
|
|
57
|
+
exit_code = cli_main(["--config", str(config_path), *argv])
|
|
58
|
+
|
|
59
|
+
if exit_code != 0:
|
|
60
|
+
raise CommandError(f"taskferry {' '.join(argv)} exited with status {exit_code}")
|
|
61
|
+
|
|
62
|
+
def _config_payload(self) -> dict[str, Any]:
|
|
63
|
+
"""The raw TASKFERRY setting, or the local default when it is absent."""
|
|
64
|
+
from django.conf import settings
|
|
65
|
+
|
|
66
|
+
raw = getattr(settings, "TASKFERRY", None)
|
|
67
|
+
if isinstance(raw, dict):
|
|
68
|
+
return raw
|
|
69
|
+
return {
|
|
70
|
+
"backends": {
|
|
71
|
+
"inline": {"factory": "inline"},
|
|
72
|
+
"thread": {"factory": "thread"},
|
|
73
|
+
"subprocess": {"factory": "subprocess"},
|
|
74
|
+
},
|
|
75
|
+
"defaults": {"inline": "inline", "task": "thread", "job": "subprocess"},
|
|
76
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"""Enqueue after the transaction commits — the classic Django task bug, fixed.
|
|
2
|
+
|
|
3
|
+
```mermaid
|
|
4
|
+
sequenceDiagram
|
|
5
|
+
participant V as view
|
|
6
|
+
participant DB as PostgreSQL
|
|
7
|
+
participant TP as Taskferry
|
|
8
|
+
participant W as worker
|
|
9
|
+
|
|
10
|
+
Note over V,W: without on_commit — the race
|
|
11
|
+
V->>DB: BEGIN; INSERT order
|
|
12
|
+
V->>TP: submit(process_order, id)
|
|
13
|
+
TP->>W: enqueue
|
|
14
|
+
W->>DB: SELECT order ❌ not committed yet
|
|
15
|
+
V->>DB: COMMIT
|
|
16
|
+
|
|
17
|
+
Note over V,W: with submit_on_commit
|
|
18
|
+
V->>DB: BEGIN; INSERT order
|
|
19
|
+
V->>TP: submit_on_commit(...)
|
|
20
|
+
V->>DB: COMMIT
|
|
21
|
+
TP->>W: enqueue
|
|
22
|
+
W->>DB: SELECT order ✓
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
A worker is usually faster than the rest of the request, so a task enqueued
|
|
26
|
+
mid-transaction routinely reaches a worker before the row it needs exists — and
|
|
27
|
+
it fails intermittently, under load, in production, which is the worst possible
|
|
28
|
+
place to discover it.
|
|
29
|
+
|
|
30
|
+
:func:`submit_on_commit` defers the submission to ``transaction.on_commit``. If
|
|
31
|
+
the transaction rolls back, nothing is enqueued at all, which is the other half
|
|
32
|
+
of the correctness argument.
|
|
33
|
+
|
|
34
|
+
This lives in the Django adapter because ``transaction.on_commit`` is Django's,
|
|
35
|
+
and the core cannot know about it. Other frameworks get the same shape from their
|
|
36
|
+
own after-commit hook wrapping ``runtime.submit``.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
from __future__ import annotations
|
|
40
|
+
|
|
41
|
+
from collections.abc import Callable
|
|
42
|
+
from typing import Any
|
|
43
|
+
|
|
44
|
+
from django.db import transaction
|
|
45
|
+
|
|
46
|
+
from taskferry import ExecutionHandle, Taskferry
|
|
47
|
+
from taskferry.specs import AnySpec
|
|
48
|
+
|
|
49
|
+
from .config import get_runtime
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def submit_on_commit(
|
|
53
|
+
spec: AnySpec,
|
|
54
|
+
*,
|
|
55
|
+
runtime: Taskferry | None = None,
|
|
56
|
+
backend: str | None = None,
|
|
57
|
+
using: str | None = None,
|
|
58
|
+
on_submitted: Callable[[ExecutionHandle], None] | None = None,
|
|
59
|
+
) -> None:
|
|
60
|
+
"""Submit ``spec`` once the current transaction commits.
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
spec: What to run.
|
|
64
|
+
runtime: Runtime to submit through. Defaults to the shared one.
|
|
65
|
+
backend: Bypass routing and use this backend.
|
|
66
|
+
using: Database alias whose transaction to hook.
|
|
67
|
+
on_submitted: Called with the handle after submission. The handle cannot
|
|
68
|
+
be returned — it does not exist yet when this function returns, and
|
|
69
|
+
inventing a placeholder for it would be a lie with a ``.cancel()``
|
|
70
|
+
method on it.
|
|
71
|
+
|
|
72
|
+
Outside a transaction (``ATOMIC_REQUESTS`` off, no ``atomic`` block) Django
|
|
73
|
+
runs the callback immediately, so this is always safe to call.
|
|
74
|
+
"""
|
|
75
|
+
target = runtime if runtime is not None else get_runtime()
|
|
76
|
+
|
|
77
|
+
def _submit() -> None:
|
|
78
|
+
handle = target.submit(spec, backend=backend)
|
|
79
|
+
if on_submitted is not None:
|
|
80
|
+
on_submitted(handle)
|
|
81
|
+
|
|
82
|
+
transaction.on_commit(_submit, using=using)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def task_on_commit(
|
|
86
|
+
task: str | Callable[..., Any],
|
|
87
|
+
/,
|
|
88
|
+
*args: Any,
|
|
89
|
+
runtime: Taskferry | None = None,
|
|
90
|
+
using: str | None = None,
|
|
91
|
+
**kwargs: Any,
|
|
92
|
+
) -> None:
|
|
93
|
+
"""Shorthand for the common case: enqueue a task after commit.
|
|
94
|
+
|
|
95
|
+
::
|
|
96
|
+
|
|
97
|
+
def create_order(request):
|
|
98
|
+
with transaction.atomic():
|
|
99
|
+
order = Order.objects.create(...)
|
|
100
|
+
task_on_commit("myapp.tasks:process_order", order.id)
|
|
101
|
+
"""
|
|
102
|
+
target = runtime if runtime is not None else get_runtime()
|
|
103
|
+
ref = target.registry.reference(task)
|
|
104
|
+
from taskferry import TaskSpec
|
|
105
|
+
|
|
106
|
+
submit_on_commit(
|
|
107
|
+
TaskSpec(task=ref.path, args=args, kwargs=kwargs),
|
|
108
|
+
runtime=target,
|
|
109
|
+
using=using,
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
__all__ = ["submit_on_commit", "task_on_commit"]
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"""Django views for push-based task delivery.
|
|
2
|
+
|
|
3
|
+
```mermaid
|
|
4
|
+
flowchart LR
|
|
5
|
+
CT["Cloud Tasks"]
|
|
6
|
+
V["task_webhook"]
|
|
7
|
+
H["execute_envelope"]
|
|
8
|
+
FN["your function"]
|
|
9
|
+
|
|
10
|
+
CT -->|"POST JSON"| V --> H --> FN
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
The agnostic receiver (:func:`taskferry.envelope.execute_envelope`) takes bytes, so
|
|
14
|
+
it works in any framework. This module is the four lines of Django glue on top —
|
|
15
|
+
URL wiring, status codes, CSRF exemption — for projects that would otherwise
|
|
16
|
+
write them by hand.
|
|
17
|
+
|
|
18
|
+
# urls.py
|
|
19
|
+
from taskferry_django.views import task_webhook
|
|
20
|
+
|
|
21
|
+
urlpatterns = [path("_taskferry/execute", task_webhook)]
|
|
22
|
+
|
|
23
|
+
Status codes, and why they differ
|
|
24
|
+
---------------------------------
|
|
25
|
+
|
|
26
|
+
A push service decides whether to retry from the response, so the codes are
|
|
27
|
+
chosen to mean the right thing to it:
|
|
28
|
+
|
|
29
|
+
| Situation | Status | The queue should |
|
|
30
|
+
| --- | --- | --- |
|
|
31
|
+
| ran fine | 204 | acknowledge |
|
|
32
|
+
| body is not a valid envelope | 400 | give up — retrying cannot help |
|
|
33
|
+
| task name will not resolve | 404 | give up — permanent |
|
|
34
|
+
| the task itself raised | 500 | retry, per its own configuration |
|
|
35
|
+
|
|
36
|
+
A malformed body is *not* a 500. Retrying it forever would fill a dead-letter
|
|
37
|
+
queue with a bug that no redelivery can fix.
|
|
38
|
+
|
|
39
|
+
Security
|
|
40
|
+
--------
|
|
41
|
+
|
|
42
|
+
**This endpoint runs code and Taskferry does not authenticate it.** Verify the
|
|
43
|
+
OIDC token your push service sends, or keep the service private. And give
|
|
44
|
+
``REGISTRY`` an import allowlist — the task name arrives over the network. See
|
|
45
|
+
``docs/security.md``; :func:`make_task_webhook` exists to make both easy.
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
from __future__ import annotations
|
|
49
|
+
|
|
50
|
+
import json
|
|
51
|
+
import logging
|
|
52
|
+
from collections.abc import Callable
|
|
53
|
+
from typing import cast
|
|
54
|
+
|
|
55
|
+
from django.http import HttpRequest, HttpResponse, JsonResponse
|
|
56
|
+
from django.views.decorators.csrf import csrf_exempt
|
|
57
|
+
from django.views.decorators.http import require_POST
|
|
58
|
+
|
|
59
|
+
from taskferry.envelope import execute_envelope
|
|
60
|
+
from taskferry.errors import FunctionResolutionError, SerializationError
|
|
61
|
+
from taskferry.functions import FunctionRegistry
|
|
62
|
+
|
|
63
|
+
logger = logging.getLogger("taskferry.django")
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def make_task_webhook(
|
|
67
|
+
*,
|
|
68
|
+
registry: FunctionRegistry | None = None,
|
|
69
|
+
authenticate: Callable[[HttpRequest], bool] | None = None,
|
|
70
|
+
) -> Callable[[HttpRequest], HttpResponse]:
|
|
71
|
+
"""Build a webhook view with your registry and your authentication.
|
|
72
|
+
|
|
73
|
+
The recommended form, because both arguments matter::
|
|
74
|
+
|
|
75
|
+
from taskferry import FunctionRegistry
|
|
76
|
+
from taskferry_django.views import make_task_webhook
|
|
77
|
+
|
|
78
|
+
task_webhook = make_task_webhook(
|
|
79
|
+
registry=FunctionRegistry(allowed_modules=["myapp"]),
|
|
80
|
+
authenticate=verify_oidc_token,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
registry: Where task names resolve. **Give it an allowlist.**
|
|
85
|
+
authenticate: Called with the request; return ``False`` to reject with
|
|
86
|
+
403. Taskferry cannot do this for you — it never sees your framework's
|
|
87
|
+
request object, which is exactly what makes the receiver portable.
|
|
88
|
+
"""
|
|
89
|
+
|
|
90
|
+
@csrf_exempt
|
|
91
|
+
@require_POST
|
|
92
|
+
def view(request: HttpRequest) -> HttpResponse:
|
|
93
|
+
if authenticate is not None and not authenticate(request):
|
|
94
|
+
return JsonResponse({"error": "unauthorized"}, status=403)
|
|
95
|
+
return _execute(request, registry)
|
|
96
|
+
|
|
97
|
+
return cast(Callable[[HttpRequest], HttpResponse], view)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
@csrf_exempt
|
|
101
|
+
@require_POST
|
|
102
|
+
def task_webhook(request: HttpRequest) -> HttpResponse:
|
|
103
|
+
"""Execute a task delivered as a JSON push request.
|
|
104
|
+
|
|
105
|
+
Wired without arguments this resolves task names through a permissive
|
|
106
|
+
registry and performs **no authentication** — appropriate behind IAM or a
|
|
107
|
+
private network, and not otherwise. Prefer :func:`make_task_webhook`.
|
|
108
|
+
"""
|
|
109
|
+
return _execute(request, None)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _execute(request: HttpRequest, registry: FunctionRegistry | None) -> HttpResponse:
|
|
113
|
+
try:
|
|
114
|
+
payload = json.loads(request.body)
|
|
115
|
+
except (ValueError, UnicodeDecodeError) as exc:
|
|
116
|
+
return JsonResponse({"error": f"invalid JSON body: {exc}"}, status=400)
|
|
117
|
+
|
|
118
|
+
try:
|
|
119
|
+
execute_envelope(payload, registry=registry)
|
|
120
|
+
except SerializationError as exc:
|
|
121
|
+
# A malformed or version-skewed envelope. Retrying cannot fix it, so 400
|
|
122
|
+
# tells the queue to stop rather than dead-lettering the same bug 25 times.
|
|
123
|
+
logger.warning("taskferry: rejected a malformed envelope: %s", exc)
|
|
124
|
+
return JsonResponse({"error": str(exc)}, status=400)
|
|
125
|
+
except FunctionResolutionError as exc:
|
|
126
|
+
# The name does not resolve, or the allowlist refused it. Permanent.
|
|
127
|
+
logger.warning("taskferry: could not resolve a pushed task: %s", exc)
|
|
128
|
+
return JsonResponse({"error": str(exc)}, status=404)
|
|
129
|
+
|
|
130
|
+
# Anything the task itself raises propagates: Django turns it into a 500,
|
|
131
|
+
# which is how the push service is told to retry per its own configuration.
|
|
132
|
+
return HttpResponse(status=204)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
__all__ = ["make_task_webhook", "task_webhook"]
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: taskferry-django
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Django integration for Taskferry — a django.tasks backend, settings, checks and CLI.
|
|
5
|
+
Project-URL: Homepage, https://github.com/xiidigital/taskferry
|
|
6
|
+
Project-URL: Documentation, https://taskferry.dev/django
|
|
7
|
+
Project-URL: Source, https://github.com/xiidigital/taskferry
|
|
8
|
+
Author: Taskferry authors
|
|
9
|
+
License-Expression: Apache-2.0
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: cloud-tasks,django,django-tasks,sqs,taskferry
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Framework :: Django
|
|
14
|
+
Classifier: Framework :: Django :: 6.0
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Typing :: Typed
|
|
19
|
+
Requires-Python: >=3.12
|
|
20
|
+
Requires-Dist: django>=5.1
|
|
21
|
+
Requires-Dist: taskferry<0.3,>=0.2
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# taskferry-django
|
|
25
|
+
|
|
26
|
+
Django integration for [Taskferry](https://github.com/xiidigital/taskferry).
|
|
27
|
+
|
|
28
|
+
```mermaid
|
|
29
|
+
flowchart BT
|
|
30
|
+
DJ["Django"]
|
|
31
|
+
TPD["taskferry-django"]
|
|
32
|
+
TP["taskferry"]
|
|
33
|
+
|
|
34
|
+
DJ --> TPD --> TP
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Taskferry does **not** invent a parallel task API — Django 6 already has one. This
|
|
38
|
+
package supplies a backend for the standard `TASKS` setting, so your `@task` code
|
|
39
|
+
never changes while the engine behind it becomes a configuration choice.
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
pip install taskferry-django taskferry-procrastinate
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Your code never changes
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
# myapp/tasks.py
|
|
49
|
+
from django.tasks import task
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@task
|
|
53
|
+
def resize_image(image_id: int) -> None: ...
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
# anywhere
|
|
58
|
+
resize_image.enqueue(42)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Settings
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
TASKS = {"default": {"BACKEND": "taskferry_django.TaskferryBackend"}}
|
|
65
|
+
|
|
66
|
+
TASKFERRY = {
|
|
67
|
+
"backends": {
|
|
68
|
+
"pg": {"factory": "procrastinate", "app": "myapp.tasks:app"},
|
|
69
|
+
"heavy": {"factory": "cloudrun", "project": "p", "location": "europe-west1"},
|
|
70
|
+
},
|
|
71
|
+
"routes": [
|
|
72
|
+
{"kind": "task", "queue": "metadata", "backend": "pg"},
|
|
73
|
+
{"kind": "job", "profile": "heavy", "backend": "heavy"},
|
|
74
|
+
],
|
|
75
|
+
"defaults": {"task": "pg", "job": "heavy"},
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
With no `TASKFERRY` setting at all, everything runs locally — a fresh project works
|
|
80
|
+
immediately and is obviously not production.
|
|
81
|
+
|
|
82
|
+
## What else you get
|
|
83
|
+
|
|
84
|
+
**Enqueue after commit.** The classic race — a worker reaching a row before the
|
|
85
|
+
transaction that created it commits — with the classic fix:
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from django.db import transaction
|
|
89
|
+
from taskferry_django import task_on_commit
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def create_order(request):
|
|
93
|
+
with transaction.atomic():
|
|
94
|
+
order = Order.objects.create(...)
|
|
95
|
+
task_on_commit("myapp.tasks:process_order", order.id)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
**System checks.** `manage.py check` builds every configured backend, because
|
|
99
|
+
"the adapter is installed" and "the adapter works with these options" are
|
|
100
|
+
different questions:
|
|
101
|
+
|
|
102
|
+
```text
|
|
103
|
+
taskferry.E002 the Taskferry backend 'heavy' cannot be built: CloudRunJobBackend
|
|
104
|
+
needs both 'project' and 'location'
|
|
105
|
+
taskferry.W003 DEBUG is False but these Taskferry backends run in this process and
|
|
106
|
+
lose pending work on restart: fast
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
**The CLI, with settings loaded.**
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
python manage.py taskferry doctor
|
|
113
|
+
python manage.py taskferry capabilities pg
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
**Jobs and inline execution**, through the same runtime:
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
from taskferry_django import get_runtime
|
|
120
|
+
|
|
121
|
+
get_runtime().jobs.submit("build-cog", image="gdal:latest", profile="heavy")
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## The dependency direction
|
|
125
|
+
|
|
126
|
+
`taskferry` never imports Django. The test suite for the core runs — and CI runs
|
|
127
|
+
it — in an environment where Django is not installed. The same `TASKFERRY` dict
|
|
128
|
+
works verbatim in a FastAPI service, a CLI or a library.
|
|
129
|
+
|
|
130
|
+
## License
|
|
131
|
+
|
|
132
|
+
Apache-2.0.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
taskferry_django/__init__.py,sha256=gUrdtubQ1c-ht8WHBOH850C3Gqwd6uOpK6lxR5Uy7UI,2613
|
|
2
|
+
taskferry_django/apps.py,sha256=coanDDQHO5UcuirB_oWC4spElNcFZHvSs6rClwdPUxk,1292
|
|
3
|
+
taskferry_django/backend.py,sha256=-PWn01Pg_tYArz2v4eVrbu7i2_QdW3gszub8sdQf8Bk,8511
|
|
4
|
+
taskferry_django/checks.py,sha256=ovHs3-Kh5RdvT4RwyJyrHTwF1MOEBD-ttmtlBHnSD1U,6653
|
|
5
|
+
taskferry_django/config.py,sha256=8N32IcF0bg5w-vLsRxflNg95ccPV_JUbSBkAjgi40gA,4107
|
|
6
|
+
taskferry_django/execute.py,sha256=NgVMr2CbK-XtixIvv2vtihVIgjLnhb24HgDAFo80mwk,3868
|
|
7
|
+
taskferry_django/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
+
taskferry_django/transaction.py,sha256=95jz3kMZMFsg1mIKsFcEOjTZ_OqE-xgBJ1zb9ZrupCk,3516
|
|
9
|
+
taskferry_django/views.py,sha256=qO25Kg8u6ocKRgW39dgpqFX9q-MIqNDCijX4TLXD5zY,4904
|
|
10
|
+
taskferry_django/management/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
11
|
+
taskferry_django/management/commands/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
12
|
+
taskferry_django/management/commands/taskferry.py,sha256=zimHSMIWE0MDlr43iZQnoNLftC6wz3ftM1YOiyI3HcY,2605
|
|
13
|
+
taskferry_django-0.2.0.dist-info/METADATA,sha256=gbCONqvWuXMZhsavRYPOIABzU53yZTTu15qNVGMmAPA,3721
|
|
14
|
+
taskferry_django-0.2.0.dist-info/WHEEL,sha256=THafob7ofN-NsuMN7Mg4qZyHaQI7KkD-QlcQatYhXPo,87
|
|
15
|
+
taskferry_django-0.2.0.dist-info/licenses/LICENSE,sha256=de-gfE0q-xTYImzwC3dj3S7BxVhanf6RmIGjo_7y3aw,11357
|
|
16
|
+
taskferry_django-0.2.0.dist-info/RECORD,,
|