Skip to content

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

feat(cron): route cron store through document backend when configured

Steven Enamakel5dd68b4ce52a
diff --git a/crates/openhuman-core/src/cron/store.rs b/crates/openhuman-core/src/cron/store.rs
index 374d6bcb3f..f9b5f28827 100644
--- a/crates/openhuman-core/src/cron/store.rs
+++ b/crates/openhuman-core/src/cron/store.rs
@@ -1,5 +1,9 @@
//! Cron persistence: a thin host wrapper over `tinyflows_sqlite::schedule`.
//!
+//! With a storage backend configured ([`crate::storage`]) every function is
+//! served by `tinyflows_drivers::schedule::CronDocuments` instead, in the
+//! acting agent's storage scope, with the same limits.
+//!
//! The SQLite job store and run history live upstream (schema, CRUD, output
//! truncation, pruning). This module only turns the host [`Config`] into the
//! store's [`CronStoreOptions`] — database path under the workspace, run-history
@@ -10,6 +14,7 @@ use anyhow::Result;
use chrono::{DateTime, Utc};
use tinyflows_schedule::DeliveryStatus;
use tinyflows_schedule::{CronJob, CronJobPatch, CronRun, DeliveryConfig, Schedule, SessionTarget};
+use tinyflows_drivers::schedule::CronDocuments;
use tinyflows_sqlite::schedule::{self as upstream, AgentJobSpec, CronStoreOptions};
/// Builds the store options from the host config: `<workspace>/cron/jobs.db`,
@@ -22,7 +27,26 @@ fn opts(config: &Config) -> CronStoreOptions {
}
}
+/// The document store for this call when the host configured a backend.
+fn documents(config: &Config) -> Result<Option<CronDocuments>> {
+ Ok(crate::storage::current_scoped()?.map(|scoped| {
+ CronDocuments::new(std::sync::Arc::clone(scoped.documents()))
+ .with_limits(config.cron.max_run_history, config.scheduler.max_tasks)
+ }))
+}
+
+/// Runs a document-store call from this synchronous API.
+fn run<T: Send + 'static>(
+ future: impl std::future::Future<Output = Result<T>> + Send + 'static,
+) -> Result<T> {
+ crate::storage::block_on_anyhow(future)
+}
+
pub fn add_job(config: &Config, expression: &str, command: &str) -> Result<CronJob> {
+ if let Some(docs) = documents(config)? {
+ let (expression, command) = (expression.to_string(), command.to_string());
+ return run(async move { docs.add_job(&expression, &command).await });
+ }
upstream::add_job(&opts(config), expression, command)
}
@@ -32,6 +56,10 @@ pub fn add_shell_job(
schedule: Schedule,
command: &str,
) -> Result<CronJob> {
+ if let Some(docs) = documents(config)? {
+ let command = command.to_string();
+ return run(async move { docs.add_shell_job(name, schedule, &command).await });
+ }
upstream::add_shell_job(&opts(config), name, schedule, command)
}
@@ -46,6 +74,10 @@ pub fn add_agent_job(
delivery: Option<DeliveryConfig>,
delete_after_run: bool,
) -> Result<CronJob> {
+ if let Some(docs) = documents(config)? {
+ let prompt = prompt.to_string();
+ return run(async move { docs.add_agent_job(name, schedule, &prompt, session_target, model, delivery, delete_after_run).await });
+ }
upstream::add_agent_job(
&opts(config),
name,
@@ -73,6 +105,10 @@ pub fn add_agent_job_with_definition(
agent_id: Option<String>,
enabled: bool,
) -> Result<CronJob> {
+ if let Some(docs) = documents(config)? {
+ let prompt = prompt.to_string();
+ return run(async move { docs.add_agent_job_with_definition(name, schedule, &prompt, session_target, model, delivery, delete_after_run, agent_id, enabled).await });
+ }
upstream::add_agent_job_with_definition(
&opts(config),
name,
@@ -89,6 +125,9 @@ pub fn add_agent_job_with_definition(
/// Adds an agent job described by `spec`, including its origin conversation.
pub fn add_agent_job_from_spec(config: &Config, spec: AgentJobSpec) -> Result<CronJob> {
+ if let Some(docs) = documents(config)? {
+ return run(async move { docs.add_agent_job_from_spec(spec).await });
+ }
upstream::add_agent_job_from_spec(&opts(config), spec)
}
@@ -99,22 +138,43 @@ pub fn add_flow_schedule_job(
flow_id: &str,
schedule: Schedule,
) -> Result<CronJob> {
+ if let Some(docs) = documents(config)? {
+ let flow_id = flow_id.to_string();
+ return run(async move { docs.add_flow_schedule_job(&flow_id, schedule).await });
+ }
upstream::add_flow_schedule_job(&opts(config), flow_id, schedule)
}
pub fn find_flow_schedule_job(config: &Config, flow_id: &str) -> Result<Option<CronJob>> {
+ if let Some(docs) = documents(config)? {
+ let flow_id = flow_id.to_string();
+ return run(async move { docs.find_flow_schedule_job(&flow_id).await });
+ }
upstream::find_flow_schedule_job(&opts(config), flow_id)
}
pub fn list_jobs(config: &Config) -> Result<Vec<CronJob>> {
+ if let Some(docs) = documents(config)? {
+ return run(async move { docs.list_jobs().await });
+ }
upstream::list_jobs(&opts(config))
}
pub fn get_job(config: &Config, job_id: &str) -> Result<CronJob> {
+ if let Some(docs) = documents(config)? {
+ let job_id = job_id.to_string();
+ return run(async move { docs.get_job(&job_id).await });
+ }
upstream::get_job(&opts(config), job_id)
}
pub fn remove_job(config: &Config, id: &str) -> Result<()> {
+ if let Some(docs) = documents(config)? {
+ let id = id.to_string();
+ run(async move { docs.remove_job(&id).await })?;
+ println!("✅ Removed cron job {id}");
+ return Ok(());
+ }
upstream::remove_job(&opts(config), id)?;
println!("✅ Removed cron job {id}");
Ok(())
@@ -122,19 +182,32 @@ pub fn remove_job(config: &Config, id: &str) -> Result<()> {
/// Deletes every cron job in the workspace (E2E `openhuman.test_reset`).
pub fn clear_all_jobs(config: &Config) -> Result<usize> {
+ if let Some(docs) = documents(config)? {
+ return run(async move { docs.clear_all_jobs().await });
+ }
upstream::clear_all_jobs(&opts(config))
}
/// Removes duplicate jobs sharing a `name`, keeping the one with most history.
pub fn dedup_named_jobs(config: &Config) -> Result<usize> {
+ if let Some(docs) = documents(config)? {
+ return run(async move { docs.dedup_named_jobs().await });
+ }
upstream::dedup_named_jobs(&opts(config))
}
pub fn due_jobs(config: &Config, now: DateTime<Utc>) -> Result<Vec<CronJob>> {
+ if let Some(docs) = documents(config)? {
+ return run(async move { docs.due_jobs(now).await });
+ }
upstream::due_jobs(&opts(config), now)
}
pub fn update_job(config: &Config, job_id: &str, patch: CronJobPatch) -> Result<CronJob> {
+ if let Some(docs) = documents(config)? {
+ let job_id = job_id.to_string();
+ return run(async move { docs.update_job(&job_id, patch).await });
+ }
upstream::update_job(&opts(config), job_id, patch)
}
@@ -145,6 +218,10 @@ pub fn record_last_run(
success: bool,
output: &str,
) -> Result<()> {
+ if let Some(docs) = documents(config)? {
+ let (job_id, output) = (job_id.to_string(), output.to_string());
+ return run(async move { docs.record_last_run(&job_id, finished_at, success, &output).await });
+ }
upstream::record_last_run(&opts(config), job_id, finished_at, success, output)
}
@@ -154,6 +231,10 @@ pub fn reschedule_after_run(
success: bool,
output: &str,
) -> Result<()> {
+ if let Some(docs) = documents(config)? {
+ let (job, output) = (job.clone(), output.to_string());
+ return run(async move { docs.reschedule_after_run(&job, success, &output).await });
+ }
upstream::reschedule_after_run(&opts(config), job, success, output)
}
@@ -166,6 +247,10 @@ pub fn record_run(
output: Option<&str>,
duration_ms: i64,
) -> Result<()> {
+ if let Some(docs) = documents(config)? {
+ let (job_id, status, output) = (job_id.to_string(), status.to_string(), output.map(str::to_string));
+ return run(async move { docs.record_run(&job_id, started_at, finished_at, &status, output.as_deref(), duration_ms).await });
+ }
upstream::record_run(
&opts(config),
job_id,
@@ -189,6 +274,10 @@ pub fn record_run_with_delivery(
duration_ms: i64,
delivery_status: Option<DeliveryStatus>,
) -> Result<()> {
+ if let Some(docs) = documents(config)? {
+ let (job_id, status, output) = (job_id.to_string(), status.to_string(), output.map(str::to_string));
+ return run(async move { docs.record_run_with_delivery(&job_id, started_at, finished_at, &status, output.as_deref(), duration_ms, delivery_status).await });
+ }
upstream::record_run_with_delivery(
&opts(config),
job_id,
@@ -203,10 +292,18 @@ pub fn record_run_with_delivery(
/// Removes "queued" placeholder rows so only the real result row remains.
pub fn delete_queued_runs(config: &Config, job_id: &str) -> Result<usize> {
+ if let Some(docs) = documents(config)? {
+ let job_id = job_id.to_string();
+ return run(async move { docs.delete_queued_runs(&job_id).await });
+ }
upstream::delete_queued_runs(&opts(config), job_id)
}
pub fn list_runs(config: &Config, job_id: &str, limit: usize) -> Result<Vec<CronRun>> {
+ if let Some(docs) = documents(config)? {
+ let job_id = job_id.to_string();
+ return run(async move { docs.list_runs(&job_id, limit).await });
+ }
upstream::list_runs(&opts(config), job_id, limit)
}