Skip to main content

wfe_core/traits/
persistence.rs

1use async_trait::async_trait;
2use chrono::{DateTime, Utc};
3
4use crate::models::{
5    Event, EventSubscription, ExecutionError, ScheduledCommand, WorkflowInstance,
6};
7
8/// Persistence for workflow instances.
9#[async_trait]
10pub trait WorkflowRepository: Send + Sync {
11    async fn create_new_workflow(&self, instance: &WorkflowInstance) -> crate::Result<String>;
12    async fn persist_workflow(&self, instance: &WorkflowInstance) -> crate::Result<()>;
13    async fn persist_workflow_with_subscriptions(
14        &self,
15        instance: &WorkflowInstance,
16        subscriptions: &[EventSubscription],
17    ) -> crate::Result<()>;
18    async fn get_runnable_instances(&self, as_at: DateTime<Utc>) -> crate::Result<Vec<String>>;
19    async fn get_workflow_instance(&self, id: &str) -> crate::Result<WorkflowInstance>;
20    async fn get_workflow_instances(&self, ids: &[String]) -> crate::Result<Vec<WorkflowInstance>>;
21}
22
23/// Persistence for event subscriptions.
24#[async_trait]
25pub trait SubscriptionRepository: Send + Sync {
26    async fn create_event_subscription(
27        &self,
28        subscription: &EventSubscription,
29    ) -> crate::Result<String>;
30    async fn get_subscriptions(
31        &self,
32        event_name: &str,
33        event_key: &str,
34        as_of: DateTime<Utc>,
35    ) -> crate::Result<Vec<EventSubscription>>;
36    async fn terminate_subscription(&self, subscription_id: &str) -> crate::Result<()>;
37    async fn get_subscription(&self, subscription_id: &str) -> crate::Result<EventSubscription>;
38    async fn get_first_open_subscription(
39        &self,
40        event_name: &str,
41        event_key: &str,
42        as_of: DateTime<Utc>,
43    ) -> crate::Result<Option<EventSubscription>>;
44    async fn set_subscription_token(
45        &self,
46        subscription_id: &str,
47        token: &str,
48        worker_id: &str,
49        expiry: DateTime<Utc>,
50    ) -> crate::Result<bool>;
51    async fn clear_subscription_token(
52        &self,
53        subscription_id: &str,
54        token: &str,
55    ) -> crate::Result<()>;
56}
57
58/// Persistence for events.
59#[async_trait]
60pub trait EventRepository: Send + Sync {
61    async fn create_event(&self, event: &Event) -> crate::Result<String>;
62    async fn get_event(&self, id: &str) -> crate::Result<Event>;
63    async fn get_runnable_events(&self, as_at: DateTime<Utc>) -> crate::Result<Vec<String>>;
64    async fn get_events(
65        &self,
66        event_name: &str,
67        event_key: &str,
68        as_of: DateTime<Utc>,
69    ) -> crate::Result<Vec<String>>;
70    async fn mark_event_processed(&self, id: &str) -> crate::Result<()>;
71    async fn mark_event_unprocessed(&self, id: &str) -> crate::Result<()>;
72}
73
74/// Persistence for scheduled commands.
75#[async_trait]
76pub trait ScheduledCommandRepository: Send + Sync {
77    fn supports_scheduled_commands(&self) -> bool;
78    async fn schedule_command(&self, command: &ScheduledCommand) -> crate::Result<()>;
79    async fn process_commands(
80        &self,
81        as_of: DateTime<Utc>,
82        handler: &(dyn Fn(ScheduledCommand) -> std::pin::Pin<Box<dyn std::future::Future<Output = crate::Result<()>> + Send>>
83              + Send
84              + Sync),
85    ) -> crate::Result<()>;
86}
87
88/// Composite persistence provider combining all repository traits.
89#[async_trait]
90pub trait PersistenceProvider:
91    WorkflowRepository + EventRepository + SubscriptionRepository + ScheduledCommandRepository
92{
93    async fn persist_errors(&self, errors: &[ExecutionError]) -> crate::Result<()>;
94    async fn ensure_store_exists(&self) -> crate::Result<()>;
95}