Routes gain `dispatch_mode TEXT NOT NULL DEFAULT 'sync'` (migration
0012). Existing routes default to sync so the migration is
non-breaking. `DispatchMode` enum lands in `picloud-shared`.
The user-routes orchestrator handler now branches:
- `dispatch_mode = async` → write outbox row with `reply_to = None`,
return `202 Accepted` + `{accepted_at, execution_id}`. Dispatcher
fires the script in the background; retries / dead-letters via
the framework from commit 5.
- `dispatch_mode = sync` → register an inbox channel
(`tokio::sync::oneshot`), write outbox row with `reply_to =
inbox_id`, `.await` on the receiver with a timeout =
script.timeout_seconds + 2s buffer. Dispatcher hands the result
back; orchestrator maps `InboxResult` into the HTTP response per
the design-notes §3 status-code table (422/502/503/504/507/500).
`InboxRegistry` (orchestrator-core/src/inbox.rs) is the in-process
implementation of `InboxResolver`. Lock-free HashMap of pending
oneshot senders keyed by `inbox_id`. Tests cover register/deliver
round-trip, unknown-id is abandoned, dropped-receiver is abandoned,
explicit cancel. Cluster mode (v1.3+) swaps this for
LISTEN/NOTIFY-keyed lookup behind the same trait.
`OutboxWriter` trait lives in `picloud-shared` so orchestrator-core
can write to the outbox without depending on manager-core (which
would invert the dependency arrow). `PostgresOutboxRepo` implements
both `OutboxRepo` (dispatcher surface) and `OutboxWriter`
(orchestrator surface); the picloud binary clones the same concrete
Arc into both trait views.
The dispatcher's HTTP arm (commit 5 had a stub) now decodes the
`HttpDispatchPayload` off the outbox row, looks up the script,
synthesizes an `ExecRequest`, and runs it through the executor.
Outcome routing reuses the same path as KV triggers — sync HTTP
flows through the inbox, async dispatch gets dropped after
success (or DL'd on exhaustion).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
94 lines
3.0 KiB
Rust
94 lines
3.0 KiB
Rust
//! Hello-World seed for fresh installs.
|
|
//!
|
|
//! Idempotent. Runs after migrations and after admin bootstrap. Only
|
|
//! seeds when the default app is empty (no scripts, no routes); on
|
|
//! upgrades it does nothing so existing content isn't polluted.
|
|
|
|
use std::sync::Arc;
|
|
|
|
use picloud_shared::{App, AppId, HostKind, PathKind};
|
|
|
|
use crate::app_repo::AppRepository;
|
|
use crate::repo::{NewScript, ScriptRepository, ScriptRepositoryError};
|
|
use crate::route_repo::{NewRoute, RouteRepository};
|
|
|
|
const HELLO_RHAI_SOURCE: &str = include_str!("../seeds/hello.rhai");
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub enum HelloWorldOutcome {
|
|
/// Default app already has scripts (or doesn't exist) — left alone.
|
|
SkippedExisting,
|
|
/// Inserted the hello.rhai script and the `/hello` route.
|
|
Seeded,
|
|
}
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum SeedError {
|
|
#[error("default app not found — did the migration run?")]
|
|
MissingDefaultApp,
|
|
#[error("repository error: {0}")]
|
|
Repo(#[from] ScriptRepositoryError),
|
|
}
|
|
|
|
pub async fn seed_hello_world_if_fresh(
|
|
apps: Arc<dyn AppRepository>,
|
|
scripts: Arc<dyn ScriptRepository>,
|
|
routes: Arc<dyn RouteRepository>,
|
|
) -> Result<HelloWorldOutcome, SeedError> {
|
|
let default = apps
|
|
.get_by_slug("default")
|
|
.await?
|
|
.ok_or(SeedError::MissingDefaultApp)?;
|
|
|
|
// Idempotence: only seed when both scripts AND routes are empty.
|
|
// (Either alone is suspicious enough to skip — the operator may have
|
|
// already started shaping the default app.)
|
|
let existing_scripts = scripts.list_for_app(default.id).await?;
|
|
let existing_routes = routes.list_for_app(default.id).await?;
|
|
if !existing_scripts.is_empty() || !existing_routes.is_empty() {
|
|
return Ok(HelloWorldOutcome::SkippedExisting);
|
|
}
|
|
|
|
seed_into(&*scripts, &*routes, &default).await?;
|
|
Ok(HelloWorldOutcome::Seeded)
|
|
}
|
|
|
|
async fn seed_into(
|
|
scripts: &dyn ScriptRepository,
|
|
routes: &dyn RouteRepository,
|
|
default: &App,
|
|
) -> Result<(), ScriptRepositoryError> {
|
|
let script = scripts
|
|
.create(NewScript {
|
|
app_id: default.id,
|
|
name: "hello".to_string(),
|
|
description: Some("Reference example: returns a greeting at GET /hello.".to_string()),
|
|
source: HELLO_RHAI_SOURCE.to_string(),
|
|
timeout_seconds: Some(5),
|
|
memory_limit_mb: None,
|
|
sandbox: None,
|
|
})
|
|
.await?;
|
|
|
|
routes
|
|
.create(NewRoute {
|
|
app_id: default.id,
|
|
script_id: script.id,
|
|
host_kind: HostKind::Any,
|
|
host: String::new(),
|
|
host_param_name: None,
|
|
path_kind: PathKind::Exact,
|
|
path: "/hello".to_string(),
|
|
// Accept any method so both `curl /hello` and
|
|
// `curl -d '{"name":"X"}' /hello` work out of the box.
|
|
method: None,
|
|
dispatch_mode: picloud_shared::DispatchMode::Sync,
|
|
})
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
fn _typecheck(_id: AppId) {} // suppress unused-import warnings if reshuffled
|