Skip to main content

wfe_core/traits/
persistence.rs

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