Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(flows): thread dispatch context through trigger handlers

Steven Enamakel7168f71c797a
diff --git a/crates/openhuman-core/src/flows/bus/trigger.rs b/crates/openhuman-core/src/flows/bus/trigger.rs
index da0b6d1de0..fcea085775 100644
--- a/crates/openhuman-core/src/flows/bus/trigger.rs
+++ b/crates/openhuman-core/src/flows/bus/trigger.rs
@@ -112,8 +112,11 @@ impl FlowTriggerSubscriber {
}
/// The configuration to handle an event under (`super::owner::config_for_scope`).
- pub(super) fn config_for_scope(&self) -> Arc<Config> {
- super::owner::config_for_scope(&crate::core::runtime::DispatchCtx::ambient(), &self.config)
+ pub(super) fn config_for_scope(
+ &self,
+ ctx: &crate::core::runtime::DispatchCtx,
+ ) -> Arc<Config> {
+ super::owner::config_for_scope(ctx, &self.config)
}
/// A `flow`-type cron job fired (`cron::scheduler`'s trigger sink calls
@@ -123,11 +126,14 @@ impl FlowTriggerSubscriber {
/// since the job was registered), and dispatches it with an empty trigger
/// payload.
pub(crate) async fn dispatch_schedule_tick(&self, flow_id: &str) {
- super::owner::as_owner(&self.config, flow_id, self.handle_schedule_tick(flow_id)).await
+ super::owner::as_owner(&self.config, flow_id, |ctx| {
+ self.handle_schedule_tick(ctx, flow_id)
+ })
+ .await
}
- async fn handle_schedule_tick(&self, flow_id: &str) {
- let config = self.config_for_scope();
+ async fn handle_schedule_tick(&self, ctx: crate::core::runtime::DispatchCtx, flow_id: &str) {
+ let config = self.config_for_scope(&ctx);
let flow = match store::get_flow(&config, flow_id) {
Ok(Some(flow)) => flow,
Ok(None) => {
@@ -149,6 +155,7 @@ impl FlowTriggerSubscriber {
}
let inputs = pinned_trigger_inputs(&flow);
self.spawn_run(
+ &ctx,
flow_id.to_string(),
Value::Null,
inputs,
@@ -160,8 +167,14 @@ impl FlowTriggerSubscriber {
/// an `app_event` trigger bound to this `toolkit`/`trigger_slug` and
/// dispatches each match with the event payload as the run input
/// (seeded into `run.trigger`, per the node-catalog contract).
- async fn handle_app_event(&self, toolkit: &str, trigger_slug: &str, payload: &Value) {
- let config = self.config_for_scope();
+ async fn handle_app_event(
+ &self,
+ ctx: crate::core::runtime::DispatchCtx,
+ toolkit: &str,
+ trigger_slug: &str,
+ payload: &Value,
+ ) {
+ let config = self.config_for_scope(&ctx);
let (flows, skipped) = match store::list_enabled_flows(&config) {
Ok(result) => result,
Err(e) => {
@@ -181,6 +194,7 @@ impl FlowTriggerSubscriber {
matched += 1;
let inputs = pinned_trigger_inputs(&flow);
self.spawn_run(
+ &ctx,
flow.id.clone(),
payload.clone(),
inputs,
@@ -201,6 +215,7 @@ impl FlowTriggerSubscriber {
/// burst of matching `app_event`s cannot run the same flow concurrently.
fn spawn_run(
&self,
+ ctx: &crate::core::runtime::DispatchCtx,
flow_id: String,
input: Value,
inputs: serde_json::Map<String, Value>,
@@ -211,7 +226,7 @@ impl FlowTriggerSubscriber {
return;
};
- let config = self.config_for_scope();
+ let config = self.config_for_scope(ctx);
// Scoped: the run keeps the owner's context the handler entered.
crate::core::runtime::spawn_scoped(async move {
// Held for the lifetime of the run; released on drop (including
@@ -269,8 +284,8 @@ impl EventHandler<DomainEvent> for FlowTriggerSubscriber {
payload,
..
} => {
- crate::storage::agents::for_each_live_scope("flows app_event", || {
- self.handle_app_event(toolkit, trigger, payload)
+ crate::storage::agents::for_each_live_scope("flows app_event", |ctx| {
+ self.handle_app_event(ctx, toolkit, trigger, payload)
})
.await;
}