Skip to content

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

style(cron): reformat long expressions to satisfy line width

Steven Enamakel1357019e521d
diff --git a/crates/openhuman-core/src/cron/claims.rs b/crates/openhuman-core/src/cron/claims.rs
index babdf534a1..6d59ff21c1 100644
--- a/crates/openhuman-core/src/cron/claims.rs
+++ b/crates/openhuman-core/src/cron/claims.rs
@@ -50,7 +50,10 @@ pub async fn claim_occurrence(
"claimed_at_ms": now_ms,
"lease_until_ms": now_ms + CLAIM_LEASE_MS,
});
- match docs.put(CLAIMS, &id, doc.clone(), Precondition::Absent).await {
+ match docs
+ .put(CLAIMS, &id, doc.clone(), Precondition::Absent)
+ .await
+ {
Ok(_) => return Ok(true),
Err(error) if error.kind() == ErrorKind::Conflict => {}
Err(error) => return Err(error).context("claiming the occurrence"),
diff --git a/crates/openhuman-core/src/cron/claims_tests.rs b/crates/openhuman-core/src/cron/claims_tests.rs
new file mode 100644
index 0000000000..3ad219cecd
--- /dev/null
+++ b/crates/openhuman-core/src/cron/claims_tests.rs
@@ -0,0 +1,88 @@
+use super::*;
+use crate::cron::{Schedule, SessionTarget};
+use crate::storage::{MemoryStorage, Scope, StorageBackend};
+use chrono::Duration;
+use std::sync::Arc;
+use tinyflows_drivers::schedule::CronDocuments;
+
+async fn job(docs: &Arc<dyn DocumentStore>) -> CronJob {
+ CronDocuments::new(Arc::clone(docs))
+ .add_agent_job(
+ None,
+ Schedule::Every {
+ every_ms: 3_600_000,
+ },
+ "ping",
+ SessionTarget::Isolated,
+ None,
+ None,
+ false,
+ )
+ .await
+ .unwrap()
+}
+
+fn docs() -> Arc<dyn DocumentStore> {
+ let scoped = MemoryStorage::new()
+ .for_scope(&Scope::new("claims").unwrap())
+ .unwrap();
+ Arc::clone(scoped.documents())
+}
+
+#[tokio::test]
+async fn competing_holders_claim_an_occurrence_once() {
+ let docs = docs();
+ let job = job(&docs).await;
+ let now = Utc::now();
+ let (a, b) = tokio::join!(
+ claim_occurrence(docs.as_ref(), &job, "node-a", now),
+ claim_occurrence(docs.as_ref(), &job, "node-b", now),
+ );
+ assert_eq!(
+ [a.unwrap(), b.unwrap()].iter().filter(|won| **won).count(),
+ 1
+ );
+ let again = claim_occurrence(docs.as_ref(), &job, "node-a", now).await;
+ assert!(
+ !again.unwrap(),
+ "a holder cannot claim the same occurrence twice"
+ );
+}
+
+#[tokio::test]
+async fn a_lapsed_claim_is_retaken_by_one_holder() {
+ let docs = docs();
+ let job = job(&docs).await;
+ let now = Utc::now();
+ assert!(claim_occurrence(docs.as_ref(), &job, "dead", now)
+ .await
+ .unwrap());
+ let later = now + Duration::milliseconds(CLAIM_LEASE_MS + 1);
+ let (a, b) = tokio::join!(
+ claim_occurrence(docs.as_ref(), &job, "node-a", later),
+ claim_occurrence(docs.as_ref(), &job, "node-b", later),
+ );
+ assert_eq!(
+ [a.unwrap(), b.unwrap()].iter().filter(|won| **won).count(),
+ 1
+ );
+}
+
+#[tokio::test]
+async fn each_occurrence_has_its_own_claim_and_old_claims_are_pruned() {
+ let docs = docs();
+ let mut job = job(&docs).await;
+ let now = Utc::now();
+ assert!(claim_occurrence(docs.as_ref(), &job, "node-a", now)
+ .await
+ .unwrap());
+ job.next_run += Duration::hours(1);
+ assert!(claim_occurrence(docs.as_ref(), &job, "node-a", now)
+ .await
+ .unwrap());
+ assert_eq!(prune(docs.as_ref(), now).await.unwrap(), 0);
+ assert_eq!(
+ prune(docs.as_ref(), now + Duration::days(2)).await.unwrap(),
+ 2
+ );
+}
diff --git a/crates/openhuman-core/src/cron/host_delivery.rs b/crates/openhuman-core/src/cron/host_delivery.rs
index 8b909cbedd..c5c5992043 100644
--- a/crates/openhuman-core/src/cron/host_delivery.rs
+++ b/crates/openhuman-core/src/cron/host_delivery.rs
@@ -47,12 +47,17 @@ static SINK: RwLock<Option<Arc<dyn CronDelivery>>> = RwLock::new(None);
/// Install the process's host delivery, returning the one it replaces.
pub fn install(sink: Arc<dyn CronDelivery>) -> Option<Arc<dyn CronDelivery>> {
- SINK.write().unwrap_or_else(|e| e.into_inner()).replace(sink)
+ SINK.write()
+ .unwrap_or_else(|e| e.into_inner())
+ .replace(sink)
}
/// Remove the installed delivery. Returns whether there was one.
pub fn clear() -> bool {
- SINK.write().unwrap_or_else(|e| e.into_inner()).take().is_some()
+ SINK.write()
+ .unwrap_or_else(|e| e.into_inner())
+ .take()
+ .is_some()
}
/// The installed delivery, if any.
diff --git a/crates/openhuman-core/src/profiles/cron.rs b/crates/openhuman-core/src/profiles/cron.rs
index 58e7b07a2b..ea2e07ae88 100644
--- a/crates/openhuman-core/src/profiles/cron.rs
+++ b/crates/openhuman-core/src/profiles/cron.rs
@@ -104,7 +104,10 @@ pub async fn sweep(host: &ProfileHost, now: DateTime<Utc>) -> SweepReport {
Ok(true) => claimed.push(job),
Ok(false) => report.skipped += 1,
Err(error) => {
- log::warn!("[profiles][cron] profile={id} job={} claim: {error}", job.id);
+ log::warn!(
+ "[profiles][cron] profile={id} job={} claim: {error}",
+ job.id
+ );
report.skipped += 1;
}
}
diff --git a/crates/openhuman-core/src/profiles/cron_tests.rs b/crates/openhuman-core/src/profiles/cron_tests.rs
new file mode 100644
index 0000000000..0b450f2028
--- /dev/null
+++ b/crates/openhuman-core/src/profiles/cron_tests.rs
@@ -0,0 +1,12 @@
+use super::*;
+use crate::core::runtime::{CoreContext, DomainSet, SaasConfig};
+
+#[tokio::test]
+async fn a_host_without_a_shared_backend_sweeps_nothing() {
+ let tmp = tempfile::tempdir().unwrap();
+ let host = ProfileHost::new(
+ SaasConfig::new(tmp.path()),
+ CoreContext::for_test(DomainSet::full(), None),
+ );
+ assert_eq!(sweep(&host, Utc::now()).await, SweepReport::default());
+}