Skip to content

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

chore: files changed crates/openhuman-core/src/cron/bus_host.rs,crates/openhuman-core/src/cron/sched

Steven Enamakelad9f49390080
diff --git a/crates/openhuman-core/src/cron/bus_host.rs b/crates/openhuman-core/src/cron/bus_host.rs
index 78bcd1d3ec..43e6c6184c 100644
--- a/crates/openhuman-core/src/cron/bus_host.rs
+++ b/crates/openhuman-core/src/cron/bus_host.rs
@@ -26,6 +26,7 @@
//! bus runtime is for serving. The binding's context is re-entered around it.
use std::future::Future;
+use std::collections::HashSet;
use std::sync::{Arc, RwLock, Weak};
use tinybus::Result as BusResult;
@@ -57,6 +58,7 @@ pub(crate) struct Binding {
runtime: Option<tokio::runtime::Handle>,
#[cfg(feature = "flows")]
flows: Arc<std::sync::OnceLock<crate::flows::bus::FlowTriggerSubscriber>>,
+ triggered_runs: Arc<std::sync::Mutex<HashSet<String>>>,
}
impl Binding {
@@ -70,6 +72,7 @@ impl Binding {
runtime: tokio::runtime::Handle::try_current().ok(),
#[cfg(feature = "flows")]
flows: Arc::new(std::sync::OnceLock::new()),
+ triggered_runs: Arc::new(std::sync::Mutex::new(HashSet::new())),
}
}
@@ -100,6 +103,35 @@ impl Binding {
.unwrap_or_else(std::sync::PoisonError::into_inner) = config.clone();
}
+ /// Publish the start event once per logical scheduler run, across Execute
+ /// retries and the separate ScheduleTrigger callback path.
+ pub(crate) fn publish_triggered_once(&self, job: &CronJob, run_id: &str) {
+ let mut runs = self
+ .triggered_runs
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ if runs.contains(run_id) {
+ return;
+ }
+ if runs.len() >= 1024 {
+ runs.clear();
+ }
+ runs.insert(run_id.to_string());
+ drop(runs);
+ crate::core::bus::BUS.publish(crate::core::events::DomainEvent::CronJobTriggered {
+ job_id: job.id.clone(),
+ job_name: job.name.clone().unwrap_or_default(),
+ job_type: format!("{:?}", job.job_type),
+ });
+ }
+
+ pub(crate) fn finish_triggered_run(&self, run_id: &str) {
+ self.triggered_runs
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .remove(run_id);
+ }
+
/// The dispatch context the bound context stands for: the bound one, or
/// (when it has since been dropped, or none was bound) the calling task's.
pub(crate) fn dispatch_ctx(&self) -> DispatchCtx {
diff --git a/crates/openhuman-core/src/cron/scheduler/executor.rs b/crates/openhuman-core/src/cron/scheduler/executor.rs
index bfe2d308a8..78799e65e9 100644
--- a/crates/openhuman-core/src/cron/scheduler/executor.rs
+++ b/crates/openhuman-core/src/cron/scheduler/executor.rs
@@ -100,11 +100,7 @@ impl HostExecutor {
let config = self.binding.config();
if self.begin(run_id) {
warn_if_high_frequency_agent_job(&config, job).await;
- BUS.publish(DomainEvent::CronJobTriggered {
- job_id: job.id.clone(),
- job_name: job.name.clone().unwrap_or_default(),
- job_type: format!("{:?}", job.job_type),
- });
+ self.binding.publish_triggered_once(job, run_id);
}
// A hosted-profile sweep runs agent jobs only.
if let Some(refusal) = kind_refusal(&self.binding, &job.job_type) {
@@ -244,6 +240,7 @@ impl HostExecutor {
/// The retries-exhausted (or permanent) report, once per failed run.
pub(crate) async fn failed(&self, job: &CronJob, run_id: &str, attempt: &Attempt) {
let state = self.forget(run_id).unwrap_or_default();
+ self.binding.finish_triggered_run(run_id);
if !matches!(job.job_type, JobType::Agent) {
return;
}
diff --git a/crates/openhuman-core/src/cron/scheduler/run_delivery.rs b/crates/openhuman-core/src/cron/scheduler/run_delivery.rs
index e9576f49b5..55af8dead5 100644
--- a/crates/openhuman-core/src/cron/scheduler/run_delivery.rs
+++ b/crates/openhuman-core/src/cron/scheduler/run_delivery.rs
@@ -49,6 +49,7 @@ pub(crate) async fn deliver(
success,
output: crate::util::truncate_with_ellipsis(&attempt.output, 512),
});
+ binding.finish_triggered_run(run_id);
BUS.publish(DomainEvent::HealthChanged {
component: "scheduler".to_string(),
healthy: success,
@@ -106,13 +107,6 @@ pub(crate) fn stand_in_for_test(command: &str) -> CronJob {
pub(crate) async fn trigger(binding: &Binding, flow_id: &str, run_id: &str) -> Attempt {
binding
.within(async {
- if let Some(refusal) = kind_refusal(binding, &JobType::Flow) {
- return Attempt {
- success: false,
- output: refusal,
- retryable: false,
- };
- }
let config = binding.config();
let job = match crate::cron::client::store::<Option<CronJob>>(
&config,
@@ -129,6 +123,14 @@ pub(crate) async fn trigger(binding: &Binding, flow_id: &str, run_id: &str) -> A
stand_in(flow_id)
}
};
+ binding.publish_triggered_once(&job, run_id);
+ if let Some(refusal) = kind_refusal(binding, &job.job_type) {
+ return Attempt {
+ success: false,
+ output: refusal,
+ retryable: false,
+ };
+ }
let Some(handler) = kind_handler(&JobType::Flow) else {
return Attempt {
success: false,
@@ -136,11 +138,6 @@ pub(crate) async fn trigger(binding: &Binding, flow_id: &str, run_id: &str) -> A
retryable: false,
};
};
- BUS.publish(DomainEvent::CronJobTriggered {
- job_id: job.id.clone(),
- job_name: job.name.clone().unwrap_or_default(),
- job_type: format!("{:?}", job.job_type),
- });
let outcome = handler
.run(&JobRunContext::new(binding.clone()), &job, run_id)
.await;