pub mod eta;
pub mod executor;
pub mod queue;
pub mod types;
pub use queue::BatchQueue;
pub use types::{
BatchCompletionSummary, BatchItem, BatchItemStatus, BatchJob, BatchJobStatus, EtaConfidence,
EtaEstimate, ItemResult, OverwritePolicy, SchedulingConfig, SizeBucket,
};
pub trait BatchItemHandler<D>: Send + Sync + 'static
where
D: Clone + Send + Sync + serde::Serialize,
{
fn process(
&self,
data: &D,
resource_key: &str,
operation: &str,
) -> impl std::future::Future<Output = anyhow::Result<ItemResult>> + Send;
fn should_skip(&self, _data: &D, _operation: &str) -> bool {
false
}
}
pub trait BatchStore<D>: Send + Sync
where
D: Clone + Send + Sync + serde::Serialize + serde::de::DeserializeOwned + 'static,
{
fn save_job(&self, job: &BatchJob<D>) -> anyhow::Result<()>;
fn load_all(&self) -> anyhow::Result<Vec<BatchJob<D>>>;
fn delete_job(&self, job_id: &str) -> anyhow::Result<()>;
}
pub fn build_job<D>(
resource_key: &str,
operation: &str,
overwrite_policy: OverwritePolicy,
items: Vec<(String, D, SizeBucket)>,
) -> BatchJob<D>
where
D: Clone + Send + Sync + serde::Serialize,
{
let batch_items = items
.into_iter()
.map(|(id, data, bucket)| BatchItem {
id,
data,
status: BatchItemStatus::Pending,
error: None,
duration_ms: None,
size_bucket: bucket,
trace_ctx: None,
attempt_id: None,
trial_id: None,
})
.collect();
BatchJob {
id: String::new(),
resource_key: resource_key.to_string(),
operation: operation.to_string(),
overwrite_policy,
items: batch_items,
status: BatchJobStatus::Queued,
created_at: String::new(),
started_at: None,
completed_at: None,
reordered: false,
reorder_note: None,
}
}
pub fn build_job_traced<D>(
resource_key: &str,
operation: &str,
overwrite_policy: OverwritePolicy,
items: Vec<(String, D, SizeBucket)>,
trace_ctx: stack_ids::TraceCtx,
) -> BatchJob<D>
where
D: Clone + Send + Sync + serde::Serialize,
{
let batch_items = items
.into_iter()
.map(|(id, data, bucket)| BatchItem {
id,
data,
status: BatchItemStatus::Pending,
error: None,
duration_ms: None,
size_bucket: bucket,
trace_ctx: Some(trace_ctx.clone()),
attempt_id: Some(stack_ids::AttemptId::generate()),
trial_id: None,
})
.collect();
BatchJob {
id: String::new(),
resource_key: resource_key.to_string(),
operation: operation.to_string(),
overwrite_policy,
items: batch_items,
status: BatchJobStatus::Queued,
created_at: String::new(),
started_at: None,
completed_at: None,
reordered: false,
reorder_note: None,
}
}