move_detect/edge/queue.py
유창욱 9386dfc580 feat(delivery): install-path fixes + cafe24 package + delivery-audit hardening
Three intertwined bodies of work developed and tested together this session.
Full test suite green: python (edge) + phpunit (cloud).

## Edge/Pi install path
- setup_online.sh: apt-lock retry (apt-daily contention), Type=exec unit +
  StartLimitIntervalSec in [Unit], journalctl-on-failure, crash-loop detection.
- onnxruntime is now best-effort (no sdist / no 32-bit-ARM wheel); [6/8] probes the
  real backend and hard-fails if none works.
- Hailo HEF auto-fetch: edge/tools/fetch_hailo_hef.py (pinned sha256, refuses to guess
  8 vs 8L, HAILO_ARCH override for serial-less boards); hailo_pose.py run() timeout fix.
- install_offline.sh: mirrors the backend gate; onnxruntime optional.

## Cafe24 cloud package (cloud/deploy/)
- build_cafe24_package.sh emits move_detect_app/ (private, one folder) + www/move_detect/
  (docroot subdir); no-dev classmap autoloader; preflight.php; hardened .htaccess.
- Subdirectory mount support: Request::baseFrom/pathFor strip the mount prefix; index.php
  walks up to find the app root; views/JS prefix every URL with APP_BASE; session cookie
  scoped to the mount.

## Delivery audit — silent-failure & correctness (docs/delivery-audit.md)
All 4 blockers + all Phase-1 silent-failure + Phase-2 HIGH correctness:
- Edge cloud clients send a custom User-Agent (cafe24 WAF blocks python-requests) and
  allow_redirects=False.
- Daemon fails loud on blank CAFE24_URL/DEVICE_SECRET; run_loop logs swallowed exceptions;
  heartbeat carries real fps + per-camera health; UvcSource reopens a dead camera;
  offline queue is size-capped and drops poison rows; TARGET_FPS/QUEUE_DB validated.
- Zone coordinates normalized 0..1 on both sides (were canvas-px vs camera-px — every zone
  misplaced); pinch zones emit PINCH/HIGH (were downgraded to WARNING).
- Fall detection uses bbox cues when keypoints are low-confidence (occluded fallen person);
  ghost incident not cancelled by a bystander; displacement normalized per-second (FPS-invariant).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019Xmn8KZWqiQUguEMRtn6rW
2026-07-17 09:07:33 +09:00

84 lines
4 KiB
Python

import base64
import json
import sqlite3
from edge.events import Event, EventType, Severity
class OfflineQueue:
def __init__(self, db_path: str, client, max_rows: int = 2000,
max_send_attempts: int = 20):
self.client = client
self.max_rows = max_rows # SD-card guard: evict oldest beyond this
self.max_send_attempts = max_send_attempts # drop a poison event after this many tries
self.db = sqlite3.connect(db_path)
self.db.execute(
"CREATE TABLE IF NOT EXISTS queue ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, payload TEXT, snaps TEXT, "
"attempts INTEGER DEFAULT 0)"
)
# Migrate an older DB that predates the attempts column.
try:
self.db.execute("ALTER TABLE queue ADD COLUMN attempts INTEGER DEFAULT 0")
except sqlite3.OperationalError:
pass # column already exists
self.db.commit()
def _rehydrate(self, payload: dict, snaps: list[bytes]) -> tuple[Event, list[bytes]]:
e = Event(EventType(payload["event_type"]), Severity(payload["severity"]),
payload["camera_id"], payload["track_id"], payload["zone_id"],
payload["occurred_at"], payload["duration_s"], payload["metrics"],
tuple(payload["spatial"]) if payload.get("spatial") else None)
return e, snaps
def enqueue(self, event: Event, snapshots: list[bytes]) -> None:
payload = event.to_payload("") # device_id는 client가 다시 채움
payload["event_type"] = event.event_type.value
payload["severity"] = event.severity.value
payload["spatial"] = list(event.spatial) if event.spatial else None
snaps = [base64.b64encode(s).decode() for s in snapshots]
self.db.execute("INSERT INTO queue (payload, snaps) VALUES (?, ?)",
(json.dumps(payload), json.dumps(snaps)))
# Bound the queue so a multi-day outage — or a permanent misconfig that queues every
# event forever — cannot fill the SD card and hard-crash the Pi. Keep the newest
# max_rows; a fresh fall alert matters more than a stale one.
self.db.execute(
"DELETE FROM queue WHERE id NOT IN "
"(SELECT id FROM queue ORDER BY id DESC LIMIT ?)", (self.max_rows,))
self.db.commit()
def try_send(self, event: Event, snapshots: list[bytes]) -> bool:
if self.client.send(event, snapshots):
return True
self.enqueue(event, snapshots)
return False
def flush(self) -> int:
rows = self.db.execute(
"SELECT id, payload, snaps, attempts FROM queue ORDER BY id").fetchall()
sent = 0
for rid, payload_json, snaps_json, attempts in rows:
payload = json.loads(payload_json)
snaps = [base64.b64decode(s) for s in json.loads(snaps_json)]
event, snapshots = self._rehydrate(payload, snaps)
if self.client.send(event, snapshots):
self.db.execute("DELETE FROM queue WHERE id = ?", (rid,))
self.db.commit()
sent += 1
continue
# Failed. A bool send() can't tell a transient outage from a permanently-rejected
# (poison) event, so count attempts: below the cap, stop and preserve order (an
# outage recovers and the whole queue drains next flush); at the cap, DROP this
# row and continue so a poison head can never wedge the queue forever.
attempts += 1
if attempts >= self.max_send_attempts:
self.db.execute("DELETE FROM queue WHERE id = ?", (rid,))
self.db.commit()
continue
self.db.execute("UPDATE queue SET attempts = ? WHERE id = ?", (attempts, rid))
self.db.commit()
break # transient — preserve order, retry the whole queue next time
return sent
def pending(self) -> int:
return self.db.execute("SELECT COUNT(*) FROM queue").fetchone()[0]