Initial commit - Trekker V2 source
This commit is contained in:
commit
d71faf19e3
65 files changed
+3615
No files matched your search
@@ -0,0 +1,78 @@
|
||||
"""Folder watch: ingest .gpx files dropped into the configured directories.
|
||||
|
||||
Internal path uses inotify-backed Observer; remote path (SMB/Nextcloud/USB
|
||||
mounts, where inotify is unreliable) uses PollingObserver per PLAN.md 1e.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
from watchdog.events import FileSystemEvent, FileSystemEventHandler
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.observers.polling import PollingObserver
|
||||
|
||||
from app.ingest.gpx_parser import parse_gpx
|
||||
from app.storage.route_store import Store
|
||||
|
||||
log = logging.getLogger("trekker.watchdog")
|
||||
|
||||
|
||||
class GpxHandler(FileSystemEventHandler):
|
||||
def __init__(self, store: Store):
|
||||
self.store = store
|
||||
|
||||
def on_created(self, event: FileSystemEvent) -> None:
|
||||
if event.is_directory or not str(event.src_path).lower().endswith(".gpx"):
|
||||
return
|
||||
self._ingest(Path(str(event.src_path)))
|
||||
|
||||
def on_moved(self, event: FileSystemEvent) -> None:
|
||||
dest = getattr(event, "dest_path", None)
|
||||
if dest and str(dest).lower().endswith(".gpx"):
|
||||
self._ingest(Path(str(dest)))
|
||||
|
||||
def _ingest(self, path: Path) -> None:
|
||||
# Wait for the writer to finish (size stable across two checks).
|
||||
try:
|
||||
last = -1
|
||||
for _ in range(20):
|
||||
size = path.stat().st_size
|
||||
if size == last and size > 0:
|
||||
break
|
||||
last = size
|
||||
time.sleep(0.5)
|
||||
route = parse_gpx(path.read_bytes(), filename=path.name)
|
||||
self.store.save_route(route)
|
||||
log.info("ingested %s as route %s (%s)", path.name, route.id, route.source)
|
||||
except Exception:
|
||||
log.exception("failed to ingest %s", path)
|
||||
|
||||
|
||||
class WatchdogDaemon:
|
||||
def __init__(self, store: Store, internal_path: str | None,
|
||||
remote_path: str | None, poll_interval: int = 30):
|
||||
self.observers = []
|
||||
handler = GpxHandler(store)
|
||||
if internal_path:
|
||||
Path(internal_path).mkdir(parents=True, exist_ok=True)
|
||||
obs = Observer()
|
||||
obs.schedule(handler, internal_path, recursive=True)
|
||||
self.observers.append(obs)
|
||||
if remote_path and Path(remote_path).is_dir():
|
||||
obs = PollingObserver(timeout=poll_interval)
|
||||
obs.schedule(handler, remote_path, recursive=True)
|
||||
self.observers.append(obs)
|
||||
|
||||
def start(self) -> None:
|
||||
for obs in self.observers:
|
||||
obs.start()
|
||||
if self.observers:
|
||||
log.info("watchdog started (%d observers)", len(self.observers))
|
||||
|
||||
def stop(self) -> None:
|
||||
for obs in self.observers:
|
||||
obs.stop()
|
||||
obs.join(timeout=5)
|
||||
Reference in new issue
Block a user