Polling Engine Architecture
camply separates target polling from user notifications so multiple campers watching the same campground share a single upstream check cycle.
Package boundaries
The Python codebase ships as one camply distribution with strict internal layering (cli | server | worker -> core -> notifications | providers -> db -> __about__):
- Base package (
core,providers,db,cli): Provides upstream discovery, availability matching, SQLite persistence, and the foreground CLI with no server or broker dependencies. - Server (
camply.server): FastAPI app, auth, and standalone lifespan runtime (serverextra). - Worker (
camply.worker): Shared async polling engine (camply.worker.engine) plus Celery/Valkey adapters (distributedextra).
Why de-duplication matters
If ten users watch Yosemite's Lower Pines for the same dates, naive polling makes ten identical upstream requests per cycle and risks IP bans. Instead, camply normalizes each (provider_id, campground_id, start_date, end_date) tuple into one shared UniqueTarget row and links each UserScan subscription to it.
graph TD
A[User saves scan] --> B[FastAPI Backend]
B --> C{UniqueTarget exists?}
C -->|No| D[Create UniqueTarget]
C -->|Yes| E[Link UserScan]
F[Scheduler heartbeat] --> G[Find due targets]
G --> H[Acquire target lock & check provider]
H --> I[Replace scan_results snapshot]
I --> J[Match active UserScans]
J --> K[Queue Pushover or ntfy alerts]
Target lifecycle and concurrency
A UniqueTarget transitions through four states:
- Initialized: Created when the first scan references a campground and date range.
- Active: Eligible for checks while at least one linked
UserScanis active and enabled. - Cooldown: After a check updates
last_checked_at, the target waits untilCAMPLY_TARGET_COOLDOWN(default55seconds) elapses. - Dormant: Marked inactive when no active scans remain; kept for inspection but skipped during discovery.
In distributed mode, workers acquire a Valkey lock (lock:target_id) before calling a provider so concurrent workers never poll the same target simultaneously. In standalone mode, camply.worker.runtime runs bounded in-process async loops inside the FastAPI lifespan—avoiding duplicate checks on a single host.
Fan-out matching and area rounds
- Blind polling: The poller asks only what campsites are open for the target's dates, converts responses to
CampsiteDTOs, and replacesscan_resultsatomically. - Per-subscriber filtering: Each
UserScanfilters the shared snapshot by its ownmin_stay_length,allowed_weekdays,preferred_types, andrequire_electricpreferences without extra provider calls. - Recreation-area rounds: Area scans join one
UserScanto oneUniqueTargetper eligible member campground viascan_targets.prepare_rounds()reconciles membership against the latestproviders.catalog_revision, recovers stale attempts after105seconds, and bounds each round withround_deadline_at(2 * CAMPLY_HEARTBEAT_INTERVAL + 780seconds) so one slow campground cannot stall other results. - Catalog atomicity: Providers like Recreation.gov parse complete archives in memory before updating
recreation_areas,campgrounds, andsearchin one transaction.
Foreground CLI execution
camply.core.search reuses the same consecutive-night matcher (camply.db.availability.matching_available_dates) as saved web scans, but runs sequentially in the foreground using one shared provider client with no database persistence or notification queues.