-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_task_queue.py
More file actions
389 lines (324 loc) · 12.6 KB
/
Copy pathasync_task_queue.py
File metadata and controls
389 lines (324 loc) · 12.6 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
"""
Async Task Queue with Priority and Retries
A production-ready async task queue implementation with priority levels,
automatic retries with exponential backoff, and dead-letter queue handling.
"""
import asyncio
import heapq
import logging
import time
import uuid
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from enum import Enum
from typing import Any, Awaitable, Callable, Dict, List, Optional, Tuple
from collections import defaultdict
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
class TaskStatus(Enum):
"""Enumeration of possible task statuses."""
PENDING = "pending"
PROCESSING = "processing"
COMPLETED = "completed"
FAILED = "failed"
RETRYING = "retrying"
class Priority(Enum):
"""Task priority levels."""
LOW = 3
NORMAL = 2
HIGH = 1
CRITICAL = 0
@dataclass
class Task:
"""
Represents a task in the queue.
Attributes:
id: Unique identifier for the task
func: The async function to execute
args: Positional arguments for the function
kwargs: Keyword arguments for the function
priority: Priority level of the task
max_retries: Maximum number of retry attempts
retry_count: Current number of retry attempts
backoff_factor: Factor for exponential backoff
status: Current status of the task
created_at: Timestamp when task was created
scheduled_at: Timestamp when task is scheduled to run
completed_at: Timestamp when task was completed
error: Error message if task failed
"""
id: str = field(default_factory=lambda: str(uuid.uuid4()))
func: Optional[Callable[..., Awaitable[Any]]] = None
args: Tuple[Any, ...] = field(default_factory=tuple)
kwargs: Dict[str, Any] = field(default_factory=dict)
priority: Priority = Priority.NORMAL
max_retries: int = 3
retry_count: int = 0
backoff_factor: float = 2.0
status: TaskStatus = TaskStatus.PENDING
created_at: float = field(default_factory=time.time)
scheduled_at: float = field(default_factory=time.time)
completed_at: Optional[float] = None
error: Optional[str] = None
def __lt__(self, other: 'Task') -> bool:
"""Enable task comparison for priority queue ordering."""
# Primary sort by priority (lower value = higher priority)
if self.priority.value != other.priority.value:
return self.priority.value < other.priority.value
# Secondary sort by scheduled time (earlier = higher priority)
return self.scheduled_at < other.scheduled_at
def calculate_backoff(self) -> float:
"""Calculate backoff time for retry."""
return self.backoff_factor ** self.retry_count
class TaskQueue:
"""
Priority-based async task queue with retry and dead-letter functionality.
Attributes:
_queue: Priority queue for pending tasks
_processing: Set of currently processing tasks
_dead_letter_queue: Queue for failed tasks
_lock: Async lock for thread safety
_task_index: Index for quick task lookup
_stats: Statistics about task processing
"""
def __init__(self) -> None:
self._queue: List[Task] = []
self._processing: Dict[str, Task] = {}
self._dead_letter_queue: List[Task] = []
self._lock = asyncio.Lock()
self._task_index: Dict[str, Task] = {}
self._stats = defaultdict(int)
async def enqueue(self, task: Task) -> str:
"""
Add a task to the queue.
Args:
task: Task to add to the queue
Returns:
Task ID
Raises:
ValueError: If task is None or has no function
"""
if not task or not task.func:
raise ValueError("Task must have a function to execute")
async with self._lock:
heapq.heappush(self._queue, task)
self._task_index[task.id] = task
self._stats['enqueued'] += 1
logger.info(f"Enqueued task {task.id} with priority {task.priority.name}")
return task.id
async def dequeue(self) -> Optional[Task]:
"""
Remove and return the highest priority task from the queue.
Returns:
Task or None if queue is empty
"""
async with self._lock:
while self._queue:
# Get the highest priority task
task = heapq.heappop(self._queue)
# Check if it's ready to be processed
if task.scheduled_at <= time.time():
task.status = TaskStatus.PROCESSING
self._processing[task.id] = task
self._stats['processing'] += 1
logger.info(f"Dequeued task {task.id}")
return task
else:
# Put it back and try the next one
heapq.heappush(self._queue, task)
break
return None
async def complete_task(self, task_id: str) -> bool:
"""
Mark a task as completed.
Args:
task_id: ID of the task to complete
Returns:
True if task was found and completed, False otherwise
"""
async with self._lock:
if task_id in self._processing:
task = self._processing.pop(task_id)
task.status = TaskStatus.COMPLETED
task.completed_at = time.time()
self._stats['completed'] += 1
logger.info(f"Completed task {task_id}")
return True
return False
async def fail_task(self, task_id: str, error: str) -> bool:
"""
Handle task failure with retry logic.
Args:
task_id: ID of the failed task
error: Error message
Returns:
True if task was retried or moved to dead letter queue, False if not found
"""
async with self._lock:
if task_id in self._processing:
task = self._processing.pop(task_id)
task.error = error
# Check if we should retry
if task.retry_count < task.max_retries:
task.retry_count += 1
task.status = TaskStatus.RETRYING
backoff = task.calculate_backoff()
task.scheduled_at = time.time() + backoff
heapq.heappush(self._queue, task)
self._stats['retried'] += 1
logger.info(f"Retrying task {task_id} in {backoff:.2f} seconds (attempt {task.retry_count})")
else:
# Move to dead letter queue
task.status = TaskStatus.FAILED
task.completed_at = time.time()
self._dead_letter_queue.append(task)
self._stats['failed'] += 1
logger.warning(f"Task {task_id} failed permanently and moved to dead letter queue")
return True
return False
async def get_stats(self) -> Dict[str, int]:
"""Get queue statistics."""
async with self._lock:
stats = self._stats.copy()
stats['pending'] = len(self._queue)
stats['processing'] = len(self._processing)
stats['dead_letter'] = len(self._dead_letter_queue)
return stats
async def get_dead_letter_queue(self) -> List[Task]:
"""Get all tasks in the dead letter queue."""
async with self._lock:
return self._dead_letter_queue.copy()
class Worker(ABC):
"""
Abstract base class for task workers.
Attributes:
queue: Task queue to process tasks from
name: Worker name for identification
_running: Flag indicating if worker is running
_task: Current running task
"""
def __init__(self, queue: TaskQueue, name: str = "Worker") -> None:
self.queue = queue
self.name = name
self._running = False
self._task: Optional[asyncio.Task] = None
async def start(self) -> None:
"""Start the worker."""
if self._running:
return
self._running = True
self._task = asyncio.create_task(self._run())
logger.info(f"{self.name} started")
async def stop(self) -> None:
"""Stop the worker."""
if not self._running:
return
self._running = False
if self._task:
self._task.cancel()
try:
await self._task
except asyncio.CancelledError:
pass
logger.info(f"{self.name} stopped")
async def _run(self) -> None:
"""Main worker loop."""
while self._running:
try:
task = await self.queue.dequeue()
if task:
await self._process_task(task)
else:
# No tasks available, sleep briefly
await asyncio.sleep(0.1)
except Exception as e:
logger.error(f"{self.name} encountered an error: {e}")
await asyncio.sleep(1) # Brief pause before continuing
async def _process_task(self, task: Task) -> None:
"""Process a single task."""
try:
logger.info(f"{self.name} processing task {task.id}")
result = await task.func(*task.args, **task.kwargs)
await self.queue.complete_task(task.id)
logger.info(f"{self.name} completed task {task.id} with result: {result}")
except Exception as e:
error_msg = str(e)
logger.error(f"{self.name} failed task {task.id}: {error_msg}")
await self.queue.fail_task(task.id, error_msg)
# Demo functions
async def demo_task_success(name: str, duration: float = 0.1) -> str:
"""Demo task that succeeds."""
await asyncio.sleep(duration)
return f"Success: {name}"
async def demo_task_failure(name: str) -> str:
"""Demo task that fails."""
await asyncio.sleep(0.1)
raise RuntimeError(f"Intentional failure for {name}")
async def demo_task_random(name: str) -> str:
"""Demo task that randomly succeeds or fails."""
await asyncio.sleep(0.1)
import random
if random.random() < 0.7: # 70% chance of success
return f"Random success: {name}"
else:
raise RuntimeError(f"Random failure for {name}")
async def main() -> None:
"""Demo the task queue functionality."""
print("=== Async Task Queue Demo ===\n")
# Create queue and worker
queue = TaskQueue()
worker = Worker(queue, "DemoWorker")
# Start worker
await worker.start()
# Create and enqueue various tasks
tasks = []
# High priority successful tasks
for i in range(3):
task = Task(
func=demo_task_success,
args=[f"High Priority Task {i}"],
priority=Priority.HIGH,
max_retries=2
)
tasks.append(task)
await queue.enqueue(task)
# Normal priority tasks with some failures
for i in range(5):
task = Task(
func=demo_task_random if i % 2 == 0 else demo_task_failure,
args=[f"Normal Task {i}"],
priority=Priority.NORMAL,
max_retries=3
)
tasks.append(task)
await queue.enqueue(task)
# Low priority tasks
for i in range(2):
task = Task(
func=demo_task_success,
args=[f"Low Priority Task {i}"],
priority=Priority.LOW,
max_retries=1
)
tasks.append(task)
await queue.enqueue(task)
print(f"Enqueued {len(tasks)} tasks\n")
# Wait for tasks to process
await asyncio.sleep(5)
# Print statistics
stats = await queue.get_stats()
print("=== Processing Statistics ===")
for key, value in stats.items():
print(f"{key.capitalize()}: {value}")
# Print dead letter queue
dlq = await queue.get_dead_letter_queue()
if dlq:
print("\n=== Dead Letter Queue ===")
for task in dlq:
print(f"Task {task.id}: {task.error}")
# Stop worker
await worker.stop()
print("\n=== Demo Complete ===")
if __name__ == "__main__":
asyncio.run(main())