35 lines
880 B
Python
35 lines
880 B
Python
import asyncio
|
|
from typing import Awaitable, Callable
|
|
|
|
|
|
_queue: asyncio.Queue = asyncio.Queue()
|
|
_current_label: str | None = None
|
|
|
|
|
|
async def enqueue(label: str, job: Callable[[], Awaitable[None]]) -> int:
|
|
await _queue.put((label, job))
|
|
return _queue.qsize()
|
|
|
|
|
|
async def worker():
|
|
global _current_label
|
|
while True:
|
|
label, job = await _queue.get()
|
|
_current_label = label
|
|
try:
|
|
await job()
|
|
finally:
|
|
_current_label = None
|
|
_queue.task_done()
|
|
|
|
|
|
def format_status() -> str:
|
|
pending = [label for label, _ in list(_queue._queue)]
|
|
lines = ["🧾 Queue"]
|
|
lines.append(f"🔄 Running: {_current_label or 'idle'}")
|
|
lines.append(f"⏳ Pending: {len(pending)}")
|
|
if pending:
|
|
preview = ", ".join(pending[:5])
|
|
lines.append(f"➡️ Next: {preview}")
|
|
return "\n".join(lines)
|