Files
romm/backend/tasks/registry.py
zurdi ff6b160117 feat: persistent notifications
Toasts disappear after a few seconds, and nothing reaches a user who has no
tab open. Notifications are now stored per user and kept until dismissed, so
what a background job or another user did is waiting the next time RomM is
opened.

Backend:
- `notifications` table (0130) with kind, level, data, actor and read_at, capped
  at the newest 200 per user.
- /api/notifications: list, mark read, dismiss one, dismiss all.
- notify()/notify_admins() store the row and push `notifications:new` to the
  user's socket room; read/dismiss push to the user's other tabs.
- Producers: a scan's end (to whoever started it; an unattended scan tells
  the admins when it failed or found new games), a manual task's end (to who
  ran it; a scheduled task's failure to the admins), an admin ending someone's
  stream, and an admin changing someone's role.

Frontend (v2):
- Unread count badge on the navbar avatar, and a Notifications entry in the
  user menu and settings sidebar.
- Notifications view: marks what it shows read, links each row to what it is
  about, dismiss one or all.
- Live toast for a new notification, except scans, which already toast.
- The text is rendered from the kind in the reader's language (18 locales).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 10:35:33 +02:00

119 lines
4.5 KiB
Python

"""The catalog of tasks an admin can see, run, or have run on a schedule."""
from typing import Any, Final
from rq.job import Job
from rq.queue import Queue
from config import TASK_RESULT_TTL
from exceptions.task_exceptions import TaskNotFoundException
from handler.redis_handler import low_prio_queue, scan_queue
from tasks.manual.cleanup_missing_firmware import cleanup_missing_firmware_task
from tasks.manual.cleanup_missing_roms import cleanup_missing_roms_task
from tasks.manual.recompute_save_content_hashes import (
recompute_save_content_hashes_task,
)
from tasks.manual.sync_folder_scan import sync_folder_scan_task
from tasks.scheduled.build_recommendations import build_recommendations_task
from tasks.scheduled.cleanup_netplay import cleanup_netplay_task
from tasks.scheduled.cleanup_orphaned_resources import cleanup_orphaned_resources_task
from tasks.scheduled.cleanup_upload_tmp import cleanup_upload_tmp_task
from tasks.scheduled.cleanup_zip_cache import cleanup_zip_cache_task
from tasks.scheduled.convert_images_to_webp import convert_images_to_webp_task
from tasks.scheduled.scan_library import scan_library_task
from tasks.scheduled.sync_retroachievements_progress import (
sync_retroachievements_progress_task,
)
from tasks.scheduled.update_launchbox_metadata import update_launchbox_metadata_task
from tasks.scheduled.update_switch_titledb import update_switch_titledb_task
from tasks.sync_push_pull_task import sync_push_pull_task
from tasks.tasks import PeriodicTask, Task, run_task_by_name
# The keys are the names the API and the cron schedule address a task by, and
# they end up in the job payload, so they outlive any given release. Every task
# that runs on a schedule belongs here; which of them the API surfaces is the
# endpoint's business.
SCHEDULED_TASKS: Final[dict[str, PeriodicTask]] = {
"scan_library": scan_library_task,
"update_launchbox_metadata": update_launchbox_metadata_task,
"update_switch_titledb": update_switch_titledb_task,
"build_recommendations": build_recommendations_task,
"convert_images_to_webp": convert_images_to_webp_task,
"cleanup_zip_cache": cleanup_zip_cache_task,
"cleanup_orphaned_resources": cleanup_orphaned_resources_task,
"cleanup_netplay": cleanup_netplay_task,
"cleanup_upload_tmp": cleanup_upload_tmp_task,
"sync_retroachievements_progress": sync_retroachievements_progress_task,
"sync_push_pull": sync_push_pull_task,
}
MANUAL_TASKS: Final[dict[str, Task]] = {
"cleanup_missing_roms": cleanup_missing_roms_task,
"cleanup_missing_firmware": cleanup_missing_firmware_task,
"sync_folder_scan": sync_folder_scan_task,
"recompute_save_content_hashes": recompute_save_content_hashes_task,
}
def get_task(name: str) -> Task | None:
"""Look up a task by the name it is addressed by."""
return SCHEDULED_TASKS.get(name) or MANUAL_TASKS.get(name)
def enqueue_task(
name: str,
*,
queue: Queue = low_prio_queue,
task_kwargs: dict[str, Any] | None = None,
run_by_user_id: int | None = None,
**job_options: Any,
) -> Job:
"""Enqueue a registered task by name.
Args:
name: The key the task is registered under.
queue: Which queue to enqueue on.
task_kwargs: Forwarded to the task's ``run``, nested so that they cannot
collide with the name of the task to run.
run_by_user_id: Who ran it by hand, notified when it ends.
job_options: Passed through to RQ, for a fixed job id and the like.
Returns:
The enqueued job.
"""
task = get_task(name)
if task is None:
raise TaskNotFoundException(name)
return queue.enqueue(
run_task_by_name,
kwargs={
"name": name,
"task_kwargs": task_kwargs or {},
"run_by_user_id": run_by_user_id,
},
job_timeout=task.timeout,
result_ttl=TASK_RESULT_TTL,
meta=task.job_meta(name),
**job_options,
)
def enqueue_scheduled_scan(name: str) -> str:
"""Put a scheduled scan on the scan queue with the abandoned-job callback.
Cron can attach no `on_failure`, so a scan it enqueues itself is the one
scan whose worker can die without anything telling the clients.
Args:
name: The key the scan task is registered under.
Returns:
The id of the enqueued scan job.
"""
# Imported here because the scan module imports the task modules this one
# pulls in.
from endpoints.sockets.scan import report_scan_failure
return enqueue_task(name, queue=scan_queue, on_failure=report_scan_failure).id