From 8d5eda32448fa33f47e1d8583ff5a4ece8043b56 Mon Sep 17 00:00:00 2001 From: benya Date: Sat, 7 Feb 2026 22:57:36 +0300 Subject: [PATCH] Add queue status command --- handlers/artifacts.py | 2 +- handlers/backup.py | 10 ++++++++-- keyboards.py | 3 ++- services/queue.py | 21 ++++++++++++++++++--- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/handlers/artifacts.py b/handlers/artifacts.py index fd6c2f6..6d5cb7c 100644 --- a/handlers/artifacts.py +++ b/handlers/artifacts.py @@ -54,7 +54,7 @@ async def cmd_artifacts_upload(msg: Message): finally: release_lock("artifacts") - pos = await enqueue(job) + pos = await enqueue("artifact-upload", job) await msg.answer(f"🕓 Upload queued (#{pos})", reply_markup=artifacts_kb) diff --git a/handlers/backup.py b/handlers/backup.py index 6461e78..dba3703 100644 --- a/handlers/backup.py +++ b/handlers/backup.py @@ -7,7 +7,7 @@ from app import dp from auth import is_admin_msg from keyboards import backup_kb from lock_utils import acquire_lock, release_lock -from services.queue import enqueue +from services.queue import enqueue, format_status from services.backup import backup_badge, restore_help from services.runner import run_cmd @@ -122,7 +122,7 @@ async def cmd_backup_now(msg: Message): finally: release_lock("backup") - pos = await enqueue(job) + pos = await enqueue("backup", job) await msg.answer(f"🕓 Backup queued (#{pos})", reply_markup=backup_kb) @@ -190,6 +190,12 @@ async def ls(msg: Message): await cmd_last_snapshot(msg) +@dp.message(F.text == "🧾 Queue") +async def qb(msg: Message): + if is_admin_msg(msg): + await msg.answer(format_status(), reply_markup=backup_kb) + + @dp.message(F.text == "▶️ Run backup") async def br(msg: Message): if is_admin_msg(msg): diff --git a/keyboards.py b/keyboards.py index cb98c14..b45e86f 100644 --- a/keyboards.py +++ b/keyboards.py @@ -29,7 +29,8 @@ backup_kb = ReplyKeyboardMarkup( keyboard=[ [KeyboardButton(text="📦 Status"), KeyboardButton(text="📦 Last snapshot")], [KeyboardButton(text="📊 Repo stats"), KeyboardButton(text="🧯 Restore help")], - [KeyboardButton(text="▶️ Run backup"), KeyboardButton(text="⬅️ Назад")], + [KeyboardButton(text="▶️ Run backup"), KeyboardButton(text="🧾 Queue")], + [KeyboardButton(text="⬅️ Назад")], ], resize_keyboard=True, ) diff --git a/services/queue.py b/services/queue.py index 7eb83ea..ac51319 100644 --- a/services/queue.py +++ b/services/queue.py @@ -3,17 +3,32 @@ from typing import Awaitable, Callable _queue: asyncio.Queue = asyncio.Queue() +_current_label: str | None = None -async def enqueue(job: Callable[[], Awaitable[None]]) -> int: - await _queue.put(job) +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: - job = await _queue.get() + 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)