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/mod.rs,crates/openhuman-core/src/cron/system_job

Steven Enamakelc4cd4f6b821b
diff --git a/crates/openhuman-core/src/cron/mod.rs b/crates/openhuman-core/src/cron/mod.rs
index b36e660e8b..636f4fbd96 100644
--- a/crates/openhuman-core/src/cron/mod.rs
+++ b/crates/openhuman-core/src/cron/mod.rs
@@ -16,6 +16,7 @@ pub mod policy;
mod schemas;
pub mod seed;
mod store;
+pub mod system_job_handlers;
pub mod system_jobs;
pub mod tools;
diff --git a/crates/openhuman-core/src/cron/system_job_handlers.rs b/crates/openhuman-core/src/cron/system_job_handlers.rs
new file mode 100644
index 0000000000..b864485e0b
--- /dev/null
+++ b/crates/openhuman-core/src/cron/system_job_handlers.rs
@@ -0,0 +1,40 @@
+//! In-process handlers for system cron jobs. Stub (RED).
+
+use std::sync::Arc;
+
+use futures::future::BoxFuture;
+
+/// The job a handler is asked to run.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct SystemJobContext {
+ /// The cron row's id.
+ pub job_id: String,
+ /// The system job's name (`system:<name>`).
+ pub name: String,
+}
+
+/// A handler for one system job.
+pub type SystemJobHandler =
+ Arc<dyn Fn(SystemJobContext) -> BoxFuture<'static, Result<(), String>> + Send + Sync>;
+
+/// Keeps a handler registered while held.
+pub struct SystemJobRegistration;
+
+/// Register `handler` for system job `name`.
+pub fn register(_name: &str, _handler: SystemJobHandler) -> SystemJobRegistration {
+ SystemJobRegistration
+}
+
+/// Whether `name` has a handler.
+pub fn has_handler(_name: &str) -> bool {
+ false
+}
+
+/// Run the handler for `ctx.name`.
+pub async fn dispatch(_ctx: SystemJobContext) -> Option<Result<(), String>> {
+ None
+}
+
+#[cfg(test)]
+#[path = "system_job_handlers_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/cron/system_job_handlers_tests.rs b/crates/openhuman-core/src/cron/system_job_handlers_tests.rs
new file mode 100644
index 0000000000..007d9a039f
--- /dev/null
+++ b/crates/openhuman-core/src/cron/system_job_handlers_tests.rs
@@ -0,0 +1,81 @@
+use super::*;
+use std::sync::atomic::{AtomicUsize, Ordering};
+
+fn context(name: &str) -> SystemJobContext {
+ SystemJobContext {
+ job_id: format!("job-{name}"),
+ name: name.to_string(),
+ }
+}
+
+fn answering(result: Result<(), String>, calls: Arc<AtomicUsize>) -> SystemJobHandler {
+ Arc::new(move |_ctx| {
+ calls.fetch_add(1, Ordering::SeqCst);
+ let result = result.clone();
+ Box::pin(async move { result })
+ })
+}
+
+#[tokio::test]
+async fn a_job_without_a_handler_is_not_dispatched() {
+ assert!(dispatch(context("handlers-unclaimed")).await.is_none());
+}
+
+#[tokio::test]
+async fn a_registered_handler_receives_the_job_and_its_result_is_returned() {
+ let seen = Arc::new(std::sync::Mutex::new(None));
+ let sink = Arc::clone(&seen);
+ let _registration = register(
+ "handlers-echo",
+ Arc::new(move |ctx: SystemJobContext| {
+ *sink.lock().unwrap() = Some(ctx.clone());
+ Box::pin(async { Err("handler said no".to_string()) })
+ }),
+ );
+ let result = dispatch(context("handlers-echo")).await;
+ assert_eq!(result, Some(Err("handler said no".to_string())));
+ let ctx = seen.lock().unwrap().clone().expect("handler ran");
+ assert_eq!(ctx.name, "handlers-echo");
+ assert_eq!(ctx.job_id, "job-handlers-echo");
+}
+
+#[tokio::test]
+async fn dropping_the_registration_unregisters_the_handler() {
+ let calls = Arc::new(AtomicUsize::new(0));
+ let registration = register("handlers-dropped", answering(Ok(()), Arc::clone(&calls)));
+ assert_eq!(dispatch(context("handlers-dropped")).await, Some(Ok(())));
+ drop(registration);
+ assert!(dispatch(context("handlers-dropped")).await.is_none());
+ assert_eq!(calls.load(Ordering::SeqCst), 1);
+}
+
+#[tokio::test]
+async fn a_replaced_registration_does_not_remove_its_replacement() {
+ let first_calls = Arc::new(AtomicUsize::new(0));
+ let second_calls = Arc::new(AtomicUsize::new(0));
+ let first = register("handlers-replaced", answering(Ok(()), Arc::clone(&first_calls)));
+ let second = register(
+ "handlers-replaced",
+ answering(Err("second".into()), Arc::clone(&second_calls)),
+ );
+ drop(first);
+ assert_eq!(
+ dispatch(context("handlers-replaced")).await,
+ Some(Err("second".to_string()))
+ );
+ assert_eq!(first_calls.load(Ordering::SeqCst), 0);
+ assert_eq!(second_calls.load(Ordering::SeqCst), 1);
+ drop(second);
+}
+
+#[test]
+fn has_handler_reports_registration() {
+ assert!(!has_handler("handlers-presence"));
+ let registration = register(
+ "handlers-presence",
+ answering(Ok(()), Arc::new(AtomicUsize::new(0))),
+ );
+ assert!(has_handler("handlers-presence"));
+ drop(registration);
+ assert!(!has_handler("handlers-presence"));
+}