Allow infra to listen to activity actively

This commit is contained in:
Jon Staab
2026-04-01 16:01:10 -07:00
parent 07dfe86210
commit 3e131b6a1b
6 changed files with 131 additions and 68 deletions
+16 -10
View File
@@ -1,6 +1,6 @@
# `pub struct Infra`
Infra is a service which polls the database and synchronizes updates to relays to a remote zooid instance via `api_url`.
Infra is a service which listens for activity and synchronizes relay updates to a remote zooid instance via `api_url`.
Members:
@@ -14,18 +14,24 @@ Members:
## `pub async fn start(self)`
- Initializes `last_activity_at` from `query.max_activity_at()` so historical activities are not replayed on restart.
- Calls `self.tick` in a loop every 10 seconds.
- Subscribes to `command.notify` before doing anything else so no activities are missed.
- Calls `catch_up` to sync any relays that need it from before this process started.
- Loops on `rx.recv()`, calling `handle_activity` for each received `Activity`.
- On `Lagged`, logs a warning and runs `catch_up` to recover.
## `pub async fn tick(self)`
## `async fn catch_up(&self)`
Iterates over `query.list_activity` since last run and does the following:
- Lists all relays via `query.list_relays()` and syncs any that have `status = "new"` or a non-empty `sync_error`.
- For `create_relay`, `update_relay`, or `deactivate_relay` activity, sync the relay to zooid.
- Uses `relay.synced` to decide POST vs PUT (not the activity type), so already-synced relays always use PUT even on restart.
- On success, calls `command.mark_relay_synced` to set `synced = 1`, `status = 'active'`, and clear `sync_error`.
- On failure, calls `command.fail_relay_sync`.
- All other activity types are ignored (e.g. `fail_relay_sync` must not trigger another sync).
## `async fn handle_activity(&self, activity: &Activity)`
- For `create_relay`, `update_relay`, or `deactivate_relay` activity, calls `sync_and_report`.
- All other activity types are ignored (e.g. `fail_relay_sync`, `complete_relay_sync`).
## `async fn sync_and_report(&self, relay: &Relay, is_new: bool)`
- Calls `sync_relay` and on success calls `command.complete_relay_sync`.
- On failure calls `command.fail_relay_sync`.
## `async fn sync_relay(&self, relay: &Relay, is_new: bool)`