Skip to content

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

test(cron): cover cancelled manual run callback lifecycle

Steven Enamakel657bb0338b21
diff --git a/crates/openhuman-core/src/cron/ops_delivery_tests.rs b/crates/openhuman-core/src/cron/ops_delivery_tests.rs
index ab670709e0..ea1629e2d8 100644
--- a/crates/openhuman-core/src/cron/ops_delivery_tests.rs
+++ b/crates/openhuman-core/src/cron/ops_delivery_tests.rs
@@ -148,3 +148,180 @@ async fn run_job_now_pairs_trigger_and_completion_after_delivery() {
let runs = cron::list_runs(&config, &job.id, 10).unwrap();
assert_eq!(runs[0].delivery_status, None);
}
+
+#[tokio::test]
+async fn cancelled_waiting_caller_keeps_manual_run_callback_served_until_completion() {
+ use crate::core::events::DomainEvent;
+ use tinybus::EventHandler;
+ use tinyflows_bus::schedule::callbacks;
+ use tokio::sync::{mpsc, oneshot};
+
+ struct Completions {
+ job_id: String,
+ name: String,
+ tx: mpsc::UnboundedSender<bool>,
+ }
+ #[async_trait]
+ impl EventHandler<DomainEvent> for Completions {
+ fn name(&self) -> &str {
+ &self.name
+ }
+ async fn handle(&self, event: &DomainEvent) {
+ if let DomainEvent::CronJobCompleted {
+ job_id, success, ..
+ } = event
+ && job_id == &self.job_id
+ {
+ let _ = self.tx.send(*success);
+ }
+ }
+ }
+
+ crate::core::bus::init().await.unwrap();
+ let tmp = TempDir::new().unwrap();
+ let config = tests::test_config(&tmp);
+ let name = format!("ops-run-now-cancel-{}", uuid::Uuid::new_v4().simple());
+ let job = crate::cron::system_jobs::ensure_system_job(
+ &config,
+ &name,
+ tinyflows_bus::schedule::types::Schedule::Every {
+ every_ms: 3_600_000,
+ },
+ )
+ .unwrap();
+ let (started_tx, started_rx) = oneshot::channel();
+ let (release_tx, release_rx) = oneshot::channel();
+ let started_tx = Arc::new(Mutex::new(Some(started_tx)));
+ let release_rx = Arc::new(Mutex::new(Some(release_rx)));
+ let calls = Arc::new(std::sync::atomic::AtomicUsize::new(0));
+ let handler_calls = Arc::clone(&calls);
+ let _handler = crate::cron::system_job_handlers::register(
+ &name,
+ Arc::new(move |_| {
+ let first_call = handler_calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst) == 0;
+ let started = first_call.then(|| started_tx.lock().unwrap().take().unwrap());
+ let release = first_call.then(|| release_rx.lock().unwrap().take().unwrap());
+ Box::pin(async move {
+ if let Some(started) = started {
+ let _ = started.send(());
+ }
+ if let Some(release) = release {
+ release.await.map_err(|error| error.to_string())?;
+ }
+ Ok(())
+ })
+ }),
+ );
+
+ let (events_tx, mut events_rx) = mpsc::unbounded_channel();
+ let _subscription = crate::core::bus::BUS
+ .subscribe(Arc::new(Completions {
+ job_id: job.id.clone(),
+ name: format!("test::cron::cancelled_waiter::{}", job.id),
+ tx: events_tx,
+ }))
+ .unwrap();
+ let first_run_id = format!("cancelled-{}", uuid::Uuid::new_v4());
+ let first_ctx = crate::core::runtime::DispatchCtx::ambient();
+ let first_config = config.clone();
+ let first_job = job.clone();
+ let first_id = first_run_id.clone();
+ let waiting_call = tokio::spawn(async move {
+ crate::cron::scheduler::run_job_now(
+ &first_ctx,
+ &first_config,
+ first_job,
+ first_id,
+ true,
+ tinyflows_bus::schedule::ManualDelivery::Requested,
+ )
+ .await
+ });
+ tokio::time::timeout(std::time::Duration::from_secs(3), started_rx)
+ .await
+ .expect("the module started the admitted system job")
+ .expect("the job-start signal remains open");
+
+ waiting_call.abort();
+ assert!(waiting_call.await.unwrap_err().is_cancelled());
+ let address = crate::cron::bus_host::take_callback_address_for_test(&first_run_id)
+ .expect("the cancelled request's callback address was recorded");
+ let probe = crate::modules::runtime::runtime()
+ .await
+ .unwrap()
+ .connection()
+ .proxy(
+ &address.destination,
+ &address.object_path,
+ tinyflows_bus::schedule::CALLBACK_INTERFACE,
+ )
+ .unwrap();
+ let failed = tinyflows_bus::schedule::Attempt {
+ success: false,
+ output: "probe".into(),
+ retryable: false,
+ raw_detail: None,
+ };
+ tokio::time::timeout(std::time::Duration::from_secs(3), async {
+ loop {
+ let result: Result<(), _> = probe
+ .call(callbacks::FAILED, (&job, "lifecycle-probe", &failed))
+ .await;
+ if result.is_err() {
+ break;
+ }
+ tokio::task::yield_now().await;
+ }
+ })
+ .await
+ .expect("the current caller cancellation unregisters its callback");
+ let _ = release_tx.send(());
+
+ let second_run_id = format!("after-cancel-{}", uuid::Uuid::new_v4());
+ let second_ctx = crate::core::runtime::DispatchCtx::ambient();
+ let second_config = config.clone();
+ let second_job = job.clone();
+ let second_id = second_run_id.clone();
+ tokio::time::timeout(std::time::Duration::from_secs(5), async move {
+ loop {
+ match crate::cron::scheduler::run_job_now(
+ &second_ctx,
+ &second_config,
+ second_job.clone(),
+ second_id.clone(),
+ true,
+ tinyflows_bus::schedule::ManualDelivery::Requested,
+ )
+ .await
+ {
+ Ok(tinyflows_bus::schedule::RunJobReply::Completed {
+ success: true, ..
+ }) => break,
+ Ok(tinyflows_bus::schedule::RunJobReply::Refused {
+ reason: tinyflows_bus::schedule::RunJobRefusal::AlreadyRunning,
+ }) => {
+ tokio::task::yield_now().await;
+ }
+ Err(error) if error.to_string().contains("AlreadyRunning") => {
+ tokio::task::yield_now().await;
+ }
+ other => panic!("second manual run did not complete after the first settled: {other:?}"),
+ }
+ }
+ })
+ .await
+ .expect("the first manual worker settles and releases its claim");
+
+ let completions = tokio::time::timeout(std::time::Duration::from_secs(3), async {
+ vec![events_rx.recv().await.unwrap(), events_rx.recv().await.unwrap()]
+ })
+ .await;
+ assert_eq!(
+ completions,
+ Ok(vec![true, true]),
+ "cancelling the waiter must not orphan the module worker before terminal projection"
+ );
+ let runs = cron::list_runs(&config, &job.id, 10).unwrap();
+ assert!(runs.iter().any(|run| run.run_id == first_run_id && run.status == "ok"));
+ assert!(runs.iter().any(|run| run.run_id == second_run_id && run.status == "ok"));
+}