Files
romm/backend/tasks/manual/sync_folder_scan.py
Claude 26bdc11e13 refactor(filesystem): lazy-init launchbox + sync handlers, drop tolerate_missing_base
Apply the same lazy-factory pattern to FSLaunchboxHandler and FSSyncHandler
that ssh_sync_handler now uses. With both opt-in features deferred to
first-use, the tolerate_missing_base escape hatch on FSHandler is no longer
needed — every handler now fails loudly on mkdir failure, which is the
right behavior for the always-on core paths (assets, library, resources).

Touched call sites:
  - resources_handler._resolve_local_file_uri (launchbox)
  - sync_watcher.py, endpoints/device.py, tasks/manual/sync_folder_scan.py
    (fs_sync)

Net effect:
  - Default installs never poke /romm/launchbox or /romm/sync at startup.
  - Misconfigured opt-in users get a clear, actionable PermissionError at
    the call site instead of a silent warning followed by mystery failures.
  - tolerate_missing_base, its tests, and one stale log import are gone.
2026-05-24 14:59:03 +00:00

67 lines
2.3 KiB
Python

"""Manual scan task for sync folders with unprocessed files.
This serves as a fallback for the file watcher, handling cases where
filesystem events are missed (e.g., server restart, NFS mounts).
Triggered on demand, not scheduled automatically.
"""
from typing import Any
from config import ENABLE_SYNC_FOLDER_WATCHER
from handler.database import db_device_handler
from handler.filesystem import get_fs_sync_handler
from logger.logger import log
from models.device import SyncMode
from tasks.tasks import Task, TaskType
class SyncFolderScanTask(Task):
"""Scan device sync folders for unprocessed incoming files."""
def __init__(self) -> None:
super().__init__(
title="Sync Folder Scan",
description="Scan device sync folders for new save files",
task_type=TaskType.SYNC,
enabled=ENABLE_SYNC_FOLDER_WATCHER,
manual_run=True,
)
async def run(self, *args: Any, **kwargs: Any) -> dict:
if not self.enabled:
log.info("Sync folder scan not enabled, skipping")
return {"status": "disabled"}
# Get all file_transfer devices
devices = db_device_handler.get_all_devices_by_sync_mode(SyncMode.FILE_TRANSFER)
if not devices:
log.info("No file_transfer devices found")
return {"status": "no_devices"}
fs_sync_handler = get_fs_sync_handler()
total_files = 0
for device in devices:
if not device.sync_enabled:
continue
incoming_files = fs_sync_handler.list_incoming_files(device.id)
if incoming_files:
log.info(
f"Sync folder scan: found {len(incoming_files)} files "
f"for device {device.id}"
)
# Import here to avoid circular imports
from sync_watcher import _process_device_incoming
file_tuples = [
(f["platform_slug"], f["file_name"], f["full_path"])
for f in incoming_files
]
_process_device_incoming(device.id, file_tuples)
total_files += len(incoming_files)
return {"status": "completed", "files_processed": total_files}
sync_folder_scan_task = SyncFolderScanTask()