Skip to content

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

refactor(cron): move manual run state out of scheduler

Steven Enamakeld95b2d605e9d
diff --git a/crates/openhuman-core/src/cron/bus_host.rs b/crates/openhuman-core/src/cron/bus_host.rs
index b8291a841d..96288bb0ad 100644
--- a/crates/openhuman-core/src/cron/bus_host.rs
+++ b/crates/openhuman-core/src/cron/bus_host.rs
@@ -230,12 +230,14 @@ impl ScheduleHost {
let binding = self.binding.clone();
let run_id = projection.run_id.clone();
self.binding
- .offload(async move {
- run_delivery::deliver(&binding, &projection).await;
- release_manual_host(&run_id);
- })
+ .offload(async move { run_delivery::deliver(&binding, &projection).await })
.await
- .map_err(|e| bus_error("Deliver", e))
+ .map_err(|e| bus_error("Deliver", e))?;
+ tokio::spawn(async move {
+ tokio::time::sleep(std::time::Duration::from_millis(100)).await;
+ release_manual_host(&run_id);
+ });
+ Ok(())
}
/// A flow row's schedule tick (or a `system:<name>` job).
@@ -265,9 +267,14 @@ pub(crate) fn retain_manual_host(run_id: &str, host: HostRegistration) {
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.insert(run_id.to_string(), host);
+ let run_id = run_id.to_string();
+ tokio::spawn(async move {
+ tokio::time::sleep(std::time::Duration::from_secs(2 * 60 * 60)).await;
+ release_manual_host(&run_id);
+ });
}
-fn release_manual_host(run_id: &str) {
+pub(crate) fn release_manual_host(run_id: &str) {
MANUAL_HOSTS
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
diff --git a/crates/openhuman-core/src/cron/scheduler.rs b/crates/openhuman-core/src/cron/scheduler.rs
index 82e5743672..6b9a83372b 100644
--- a/crates/openhuman-core/src/cron/scheduler.rs
+++ b/crates/openhuman-core/src/cron/scheduler.rs
@@ -23,7 +23,6 @@ mod dispatcher;
pub(crate) mod executor;
mod failure_classification;
mod live_agents;
-mod manual_run;
mod origin_context;
mod origin_delivery;
pub(crate) mod run_delivery;
@@ -54,9 +53,7 @@ use crate::cron::documents_port::DocScope;
use crate::modules::tinyflows as module;
use crate::security::SecurityPolicy;
use anyhow::{anyhow, Result};
-use std::collections::{HashMap, HashSet};
-use std::sync::atomic::{AtomicBool, Ordering};
-use std::sync::{Arc, LazyLock, Mutex};
+use std::sync::Arc;
use tinyflows_bus::schedule::types::CronJob;
use tinyflows_bus::schedule::{
members, Attempt, ManualDelivery, RunJobNow, RunJobReply, StartScheduler, StoreBackend,
diff --git a/crates/openhuman-core/src/cron/scheduler/manual_run.rs b/crates/openhuman-core/src/cron/scheduler/manual_run.rs
deleted file mode 100644
index 51858c9048..0000000000
--- a/crates/openhuman-core/src/cron/scheduler/manual_run.rs
+++ /dev/null
@@ -1,70 +0,0 @@
-use super::*;
-
-pub(super) const MIN_POLL_SECONDS: u64 = 5;
-
-/// What the module answers when another run of the job holds its claim.
-pub(crate) const ALREADY_RUNNING: &str = "job is already running";
-
-/// Whether the host's cron poll loop is running in this process.
-static LOOP_RUNNING: AtomicBool = AtomicBool::new(false);
-
-/// The poll-loop slot: one loop per process, or two would each see the same
-/// due rows. Released when dropped.
-pub(super) struct LoopSlot;
-
-impl LoopSlot {
- pub(super) fn acquire() -> Option<Self> {
- LOOP_RUNNING
- .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
- .ok()
- .map(|_| Self)
- }
-}
-
-impl Drop for LoopSlot {
- fn drop(&mut self) {
- LOOP_RUNNING.store(false, Ordering::SeqCst);
- }
-}
-
-/// Whether a cron poll loop is running in this process.
-pub fn is_running() -> bool {
- LOOP_RUNNING.load(Ordering::SeqCst)
-}
-
-/// Job ids a manual run (Run Now, the run tool) holds in this process.
-static MANUAL_RUNS: LazyLock<Mutex<HashSet<String>>> = LazyLock::new(Mutex::default);
-
-/// A held manual run. Released on drop, including on panic or future
-/// cancellation, so an aborted task can never lock a job id.
-pub(crate) struct RunGuard(String);
-
-impl Drop for RunGuard {
- fn drop(&mut self) {
- MANUAL_RUNS
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner)
- .remove(&self.0);
- }
-}
-
-/// Claims `job_id` for a manual run, or `None` when another manual run holds
-/// it. A scheduled run of the same job is refused later, by the module's own
-/// claim (`ExecuteJob` answers [`ALREADY_RUNNING`]).
-pub(crate) fn try_acquire_run(job_id: &str) -> Option<RunGuard> {
- MANUAL_RUNS
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner)
- .insert(job_id.to_string())
- .then(|| RunGuard(job_id.to_string()))
-}
-
-/// The module's retry settings from the operator's reliability config.
-pub(crate) fn scheduler_config(config: &crate::config::Config) -> serde_json::Value {
- serde_json::json!({
- "poll_secs": config.reliability.scheduler_poll_secs,
- "max_concurrent": config.scheduler.max_concurrent,
- "retries": config.reliability.scheduler_retries,
- "backoff_ms": config.reliability.provider_backoff_ms,
- })
-}