Testing

Prompt

Act as a Senior Principal Software Engineer. Design and implement a robust, localized, thread-safe asynchronous Task Orchestrator and Retry Management Engine in Python (3.11+) that manages unstable distributed background tasks. Your solution must satisfy the following technical requirements: 1. Orchestrator Architecture: Define a `Task` abstraction using data classes that tracks task ID, retry budgets, backoff multipliers, execution state, and localized metadata parameters. 2. Asynchronous Queue: Implement a thread-safe `AsyncTaskQueue` using `asyncio` that handles task execution concurrently with a strict worker-pool limit (max 4 concurrent workers). 3. Resilient Retries & Backoff: Implement a worker polling mechanism that handles failures using exponential backoff with random jitter. Prevent "thundering herd" problems explicitly. 4. Telemetry Logging: Embed highly verbose logger strings tracking state transitions (Pending -> Running -> [Failed/Retrying] -> Succeeded/MaxRetriesExceeded). 5. Hardware Optimization Mocking: Write an optimized memory pooling trick within the task handling routing that minimizes excessive garbage collection when spinning up millions of lightweight mock task allocations. Provide the complete, production-ready, fully commented script. Do not truncate the code or leave "TODO" placeholders. Include a clear explanation of how your system prevents memory leaks and synchronization race conditions during concurrent worker polling.

Drag to resize
Drag to resize