use std::future::Future;
use std::pin::Pin;
use chrono::{DateTime, Utc};
use uuid::Uuid;
use crate::api_key_store::ApiKeyStore;
use crate::artifact_store::ArtifactStore;
use crate::audit_log_store::AuditLogStore;
use crate::entities::{
LeaseRequest, NewRun, NewStep, NewStepDependency, Page, PurgePolicy, PurgeableRun, ReapedRun,
Run, RunCreation, RunFilter, RunStats, RunStatus, RunUpdate, Step, StepDependency, StepUpdate,
};
use crate::error::StoreError;
use crate::log_store::LogStore;
use crate::secret_store::SecretStore;
use crate::user_store::UserStore;
pub type StoreFuture<'a, T> = Pin<Box<dyn Future<Output = Result<T, StoreError>> + Send + 'a>>;
pub const LEASE_EXPIRED_ERROR: &str = "worker lease expired";
pub trait RunStore: Send + Sync {
fn create_run(&self, req: NewRun) -> StoreFuture<'_, RunCreation>;
fn find_run_by_idempotency_key(&self, key: &str) -> StoreFuture<'_, Option<Run>>;
fn get_run(&self, id: Uuid) -> StoreFuture<'_, Option<Run>>;
fn list_runs(&self, filter: RunFilter, page: u32, per_page: u32) -> StoreFuture<'_, Page<Run>>;
fn update_run_status(&self, id: Uuid, new_status: RunStatus) -> StoreFuture<'_, ()>;
fn update_run(&self, id: Uuid, update: RunUpdate) -> StoreFuture<'_, ()>;
fn pick_next_pending(&self, lease: Option<LeaseRequest>) -> StoreFuture<'_, Option<Run>>;
fn renew_lease(&self, id: Uuid, lease: LeaseRequest) -> StoreFuture<'_, DateTime<Utc>>;
fn reap_expired_leases(&self, limit: u32) -> StoreFuture<'_, Vec<ReapedRun>>;
fn create_step(&self, step: NewStep) -> StoreFuture<'_, Step>;
fn update_step(&self, id: Uuid, update: StepUpdate) -> StoreFuture<'_, ()>;
fn get_step(&self, id: Uuid) -> StoreFuture<'_, Option<Step>>;
fn list_steps(&self, run_id: Uuid) -> StoreFuture<'_, Vec<Step>>;
fn get_stats(&self, filter: RunFilter) -> StoreFuture<'_, RunStats>;
fn create_step_dependencies(&self, deps: Vec<NewStepDependency>) -> StoreFuture<'_, ()>;
fn list_step_dependencies(&self, run_id: Uuid) -> StoreFuture<'_, Vec<StepDependency>>;
fn list_purgeable_runs(
&self,
policy: &PurgePolicy,
batch_size: u32,
) -> StoreFuture<'_, Vec<PurgeableRun>>;
fn delete_run(&self, id: Uuid) -> StoreFuture<'_, Vec<String>>;
fn update_run_returning(&self, id: Uuid, update: RunUpdate) -> StoreFuture<'_, Run> {
Box::pin(async move {
self.update_run(id, update).await?;
self.get_run(id).await?.ok_or(StoreError::RunNotFound(id))
})
}
}
pub trait Store:
RunStore + UserStore + ApiKeyStore + SecretStore + AuditLogStore + ArtifactStore + LogStore
{
}
impl<T: RunStore + UserStore + ApiKeyStore + SecretStore + AuditLogStore + ArtifactStore + LogStore>
Store for T
{
}