-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_queue.py
More file actions
191 lines (156 loc) · 5.79 KB
/
Copy pathtask_queue.py
File metadata and controls
191 lines (156 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""Durable work queue pattern using NATS JetStream.
Distributes tasks to worker agents with acknowledgment, retry,
and exponential backoff. Built for AI agent task delegation.
"""
from __future__ import annotations
import json
import uuid
from dataclasses import asdict, dataclass, field
from datetime import datetime, timezone
from typing import Any, Callable, Awaitable
from nats.aio.client import Client as NATSClient
from nats.js.api import (
ConsumerConfig,
DeliverPolicy,
AckPolicy,
StreamConfig,
RetentionPolicy,
)
@dataclass
class Task:
"""A unit of work to be processed by a worker agent."""
type: str
payload: dict[str, Any]
id: str = field(default_factory=lambda: str(uuid.uuid4()))
priority: int = 0
created_at: str = field(
default_factory=lambda: datetime.now(timezone.utc).isoformat()
)
retry_count: int = 0
def serialize(self) -> bytes:
return json.dumps(asdict(self)).encode()
@classmethod
def deserialize(cls, data: bytes) -> Task:
d = json.loads(data.decode())
return cls(**d)
class TaskQueue:
"""Durable task queue backed by NATS JetStream.
Creates a stream for task distribution with pull-based consumers,
acknowledgment, and automatic redelivery on failure.
Usage:
queue = TaskQueue(nc, stream_name="agent-tasks")
await queue.setup()
# Producer
await queue.enqueue(Task(type="analyze", payload={"url": "..."}))
# Consumer
await queue.worker(my_handler, durable_name="worker-1")
"""
MAX_RETRIES = 3
BACKOFF_BASE_SECONDS = 2.0
def __init__(
self,
nc: NATSClient,
stream_name: str = "TASKS",
org: str = "default",
) -> None:
self._nc = nc
self._stream_name = stream_name
self._org = org
self._js = None
self._subject = f"{org}.tasks.>"
@property
def subject_prefix(self) -> str:
return f"{self._org}.tasks"
async def setup(self) -> None:
"""Initialize the JetStream context and create the stream."""
self._js = self._nc.jetstream()
await self._js.add_stream(
StreamConfig(
name=self._stream_name,
subjects=[self._subject],
retention=RetentionPolicy.WORK_QUEUE,
)
)
async def enqueue(self, task: Task, task_subject: str | None = None) -> None:
"""Publish a task to the queue.
Args:
task: The Task to enqueue.
task_subject: Optional subject suffix (defaults to task.type).
"""
if self._js is None:
raise RuntimeError("TaskQueue not set up. Call setup() first.")
subject = task_subject or f"{self.subject_prefix}.{task.type}"
await self._js.publish(subject, task.serialize())
async def worker(
self,
handler: Callable[[Task], Awaitable[None]],
durable_name: str,
task_type: str = "*",
):
"""Start consuming tasks from the queue.
Tasks are acknowledged on success. On failure, the message is
nak'd with a backoff delay so JetStream redelivers it.
Args:
handler: Async function that processes a Task. Raise to signal failure.
durable_name: Durable consumer name (survives restarts).
task_type: Filter to specific task type, or '*' for all.
Returns:
The pull subscription (for lifecycle management).
"""
if self._js is None:
raise RuntimeError("TaskQueue not set up. Call setup() first.")
filter_subject = f"{self.subject_prefix}.{task_type}"
sub = await self._js.pull_subscribe(
filter_subject,
durable=durable_name,
config=ConsumerConfig(
durable_name=durable_name,
ack_policy=AckPolicy.EXPLICIT,
deliver_policy=DeliverPolicy.ALL,
max_deliver=self.MAX_RETRIES + 1,
ack_wait=30,
),
)
return _WorkerSubscription(sub, handler, max_retries=self.MAX_RETRIES)
class _WorkerSubscription:
"""Wraps a pull subscription with processing logic."""
def __init__(self, sub, handler, max_retries: int = 3) -> None:
self._sub = sub
self._handler = handler
self._max_retries = max_retries
self._running = False
async def process_batch(self, batch_size: int = 10, timeout: float = 5.0) -> int:
"""Fetch and process a batch of tasks.
Args:
batch_size: Max number of tasks to fetch at once.
timeout: How long to wait for messages.
Returns:
Number of tasks successfully processed in this batch.
"""
try:
messages = await self._sub.fetch(batch=batch_size, timeout=timeout)
except Exception:
return 0
processed = 0
for msg in messages:
task = Task.deserialize(msg.data)
try:
await self._handler(task)
await msg.ack()
processed += 1
except Exception:
retry = msg.metadata.num_delivered if msg.metadata else task.retry_count
if retry >= self._max_retries:
await msg.term()
else:
backoff = TaskQueue.BACKOFF_BASE_SECONDS * (2 ** retry)
await msg.nak(delay=backoff)
return processed
async def run(self, batch_size: int = 10, timeout: float = 5.0) -> None:
"""Continuously process tasks until stopped."""
self._running = True
while self._running:
await self.process_batch(batch_size, timeout)
def stop(self) -> None:
"""Signal the worker to stop after the current batch."""
self._running = False