1pub mod ctx;
2pub mod error;
3pub mod executor;
4
5pub use ctx::Ctx;
6pub use ctx::RetryPolicy;
7pub use ctx::{TaskQuery, TaskSort, TaskSummary};
8pub use durable_db::entity::sea_orm_active_enums::TaskStatus;
9pub use durable_macros::{step, workflow};
10pub use error::DurableError;
11pub use executor::{Executor, HeartbeatConfig, RecoveredTask};
12pub use sea_orm::DatabaseTransaction;
13
14pub use inventory;
16
17use sea_orm::{ConnectOptions, Database, DatabaseConnection};
18use sea_orm_migration::MigratorTrait;
19use std::future::Future;
20use std::pin::Pin;
21use std::sync::RwLock;
22
23static EXECUTOR_ID: RwLock<Option<String>> = RwLock::new(None);
26
27pub fn executor_id() -> Option<String> {
29 EXECUTOR_ID.read().ok().and_then(|g| g.clone())
30}
31
32pub struct WorkflowRegistration {
39 pub name: &'static str,
41 pub resume_fn: fn(Ctx) -> Pin<Box<dyn Future<Output = Result<(), DurableError>> + Send>>,
44}
45
46inventory::collect!(WorkflowRegistration);
47
48pub fn find_workflow(name: &str) -> Option<&'static WorkflowRegistration> {
50 inventory::iter::<WorkflowRegistration>().find(|r| r.name == name)
51}
52
53pub async fn init(
70 database_url: &str,
71) -> Result<(DatabaseConnection, Vec<RecoveredTask>), DurableError> {
72 init_with_config(database_url, HeartbeatConfig::default()).await
73}
74
75pub async fn init_with_config(
77 database_url: &str,
78 config: HeartbeatConfig,
79) -> Result<(DatabaseConnection, Vec<RecoveredTask>), DurableError> {
80 let mut opt = ConnectOptions::new(database_url);
81 opt.set_schema_search_path("public,durable");
82 let db = Database::connect(opt).await?;
83
84 durable_db::Migrator::up(&db, None).await?;
86
87 let eid = format!("exec-{}-{}", std::process::id(), uuid::Uuid::new_v4());
89 if let Ok(mut guard) = EXECUTOR_ID.write() {
90 *guard = Some(eid.clone());
91 }
92 let executor = Executor::new(db.clone(), eid);
93
94 executor.heartbeat().await?;
96
97 let mut all_recovered = Vec::new();
98
99 let recovered = executor.recover().await?;
101 if !recovered.is_empty() {
102 tracing::info!(
103 "recovered {} stale tasks (timeout/deadline)",
104 recovered.len()
105 );
106 }
107 all_recovered.extend(recovered);
108
109 let recovered = executor
111 .recover_stale_tasks(config.staleness_threshold)
112 .await?;
113 if !recovered.is_empty() {
114 tracing::info!(
115 "recovered {} stale tasks from dead/unknown workers",
116 recovered.len()
117 );
118 }
119 all_recovered.extend(recovered);
120
121 dispatch_recovered(&db, &all_recovered);
123
124 executor.start_heartbeat(&config);
126
127 start_recovery_dispatch_loop(
129 db.clone(),
130 executor.executor_id().to_string(),
131 config.staleness_threshold,
132 );
133
134 tracing::info!("durable initialized (executor={})", executor.executor_id());
135 Ok((db, all_recovered))
136}
137
138fn dispatch_recovered(db: &DatabaseConnection, recovered: &[RecoveredTask]) {
141 for task in recovered {
142 if task.parent_id.is_some() {
144 continue;
145 }
146
147 if let Some(reg) = find_workflow(&task.name) {
148 let db_inner = db.clone();
149 let task_id = task.id;
150 let task_name = task.name.clone();
151 let resume = reg.resume_fn;
152 tokio::spawn(async move {
153 tracing::info!(
154 workflow = %task_name,
155 id = %task_id,
156 "auto-resuming recovered workflow"
157 );
158 match Ctx::from_id(&db_inner, task_id).await {
159 Ok(ctx) => {
160 if let Err(e) = (resume)(ctx).await {
161 tracing::error!(
162 workflow = %task_name,
163 id = %task_id,
164 error = %e,
165 "recovered workflow failed"
166 );
167 }
168 }
169 Err(e) => {
170 tracing::error!(
171 workflow = %task_name,
172 id = %task_id,
173 error = %e,
174 "failed to attach to recovered workflow"
175 );
176 }
177 }
178 });
179 } else {
180 tracing::warn!(
181 workflow = %task.name,
182 id = %task.id,
183 "no registered handler for recovered task — use Ctx::from_id() to resume manually"
184 );
185 }
186 }
187}
188
189fn start_recovery_dispatch_loop(
191 db: DatabaseConnection,
192 executor_id: String,
193 staleness_threshold: std::time::Duration,
194) {
195 tokio::spawn(async move {
196 let executor = Executor::new(db.clone(), executor_id);
197 let mut ticker = tokio::time::interval(staleness_threshold);
198 loop {
199 ticker.tick().await;
200
201 match executor.recover().await {
203 Ok(ref recovered) if !recovered.is_empty() => {
204 tracing::info!(
205 "recovered {} stale tasks (timeout/deadline)",
206 recovered.len()
207 );
208 dispatch_recovered(&db, recovered);
209 }
210 Err(e) => tracing::warn!("timeout recovery failed: {e}"),
211 _ => {}
212 }
213
214 match executor.recover_stale_tasks(staleness_threshold).await {
216 Ok(ref recovered) if !recovered.is_empty() => {
217 tracing::info!(
218 "recovered {} stale tasks from dead workers",
219 recovered.len()
220 );
221 dispatch_recovered(&db, recovered);
222 }
223 Err(e) => tracing::warn!("heartbeat recovery failed: {e}"),
224 _ => {}
225 }
226 }
227 });
228}
229
230pub async fn init_db(database_url: &str) -> Result<DatabaseConnection, DurableError> {
240 let mut opt = ConnectOptions::new(database_url);
241 opt.set_schema_search_path("public,durable");
242 let db = Database::connect(opt).await?;
243 durable_db::Migrator::up(&db, None).await?;
244 tracing::info!("durable initialized (db only)");
245 Ok(db)
246}