taskferry 0.2.0__tar.gz → 0.2.1__tar.gz

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.
Files changed (50) hide show
  1. {taskferry-0.2.0 → taskferry-0.2.1}/CHANGELOG.md +22 -14
  2. {taskferry-0.2.0 → taskferry-0.2.1}/PKG-INFO +2 -2
  3. {taskferry-0.2.0 → taskferry-0.2.1}/README.md +1 -1
  4. {taskferry-0.2.0 → taskferry-0.2.1}/pyproject.toml +1 -1
  5. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/__init__.py +1 -1
  6. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/backends/process.py +1 -1
  7. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/backends/thread.py +14 -2
  8. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/capabilities.py +9 -0
  9. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/core/__init__.py +3 -4
  10. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/envelope.py +22 -2
  11. taskferry-0.2.1/src/taskferry/execution_context.py +49 -0
  12. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/handle.py +12 -0
  13. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/ports.py +18 -0
  14. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/specs.py +3 -1
  15. {taskferry-0.2.0 → taskferry-0.2.1}/.gitignore +0 -0
  16. {taskferry-0.2.0 → taskferry-0.2.1}/LICENSE +0 -0
  17. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/aio.py +0 -0
  18. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/backends/__init__.py +0 -0
  19. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/backends/inline.py +0 -0
  20. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/backends/subprocess.py +0 -0
  21. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/cli.py +0 -0
  22. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/config.py +0 -0
  23. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/contract/__init__.py +0 -0
  24. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/contract/base.py +0 -0
  25. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/contract/inline.py +0 -0
  26. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/contract/job.py +0 -0
  27. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/contract/task.py +0 -0
  28. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/core/capabilities.py +0 -0
  29. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/core/config.py +0 -0
  30. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/core/correlation.py +0 -0
  31. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/core/delivery.py +0 -0
  32. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/core/errors.py +0 -0
  33. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/core/ids.py +0 -0
  34. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/core/observability.py +0 -0
  35. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/core/otel.py +0 -0
  36. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/core/provider.py +0 -0
  37. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/core/py.typed +0 -0
  38. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/core/registry.py +0 -0
  39. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/core/serialization.py +0 -0
  40. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/core/typing.py +0 -0
  41. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/errors.py +0 -0
  42. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/execution.py +0 -0
  43. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/functions.py +0 -0
  44. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/hooks.py +0 -0
  45. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/plugins.py +0 -0
  46. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/py.typed +0 -0
  47. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/retry.py +0 -0
  48. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/router.py +0 -0
  49. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/runtime.py +0 -0
  50. {taskferry-0.2.0 → taskferry-0.2.1}/src/taskferry/tracking.py +0 -0
@@ -4,28 +4,41 @@ All notable changes to `taskferry` are documented here.
4
4
  The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and
5
5
  this project adheres to [Semantic Versioning](https://semver.org/).
6
6
 
7
+ ## Unreleased
8
+
9
+ ## [0.2.1] — 2026-11-27
10
+
11
+ ### Added
12
+
13
+ - Provider-observed `TaskExecutionContext` for portable execution identity,
14
+ attempt number, worker id and start time where an adapter can report them.
15
+ - `ATTEMPT_COUNT` and `DELETE_PENDING` capabilities for provider operations that
16
+ are distinct from durable state and cancellation.
17
+
18
+ ### Changed
19
+
20
+ - `ExecutionHandle` exposes capability-checked `attempt_count()` and
21
+ `delete_pending()` operations.
22
+
7
23
  ## [0.2.0] — 2026-07-26
8
24
 
9
- The distribution formerly published as `taskferry-core`, reframed as the portable
10
- execution layer itself. See [ADR-0001](../../docs/adr/0001-portable-execution-layer.md)
11
- and the [migration guide](../../docs/migration/0.1-to-0.2.md).
25
+ This release defines the portable Taskferry execution runtime. See
26
+ [ADR-0001](../../docs/adr/0001-portable-execution-layer.md) for its design.
12
27
 
13
28
  ### Added
14
29
 
15
30
  - **A public API.** `from taskferry import Taskferry, TaskSpec, JobSpec, Execution,
16
- ExecutionHandle` now works; `taskferry` is a regular package with a curated
31
+ ExecutionHandle` is available from a regular package with a curated
17
32
  `__all__`.
18
33
  - **Three execution primitives** — `InlineSpec`, `TaskSpec`, `JobSpec` — modelled
19
- framework-agnostically for the first time. Tasks previously existed only inside
20
- Django.
34
+ independently of any framework.
21
35
  - **`Taskferry` runtime** with `inline` / `tasks` / `jobs` facades, lazy backend
22
36
  construction, a bounded execution index and context-manager lifecycle.
23
37
  - **`Router`** with ordered, glob-capable rules on kind, queue, profile, name and
24
38
  labels; first match wins, and an unroutable spec names every rule it tried.
25
39
  - **`Execution`, `ExecutionState`, `ExecutionHandle`** — one portable identity and
26
40
  one documented state machine shared by every backend of every kind.
27
- - **A single `Capability` enum** shared across kinds, replacing the per-domain
28
- enums that could not be compared.
41
+ - **A single `Capability` enum** shared across execution kinds.
29
42
  - **`RetryPolicy` / `TimeoutPolicy`** with explicit `RetryOwner`, so exactly one
30
43
  layer retries. See [ADR-0011](../../docs/adr/0011-retry-ownership.md).
31
44
  - **`FunctionRef` / `FunctionRegistry`** — the portable
@@ -48,8 +61,7 @@ and the [migration guide](../../docs/migration/0.1-to-0.2.md).
48
61
 
49
62
  ### Changed
50
63
 
51
- - `taskferry-core` is absorbed into `taskferry`. `taskferry.core` remains a valid
52
- import path.
64
+ - The `taskferry` distribution includes the `taskferry.core` module.
53
65
  - `QUEUED → SUCCEEDED` is a legal transition: a worker that finishes before the
54
66
  first poll is normal, not a broken adapter.
55
67
 
@@ -58,7 +70,3 @@ and the [migration guide](../../docs/migration/0.1-to-0.2.md).
58
70
  - `UnsupportedCapability` is now an alias of `UnsupportedCapabilityError` rather
59
71
  than a subclass. As a subclass, the documented `except UnsupportedCapability`
60
72
  missed everything raised by `CapabilitySet.require()`.
61
-
62
- ### Removed
63
-
64
- - `taskferry-core` as a separate distribution. Depend on `taskferry`.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: taskferry
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Summary: A portable execution layer for Python — inline, tasks and jobs on the engines you already run.
5
5
  Project-URL: Homepage, https://github.com/xiidigital/taskferry
6
6
  Project-URL: Documentation, https://taskferry.dev
@@ -101,7 +101,7 @@ taskferry doctor
101
101
 
102
102
  ## Documentation
103
103
 
104
- Full documentation, ADRs and the migration guide live in the
104
+ The architecture guide and ADRs live in the
105
105
  [repository](https://github.com/xiidigital/taskferry).
106
106
 
107
107
  ## License
@@ -78,7 +78,7 @@ taskferry doctor
78
78
 
79
79
  ## Documentation
80
80
 
81
- Full documentation, ADRs and the migration guide live in the
81
+ The architecture guide and ADRs live in the
82
82
  [repository](https://github.com/xiidigital/taskferry).
83
83
 
84
84
  ## License
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "taskferry"
7
- version = "0.2.0"
7
+ version = "0.2.1"
8
8
  description = "A portable execution layer for Python — inline, tasks and jobs on the engines you already run."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.12"
@@ -129,7 +129,7 @@ from .specs import (
129
129
  TaskSpec,
130
130
  )
131
131
 
132
- __version__ = "0.2.0"
132
+ __version__ = "0.2.1"
133
133
 
134
134
  # Grouped by concept rather than alphabetically: this list doubles as the map of
135
135
  # the public API, and "everything about executions, together" is far more useful
@@ -157,7 +157,7 @@ class ProcessTaskBackend(BaseBackend):
157
157
  assert isinstance(spec, TaskSpec)
158
158
  pool = self._ensure_pool()
159
159
  execution = Execution(
160
- id=new_execution_id(ExecutionKind.TASK),
160
+ id=spec.execution_id or new_execution_id(ExecutionKind.TASK),
161
161
  kind=ExecutionKind.TASK,
162
162
  backend=self.name,
163
163
  state=ExecutionState.QUEUED,
@@ -56,6 +56,7 @@ from ..execution import (
56
56
  ExecutionState,
57
57
  new_execution_id,
58
58
  )
59
+ from ..execution_context import TaskExecutionContext, use_task_execution
59
60
  from ..functions import FunctionRegistry, is_async_callable
60
61
  from ..ports import BaseBackend
61
62
  from ..specs import ExecutionSpec, TaskSpec
@@ -69,6 +70,7 @@ THREAD_CAPABILITIES = frozenset(
69
70
  Capability.DELAY,
70
71
  Capability.RETRY,
71
72
  Capability.ASYNC_CALLABLE,
73
+ Capability.EXECUTION_CONTEXT,
72
74
  }
73
75
  )
74
76
 
@@ -139,7 +141,7 @@ class ThreadTaskBackend(BaseBackend):
139
141
  raise SubmissionError("this ThreadTaskBackend is closed", backend=self.name)
140
142
 
141
143
  execution = Execution(
142
- id=new_execution_id(ExecutionKind.TASK),
144
+ id=spec.execution_id or new_execution_id(ExecutionKind.TASK),
143
145
  kind=ExecutionKind.TASK,
144
146
  backend=self.name,
145
147
  state=ExecutionState.QUEUED,
@@ -190,7 +192,17 @@ class ThreadTaskBackend(BaseBackend):
190
192
  )
191
193
  while True:
192
194
  try:
193
- with bind:
195
+ with (
196
+ bind,
197
+ use_task_execution(
198
+ TaskExecutionContext(
199
+ execution_id=entry.execution.id,
200
+ attempt=attempt,
201
+ worker_id=threading.current_thread().name,
202
+ started_at=datetime.now(UTC),
203
+ )
204
+ ),
205
+ ):
194
206
  value = self._call(func, spec)
195
207
  except Exception as exc:
196
208
  if spec.retry.should_retry(exc, attempt):
@@ -58,6 +58,15 @@ class Capability(_CapabilityBase):
58
58
  """Cancel a queued or running execution."""
59
59
 
60
60
  RETRY = "retry"
61
+
62
+ EXECUTION_CONTEXT = "execution_context"
63
+ """Provide portable execution identity and a truthful current attempt to workers."""
64
+
65
+ ATTEMPT_COUNT = "attempt_count"
66
+ """Report provider dispatch attempts while the provider still retains the task."""
67
+
68
+ DELETE_PENDING = "delete_pending"
69
+ """Delete a queued/delivered provider task without promising to stop running work."""
61
70
  """Apply a :class:`~taskferry.retry.RetryPolicy` natively."""
62
71
 
63
72
  TIMEOUT = "timeout"
@@ -1,4 +1,4 @@
1
- """``taskferry.core`` — the tiny shared substrate for the Taskferry family.
1
+ """``taskferry.core`` — the shared core for the Taskferry runtime.
2
2
 
3
3
  This package deliberately stays small (ADR-0004). It contains only what is
4
4
  *genuinely* transversal across Tasks, Jobs, Events and Schedules: identifiers,
@@ -9,9 +9,8 @@ the root error hierarchy.
9
9
  Domain types (``JobSpec``, ``Event``, ``Schedule`` ...) live in their own
10
10
  packages, not here — centralization is not a reason to promote a type.
11
11
 
12
- End users normally do not depend on ``taskferry-core`` directly; they install
13
- ``taskferry-django`` / ``taskferry-jobs`` / ``taskferry-events`` /
14
- ``taskferry-scheduler``, which depend on it.
12
+ Framework and provider adapters depend on the ``taskferry`` distribution, which
13
+ provides this core package.
15
14
  """
16
15
 
17
16
  from __future__ import annotations
@@ -44,11 +44,13 @@ an unfamiliar version rather than guessing.
44
44
 
45
45
  from __future__ import annotations
46
46
 
47
+ from datetime import UTC, datetime
47
48
  from typing import Any, TypedDict
48
49
 
49
50
  from .core.correlation import Correlation
50
51
  from .core.typing import JSONValue
51
52
  from .errors import SerializationError
53
+ from .execution import ExecutionId
52
54
  from .retry import NO_RETRY, Backoff, RetryPolicy
53
55
  from .specs import TaskSpec
54
56
 
@@ -68,6 +70,7 @@ class Envelope(TypedDict, total=False):
68
70
  correlation: dict[str, str] | None
69
71
  labels: dict[str, str]
70
72
  retry: dict[str, JSONValue] | None
73
+ execution_id: str
71
74
 
72
75
 
73
76
  def build_envelope(spec: TaskSpec) -> Envelope:
@@ -86,6 +89,7 @@ def build_envelope(spec: TaskSpec) -> Envelope:
86
89
  "correlation": spec.correlation.to_headers() if spec.correlation else None,
87
90
  "labels": dict(spec.labels),
88
91
  "retry": encode_retry(spec.retry),
92
+ **({"execution_id": str(spec.execution_id)} if spec.execution_id else {}),
89
93
  }
90
94
 
91
95
 
@@ -155,7 +159,13 @@ def decode_retry(envelope: Envelope) -> RetryPolicy:
155
159
  )
156
160
 
157
161
 
158
- def execute_envelope(payload: Any, *, registry: Any = None) -> Any:
162
+ def execute_envelope(
163
+ payload: Any,
164
+ *,
165
+ registry: Any = None,
166
+ attempt: int | None = None,
167
+ worker_id: str | None = None,
168
+ ) -> Any:
159
169
  """Validate, resolve and run one envelope. The whole worker side, in one call.
160
170
 
161
171
  Args:
@@ -177,7 +187,17 @@ def execute_envelope(payload: Any, *, registry: Any = None) -> Any:
177
187
  args = list(envelope.get("args") or [])
178
188
  kwargs = dict(envelope.get("kwargs") or {})
179
189
 
180
- with use_correlation(read_correlation(envelope)):
190
+ from .execution_context import TaskExecutionContext, use_task_execution
191
+
192
+ context = TaskExecutionContext(
193
+ execution_id=(
194
+ None if not envelope.get("execution_id") else ExecutionId(str(envelope["execution_id"]))
195
+ ),
196
+ attempt=attempt,
197
+ worker_id=worker_id,
198
+ started_at=datetime.now(UTC),
199
+ )
200
+ with use_correlation(read_correlation(envelope)), use_task_execution(context):
181
201
  if is_async_callable(func):
182
202
  import asyncio
183
203
 
@@ -0,0 +1,49 @@
1
+ """Ambient metadata for the currently executing Taskferry task.
2
+
3
+ Adapters set only facts they can observe. Consumers may use this context to
4
+ bridge execution metadata into their own task framework without coupling the
5
+ Taskferry core to that framework.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from collections.abc import Iterator
11
+ from contextlib import contextmanager
12
+ from contextvars import ContextVar, Token
13
+ from dataclasses import dataclass
14
+ from datetime import datetime
15
+
16
+ from .execution import ExecutionId
17
+
18
+
19
+ @dataclass(frozen=True, slots=True)
20
+ class TaskExecutionContext:
21
+ """Provider-observed metadata for one delivery attempt."""
22
+
23
+ execution_id: ExecutionId | None = None
24
+ attempt: int | None = None
25
+ worker_id: str | None = None
26
+ started_at: datetime | None = None
27
+
28
+
29
+ _CURRENT: ContextVar[TaskExecutionContext | None] = ContextVar(
30
+ "taskferry_task_execution_context", default=None
31
+ )
32
+
33
+
34
+ def current_task_execution() -> TaskExecutionContext | None:
35
+ """Return the execution metadata set by the active backend, if any."""
36
+ return _CURRENT.get()
37
+
38
+
39
+ @contextmanager
40
+ def use_task_execution(context: TaskExecutionContext) -> Iterator[None]:
41
+ """Set execution metadata for the duration of a worker call."""
42
+ token: Token[TaskExecutionContext | None] = _CURRENT.set(context)
43
+ try:
44
+ yield
45
+ finally:
46
+ _CURRENT.reset(token)
47
+
48
+
49
+ __all__ = ["TaskExecutionContext", "current_task_execution", "use_task_execution"]
@@ -182,5 +182,17 @@ class ExecutionHandle:
182
182
  self._execution = cancelled
183
183
  return cancelled
184
184
 
185
+ def attempt_count(self) -> int:
186
+ """Read provider dispatch attempts while its task record still exists."""
187
+ if not self._backend.capabilities.supports(Capability.ATTEMPT_COUNT):
188
+ raise UnsupportedCapability(Capability.ATTEMPT_COUNT.value, provider=self.backend)
189
+ return self._backend.attempt_count(self.external_id or self.id)
190
+
191
+ def delete_pending(self) -> None:
192
+ """Delete a pending provider delivery without promising to stop a running handler."""
193
+ if not self._backend.capabilities.supports(Capability.DELETE_PENDING):
194
+ raise UnsupportedCapability(Capability.DELETE_PENDING.value, provider=self.backend)
195
+ self._backend.delete_pending(self.external_id or self.id)
196
+
185
197
 
186
198
  __all__ = ["ExecutionHandle"]
@@ -111,6 +111,10 @@ class ExecutionBackend(Protocol):
111
111
  """Block until the execution is terminal. Requires ``Capability.STATE``."""
112
112
  ...
113
113
 
114
+ def attempt_count(self, execution_id: ExecutionId | str) -> int: ...
115
+
116
+ def delete_pending(self, execution_id: ExecutionId | str) -> None: ...
117
+
114
118
  # -- the async surface --------------------------------------------------- #
115
119
  # Every backend has one. :class:`BaseBackend` derives it from the sync
116
120
  # methods with ``asyncio.to_thread``, so an adapter gets a correct — never
@@ -210,6 +214,12 @@ class BaseBackend(ABC):
210
214
  ) -> ExecutionResult: # pragma: no cover - overridden
211
215
  raise UnsupportedCapability(Capability.RESULT.value, provider=self.name)
212
216
 
217
+ def _attempt_count(self, execution_id: ExecutionId) -> int:
218
+ raise UnsupportedCapability(Capability.ATTEMPT_COUNT.value, provider=self.name)
219
+
220
+ def _delete_pending(self, execution_id: ExecutionId) -> None:
221
+ raise UnsupportedCapability(Capability.DELETE_PENDING.value, provider=self.name)
222
+
213
223
  def _wait(self, execution_id: ExecutionId, *, timeout: float | None) -> Execution:
214
224
  """Block until terminal. Default implementation polls ``_get``.
215
225
 
@@ -265,6 +275,14 @@ class BaseBackend(ABC):
265
275
  self.capabilities.require(Capability.STATE)
266
276
  return self._wait(ExecutionId(str(execution_id)), timeout=timeout)
267
277
 
278
+ def attempt_count(self, execution_id: ExecutionId | str) -> int:
279
+ self.capabilities.require(Capability.ATTEMPT_COUNT)
280
+ return self._attempt_count(ExecutionId(str(execution_id)))
281
+
282
+ def delete_pending(self, execution_id: ExecutionId | str) -> None:
283
+ self.capabilities.require(Capability.DELETE_PENDING)
284
+ self._delete_pending(ExecutionId(str(execution_id)))
285
+
268
286
  # -- the async surface ---------------------------------------------------- #
269
287
  # Derived from the sync methods by default. ``asyncio.to_thread`` runs the
270
288
  # blocking call on a worker thread, so the caller's event loop keeps
@@ -50,7 +50,7 @@ from taskferry.core.serialization import ensure_json_serializable
50
50
  from taskferry.core.typing import JSONObject, JSONValue
51
51
 
52
52
  from .capabilities import Capability
53
- from .execution import ExecutionKind
53
+ from .execution import ExecutionId, ExecutionKind
54
54
  from .functions import FunctionRef
55
55
  from .retry import NO_RETRY, NO_TIMEOUT, RetryPolicy, TimeoutPolicy
56
56
 
@@ -118,6 +118,8 @@ class ExecutionSpec:
118
118
  correlation: Correlation | None = None
119
119
  labels: Mapping[str, str] = field(default_factory=dict)
120
120
  backend_options: BackendOptions = field(default_factory=BackendOptions)
121
+ execution_id: ExecutionId | None = None
122
+ """Optional portable id supplied by an integration that must carry it in its payload."""
121
123
 
122
124
  def __post_init__(self) -> None:
123
125
  object.__setattr__(self, "labels", MappingProxyType(dict(self.labels)))
File without changes
File without changes