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
+1
View File
@@ -12,3 +12,4 @@ Members:
## `pub fn new(query: Query, command: Command, robot: Robot) -> Self`
- Reads environment and populates members
- Subscribes to `command.notify.notified`
+6 -2
View File
@@ -5,15 +5,19 @@ Command writes to the database.
Members:
- `pool: SqlitePool` - a sqlite connection pool
- `pub notify: broadcast::Sender<Activity>` - callers can subscribe via `command.notify.subscribe()`
Notes:
- All public write methods should be atomic
- All writes should be accompanied by an activity log entry of `(tenant, activity_type, resource_type, resource_id)`
- `insert_activity` builds and returns the `Activity` struct (using `chrono::Utc::now()` for `created_at`)
- After each successful commit, sends the `Activity` on the broadcast channel
## `pub fn new(&self, pool: SqlitePool) -> Self`
- Assigns pool to self
- Creates the broadcast channel
## `pub fn create_tenant(&self, tenant: &Tenant) -> Result<()>`
@@ -51,7 +55,7 @@ Notes:
- Sets relay status to `inactive`, sets `sync_error`
- Logs activity as `(fail_relay_sync, relay_id)`
## `pub fn mark_relay_synced(&self, relay_id: &str) -> Result<()>`
## `pub fn complete_relay_sync(&self, relay_id: &str) -> Result<()>`
- Sets `synced = 1`, `status = 'active'`, clears `sync_error`
- No activity log (called by infra after successful sync)
- Logs activity as `(complete_relay_sync, relay_id)`
+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)`