Skip to content

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

feat(cron): add documents port for cron jobs

Steven Enamakele53fb226879b
diff --git a/crates/openhuman-core/src/cron/documents_port.rs b/crates/openhuman-core/src/cron/documents_port.rs
index a6d0ca3217..6f9ca7e4e5 100644
--- a/crates/openhuman-core/src/cron/documents_port.rs
+++ b/crates/openhuman-core/src/cron/documents_port.rs
@@ -28,8 +28,8 @@ use serde_json::{json, Value};
use tinybus::Result as BusResult;
use tinyflows_bus::schedule::CallbackAddress;
use tinystoragedrivers::{
- Capability, CollectionSpec, DocumentStore, Filter, Precondition, Query, Scope, StorageError,
- WriteOp,
+ Capability, CollectionSpec, DocumentStore, Filter, Precondition, Query, Scope, StorageBackend,
+ StorageError, WriteOp,
};
use crate::config::Config;
@@ -45,10 +45,13 @@ const COLLECTIONS: [&str; 4] = [
/// Where one scope's cron documents live: the workspace whose default backend
/// is used when none is installed, and the storage scope within it.
-#[derive(Clone, Debug)]
+#[derive(Clone)]
pub(crate) struct DocScope {
workspace_dir: PathBuf,
scope: Scope,
+ /// A backend to use instead of resolving the workspace's (the legacy
+ /// importer runs against the backend it was handed).
+ backend: Option<Arc<dyn StorageBackend>>,
}
impl DocScope {
@@ -62,6 +65,7 @@ impl DocScope {
Ok(Self {
workspace_dir: config.workspace_dir.clone(),
scope: opened.scope,
+ backend: None,
})
}
@@ -71,16 +75,41 @@ impl DocScope {
Self {
workspace_dir: config.workspace_dir.clone(),
scope,
+ backend: None,
+ }
+ }
+
+ /// `scope` of an explicit `backend`.
+ pub(crate) fn on_backend(
+ config: &Config,
+ backend: Arc<dyn StorageBackend>,
+ scope: Scope,
+ ) -> Self {
+ Self {
+ workspace_dir: config.workspace_dir.clone(),
+ scope,
+ backend: Some(backend),
}
}
fn key(&self) -> String {
- format!("{}|{}", self.workspace_dir.display(), self.scope.as_str())
+ let backend = self
+ .backend
+ .as_ref()
+ .map_or(0, |backend| Arc::as_ptr(backend).cast::<()>() as usize);
+ format!(
+ "{}|{}|{backend:x}",
+ self.workspace_dir.display(),
+ self.scope.as_str()
+ )
}
fn documents(&self) -> Result<Arc<dyn DocumentStore>, StorageError> {
- let backend = crate::storage::local::backend_for(&self.workspace_dir)
- .map_err(|error| StorageError::unavailable(error.to_string()))?;
+ let backend = match &self.backend {
+ Some(backend) => Arc::clone(backend),
+ None => crate::storage::local::backend_for(&self.workspace_dir)
+ .map_err(|error| StorageError::unavailable(error.to_string()))?,
+ };
let scoped = backend.for_scope(&self.scope)?;
Ok(Arc::clone(scoped.documents()))
}
diff --git a/crates/openhuman-core/src/storage/import/tinyflows.rs b/crates/openhuman-core/src/storage/import/tinyflows.rs
index 83cef4d68c..351c1404c4 100644
--- a/crates/openhuman-core/src/storage/import/tinyflows.rs
+++ b/crates/openhuman-core/src/storage/import/tinyflows.rs
@@ -203,7 +203,9 @@ pub fn import_cron(
scope_dir: &Path,
label: &str,
) -> Result<Outcome> {
- use tinyflows_drivers::schedule::CronDocuments;
+ use crate::cron::client::store_in;
+ use crate::cron::documents_port::DocScope;
+ use tinyflows_bus::schedule::{ImportedJob, StoreOperation};
let domain = if label.is_empty() {
CRON_DOMAIN.to_string()
@@ -215,16 +217,48 @@ pub fn import_cron(
if !jobs_db.is_file() {
return Ok(None);
}
- let scoped = backend
- .for_scope(scope)
- .map_err(|error| anyhow!("open the storage scope: {error}"))?;
- let cron = CronDocuments::new(Arc::clone(scoped.documents()));
+ // The legacy file is decoded here (`tinyflows-import`'s reader, bus
+ // records only) and replayed through the module's `import_job` /
+ // `import_run` store operations, which write only what is absent and
+ // file a flow-schedule job under the id its flow derives.
+ let doc_scope = DocScope::on_backend(config, Arc::clone(backend), scope.clone());
+ let config = config.clone();
let report = crate::storage::block_on_anyhow({
let jobs_db = jobs_db.clone();
async move {
- tinyflows_import::import_cron(&jobs_db, &cron)
+ let legacy = tinyflows_import::read_legacy_cron(&jobs_db)
.await
- .context("import the cron jobs")
+ .context("read the legacy cron database")?;
+ let mut report = legacy.report;
+ let mut filed_as = std::collections::HashMap::new();
+ for job in legacy.jobs {
+ let source_id = job.id.clone();
+ let imported: ImportedJob = store_in(
+ &config,
+ &doc_scope,
+ StoreOperation::ImportJob { job },
+ )
+ .await
+ .context("import a cron job")?;
+ report.wrote("jobs", imported.written);
+ filed_as.insert(source_id, imported.id);
+ }
+ for mut run in legacy.runs {
+ let Some(id) = filed_as.get(&run.job_id) else {
+ report.invalid(
+ "runs",
+ format!("run {}: job '{}' was not imported", run.id, run.job_id),
+ );
+ continue;
+ };
+ run.job_id = id.clone();
+ let written: bool =
+ store_in(&config, &doc_scope, StoreOperation::ImportRun { run })
+ .await
+ .context("import a cron run")?;
+ report.wrote("runs", written);
+ }
+ Ok(report)
}
})?;
Ok(Some(counts(&domain, &report)))