Files
romm/backend/tasks/manual/sync_folder_scan.py

66 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.
"""
import asyncio
from typing import Any
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.registry import SYNC_FOLDER_SCAN_SPEC
from tasks.tasks import Task
class SyncFolderScanTask(Task):
"""Scan device sync folders for unprocessed incoming files."""
def __init__(self) -> None:
super().__init__(SYNC_FOLDER_SCAN_SPEC)
async def run(self, *args: Any, **kwargs: Any) -> dict[str, Any]:
if not self.spec.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
]
# It drives its own event loops with asyncio.run, which cannot
# nest in the loop running this task, so it gets a thread.
await asyncio.to_thread(
_process_device_incoming, device.id, file_tuples
)
total_files += len(incoming_files)
return {"status": "completed", "files_processed": total_files}
sync_folder_scan_task = SyncFolderScanTask()