use aion_core::{
Event, RunId, TimerId, WorkflowFilter, WorkflowId, WorkflowStatus, WorkflowSummary,
};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use crate::{StoreError, TimerEntry};
mod write_capability {
#[derive(Clone, Copy, Debug)]
pub struct WriteToken {
_private: (),
}
impl WriteToken {
#[must_use]
pub fn recorder() -> Self {
Self { _private: () }
}
}
pub(crate) fn conformance() -> WriteToken {
WriteToken { _private: () }
}
}
pub use write_capability::WriteToken;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RunSummary {
pub run_id: RunId,
pub parent_run_id: Option<RunId>,
pub status: WorkflowStatus,
pub started_at: DateTime<Utc>,
pub closed_at: Option<DateTime<Utc>>,
}
#[async_trait]
pub trait ReadableEventStore: Send + Sync + 'static {
async fn read_history(&self, workflow_id: &WorkflowId) -> Result<Vec<Event>, StoreError>;
async fn read_history_from(
&self,
workflow_id: &WorkflowId,
from_seq: u64,
) -> Result<Vec<Event>, StoreError>;
async fn read_run_chain(&self, workflow_id: &WorkflowId)
-> Result<Vec<RunSummary>, StoreError>;
async fn list_workflow_ids(&self) -> Result<Vec<WorkflowId>, StoreError>;
async fn list_active(&self) -> Result<Vec<WorkflowId>, StoreError>;
async fn query(&self, filter: &WorkflowFilter) -> Result<Vec<WorkflowSummary>, StoreError>;
async fn schedule_timer(
&self,
workflow_id: &WorkflowId,
timer_id: &TimerId,
fire_at: DateTime<Utc>,
) -> Result<(), StoreError>;
async fn expired_timers(&self, as_of: DateTime<Utc>) -> Result<Vec<TimerEntry>, StoreError>;
}
#[async_trait]
pub trait WritableEventStore: Send + Sync + 'static {
async fn append(
&self,
token: WriteToken,
workflow_id: &WorkflowId,
events: &[Event],
expected_seq: u64,
) -> Result<(), StoreError>;
}
pub trait EventStore: ReadableEventStore + WritableEventStore {}
impl<T> EventStore for T where T: ReadableEventStore + WritableEventStore + ?Sized {}
pub(crate) fn conformance_write_token() -> WriteToken {
write_capability::conformance()
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use super::{EventStore, ReadableEventStore, WritableEventStore};
#[test]
fn event_store_traits_are_object_safe() {
let _: Option<Arc<dyn ReadableEventStore>> = None;
let _: Option<Arc<dyn WritableEventStore>> = None;
let _: Option<Arc<dyn EventStore>> = None;
}
}