cloacina 0.6.1

A Rust library for resilient task execution and orchestration.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/*
 *  Copyright 2025-2026 Colliery Software
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

//! Unified Schedule Execution DAL with runtime backend selection
//!
//! This module provides operations for the unified `schedule_executions` table
//! that replaces the separate `cron_executions` and `trigger_executions` tables.
//! Works with both PostgreSQL and SQLite backends, selecting the appropriate
//! implementation at runtime based on the database connection type.

mod crud;

use super::DAL;
use crate::database::universal_types::UniversalUuid;
use crate::error::ValidationError;
use crate::models::schedule::{NewScheduleExecution, ScheduleExecution};
use chrono::{DateTime, Utc};

/// Statistics about schedule execution performance
#[derive(Debug)]
pub struct ScheduleExecutionStats {
    /// Total number of executions attempted
    pub total_executions: i64,
    /// Number of executions that successfully handed off to workflow executor
    pub successful_executions: i64,
    /// Number of executions that were lost (started but never completed within expected time)
    pub lost_executions: i64,
    /// Success rate as a percentage
    pub success_rate: f64,
}

/// Data access layer for unified schedule execution operations with runtime backend selection.
#[derive(Clone)]
pub struct ScheduleExecutionDAL<'a> {
    dal: &'a DAL,
}

impl<'a> ScheduleExecutionDAL<'a> {
    /// Creates a new ScheduleExecutionDAL instance.
    pub fn new(dal: &'a DAL) -> Self {
        Self { dal }
    }

    /// Creates a new schedule execution record in the database.
    pub async fn create(
        &self,
        new_execution: NewScheduleExecution,
    ) -> Result<ScheduleExecution, ValidationError> {
        crate::dispatch_backend!(
            self.dal.backend(),
            self.create_postgres(new_execution).await,
            self.create_sqlite(new_execution).await
        )
    }

    /// Retrieves a schedule execution by its ID.
    pub async fn get_by_id(&self, id: UniversalUuid) -> Result<ScheduleExecution, ValidationError> {
        crate::dispatch_backend!(
            self.dal.backend(),
            self.get_by_id_postgres(id).await,
            self.get_by_id_sqlite(id).await
        )
    }

    /// Lists schedule executions for a given schedule.
    pub async fn list_by_schedule(
        &self,
        schedule_id: UniversalUuid,
        limit: i64,
        offset: i64,
    ) -> Result<Vec<ScheduleExecution>, ValidationError> {
        crate::dispatch_backend!(
            self.dal.backend(),
            self.list_by_schedule_postgres(schedule_id, limit, offset)
                .await,
            self.list_by_schedule_sqlite(schedule_id, limit, offset)
                .await
        )
    }

    /// Marks a schedule execution as completed.
    pub async fn complete(
        &self,
        id: UniversalUuid,
        completed_at: DateTime<Utc>,
    ) -> Result<(), ValidationError> {
        crate::dispatch_backend!(
            self.dal.backend(),
            self.complete_postgres(id, completed_at).await,
            self.complete_sqlite(id, completed_at).await
        )
    }

    /// Checks if there is an active (uncompleted) execution for a schedule with the given context hash.
    pub async fn has_active_execution(
        &self,
        schedule_id: UniversalUuid,
        context_hash: &str,
    ) -> Result<bool, ValidationError> {
        let context_hash_owned = context_hash.to_string();
        crate::dispatch_backend!(
            self.dal.backend(),
            self.has_active_execution_postgres(schedule_id, context_hash_owned.clone())
                .await,
            self.has_active_execution_sqlite(schedule_id, context_hash_owned)
                .await
        )
    }

    /// Updates the workflow execution ID for a schedule execution.
    pub async fn update_workflow_execution_id(
        &self,
        id: UniversalUuid,
        workflow_execution_id: UniversalUuid,
    ) -> Result<(), ValidationError> {
        crate::dispatch_backend!(
            self.dal.backend(),
            self.update_workflow_execution_id_postgres(id, workflow_execution_id)
                .await,
            self.update_workflow_execution_id_sqlite(id, workflow_execution_id)
                .await
        )
    }

    /// Finds lost executions (started but not completed) older than the specified minutes.
    pub async fn find_lost_executions(
        &self,
        older_than_minutes: i32,
    ) -> Result<Vec<ScheduleExecution>, ValidationError> {
        crate::dispatch_backend!(
            self.dal.backend(),
            self.find_lost_executions_postgres(older_than_minutes).await,
            self.find_lost_executions_sqlite(older_than_minutes).await
        )
    }

    /// Gets the latest execution for a given schedule.
    pub async fn get_latest_by_schedule(
        &self,
        schedule_id: UniversalUuid,
    ) -> Result<Option<ScheduleExecution>, ValidationError> {
        crate::dispatch_backend!(
            self.dal.backend(),
            self.get_latest_by_schedule_postgres(schedule_id).await,
            self.get_latest_by_schedule_sqlite(schedule_id).await
        )
    }

    /// Gets execution statistics for monitoring and alerting.
    pub async fn get_execution_stats(
        &self,
        since: DateTime<Utc>,
    ) -> Result<ScheduleExecutionStats, ValidationError> {
        crate::dispatch_backend!(
            self.dal.backend(),
            self.get_execution_stats_postgres(since).await,
            self.get_execution_stats_sqlite(since).await
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::database::universal_types::UniversalTimestamp;
    use crate::database::Database;
    use crate::models::schedule::{NewSchedule, NewScheduleExecution};

    #[cfg(feature = "sqlite")]
    async fn unique_dal() -> DAL {
        let url = format!(
            "file:sched_exec_test_{}?mode=memory&cache=shared",
            uuid::Uuid::new_v4()
        );
        let db = Database::new(&url, "", 5);
        db.run_migrations()
            .await
            .expect("migrations should succeed");
        DAL::new(db)
    }

    /// Helper: create a cron schedule and return its ID.
    #[cfg(feature = "sqlite")]
    async fn create_schedule(dal: &DAL) -> UniversalUuid {
        let next_run = UniversalTimestamp::now();
        let schedule = dal
            .schedule()
            .create(NewSchedule::cron("test_wf", "0 * * * *", next_run))
            .await
            .unwrap();
        schedule.id
    }

    /// Helper: build a NewScheduleExecution for a given schedule.
    #[cfg(feature = "sqlite")]
    fn new_exec(schedule_id: UniversalUuid) -> NewScheduleExecution {
        NewScheduleExecution {
            schedule_id,
            workflow_execution_id: None,
            scheduled_time: Some(UniversalTimestamp::now()),
            claimed_at: Some(UniversalTimestamp::now()),
            context_hash: Some(uuid::Uuid::new_v4().to_string()),
        }
    }

    // ── create + get_by_id ──────────────────────────────────────────

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_create_execution() {
        let dal = unique_dal().await;
        let sched_id = create_schedule(&dal).await;

        let exec = dal
            .schedule_execution()
            .create(new_exec(sched_id))
            .await
            .unwrap();

        assert_eq!(exec.schedule_id, sched_id);
        assert!(exec.workflow_execution_id.is_none());
        assert!(exec.completed_at.is_none());
        assert!(exec.scheduled_time.is_some());
        assert!(exec.context_hash.is_some());
    }

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_get_by_id() {
        let dal = unique_dal().await;
        let sched_id = create_schedule(&dal).await;
        let created = dal
            .schedule_execution()
            .create(new_exec(sched_id))
            .await
            .unwrap();

        let fetched = dal
            .schedule_execution()
            .get_by_id(created.id)
            .await
            .unwrap();
        assert_eq!(fetched.id, created.id);
        assert_eq!(fetched.schedule_id, sched_id);
    }

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_get_by_id_not_found() {
        let dal = unique_dal().await;
        let result = dal
            .schedule_execution()
            .get_by_id(UniversalUuid::new_v4())
            .await;
        assert!(result.is_err());
    }

    // ── list_by_schedule ────────────────────────────────────────────

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_list_by_schedule() {
        let dal = unique_dal().await;
        let sched_id = create_schedule(&dal).await;
        let other_sched_id = create_schedule(&dal).await;

        // Create 3 executions for sched_id, 1 for other
        for _ in 0..3 {
            dal.schedule_execution()
                .create(new_exec(sched_id))
                .await
                .unwrap();
        }
        dal.schedule_execution()
            .create(new_exec(other_sched_id))
            .await
            .unwrap();

        let list = dal
            .schedule_execution()
            .list_by_schedule(sched_id, 100, 0)
            .await
            .unwrap();
        assert_eq!(list.len(), 3);

        // With limit
        let limited = dal
            .schedule_execution()
            .list_by_schedule(sched_id, 2, 0)
            .await
            .unwrap();
        assert_eq!(limited.len(), 2);

        // With offset
        let offset = dal
            .schedule_execution()
            .list_by_schedule(sched_id, 100, 2)
            .await
            .unwrap();
        assert_eq!(offset.len(), 1);
    }

    // ── complete ────────────────────────────────────────────────────

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_complete_execution() {
        let dal = unique_dal().await;
        let sched_id = create_schedule(&dal).await;
        let exec = dal
            .schedule_execution()
            .create(new_exec(sched_id))
            .await
            .unwrap();
        assert!(exec.completed_at.is_none());

        let completed_at = Utc::now();
        dal.schedule_execution()
            .complete(exec.id, completed_at)
            .await
            .unwrap();

        let updated = dal.schedule_execution().get_by_id(exec.id).await.unwrap();
        assert!(updated.completed_at.is_some());
    }

    // ── has_active_execution ────────────────────────────────────────

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_has_active_execution() {
        let dal = unique_dal().await;
        let sched_id = create_schedule(&dal).await;

        // No executions yet
        let active = dal
            .schedule_execution()
            .has_active_execution(sched_id, "hash1")
            .await
            .unwrap();
        assert!(!active);

        // Create an uncompleted execution
        let mut ne = new_exec(sched_id);
        ne.context_hash = Some("hash1".to_string());
        dal.schedule_execution().create(ne).await.unwrap();

        let active = dal
            .schedule_execution()
            .has_active_execution(sched_id, "hash1")
            .await
            .unwrap();
        assert!(active);

        // Different hash should not match
        let active_other = dal
            .schedule_execution()
            .has_active_execution(sched_id, "hash_other")
            .await
            .unwrap();
        assert!(!active_other);
    }

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_has_active_execution_completed_not_active() {
        let dal = unique_dal().await;
        let sched_id = create_schedule(&dal).await;

        let mut ne = new_exec(sched_id);
        ne.context_hash = Some("hash_done".to_string());
        let exec = dal.schedule_execution().create(ne).await.unwrap();

        // Complete it
        dal.schedule_execution()
            .complete(exec.id, Utc::now())
            .await
            .unwrap();

        let active = dal
            .schedule_execution()
            .has_active_execution(sched_id, "hash_done")
            .await
            .unwrap();
        assert!(!active);
    }

    // ── update_workflow_execution_id ────────────────────────────────

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_update_workflow_execution_id() {
        let dal = unique_dal().await;
        let sched_id = create_schedule(&dal).await;
        let exec = dal
            .schedule_execution()
            .create(new_exec(sched_id))
            .await
            .unwrap();
        assert!(exec.workflow_execution_id.is_none());

        // Create a real workflow execution so the FK constraint is satisfied
        use crate::models::workflow_execution::NewWorkflowExecution;
        let wf_exec = dal
            .workflow_execution()
            .create(NewWorkflowExecution {
                workflow_name: "fk-test".to_string(),
                workflow_version: "1.0".to_string(),
                status: "Running".to_string(),
                context_id: None,
            })
            .await
            .unwrap();

        dal.schedule_execution()
            .update_workflow_execution_id(exec.id, wf_exec.id)
            .await
            .unwrap();

        let updated = dal.schedule_execution().get_by_id(exec.id).await.unwrap();
        assert_eq!(updated.workflow_execution_id, Some(wf_exec.id));
    }

    // ── get_latest_by_schedule ──────────────────────────────────────

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_get_latest_by_schedule() {
        let dal = unique_dal().await;
        let sched_id = create_schedule(&dal).await;

        // No executions => None
        let latest = dal
            .schedule_execution()
            .get_latest_by_schedule(sched_id)
            .await
            .unwrap();
        assert!(latest.is_none());

        // Create two executions; the second is "latest" by created_at
        let _first = dal
            .schedule_execution()
            .create(new_exec(sched_id))
            .await
            .unwrap();
        let second = dal
            .schedule_execution()
            .create(new_exec(sched_id))
            .await
            .unwrap();

        let latest = dal
            .schedule_execution()
            .get_latest_by_schedule(sched_id)
            .await
            .unwrap();
        assert!(latest.is_some());
        assert_eq!(latest.unwrap().id, second.id);
    }

    // ── find_lost_executions ────────────────────────────────────────

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_find_lost_executions_none_lost() {
        let dal = unique_dal().await;
        let sched_id = create_schedule(&dal).await;

        // Create a fresh (just-started) execution — not lost yet
        dal.schedule_execution()
            .create(new_exec(sched_id))
            .await
            .unwrap();

        // Looking for executions older than 60 minutes — our fresh one should not appear
        let lost = dal
            .schedule_execution()
            .find_lost_executions(60)
            .await
            .unwrap();
        assert!(lost.is_empty());
    }

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_find_lost_executions_completed_not_lost() {
        let dal = unique_dal().await;
        let sched_id = create_schedule(&dal).await;

        let exec = dal
            .schedule_execution()
            .create(new_exec(sched_id))
            .await
            .unwrap();

        // Complete it so it should never be considered "lost"
        dal.schedule_execution()
            .complete(exec.id, Utc::now())
            .await
            .unwrap();

        let lost = dal
            .schedule_execution()
            .find_lost_executions(0)
            .await
            .unwrap();
        assert!(lost.is_empty());
    }

    // ── get_execution_stats ─────────────────────────────────────────

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_get_execution_stats_empty() {
        let dal = unique_dal().await;
        let since = Utc::now() - chrono::Duration::hours(1);

        let stats = dal
            .schedule_execution()
            .get_execution_stats(since)
            .await
            .unwrap();

        assert_eq!(stats.total_executions, 0);
        assert_eq!(stats.successful_executions, 0);
        assert_eq!(stats.lost_executions, 0);
        assert_eq!(stats.success_rate, 0.0);
    }

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_get_execution_stats_with_data() {
        let dal = unique_dal().await;
        let sched_id = create_schedule(&dal).await;
        let since = Utc::now() - chrono::Duration::hours(1);

        // Create two executions
        let exec1 = dal
            .schedule_execution()
            .create(new_exec(sched_id))
            .await
            .unwrap();
        dal.schedule_execution()
            .create(new_exec(sched_id))
            .await
            .unwrap();

        // Link one to a real workflow execution (FK constraint requires it to exist)
        use crate::models::workflow_execution::NewWorkflowExecution;
        let wf_exec = dal
            .workflow_execution()
            .create(NewWorkflowExecution {
                workflow_name: "stats-test".to_string(),
                workflow_version: "1.0".to_string(),
                status: "Completed".to_string(),
                context_id: None,
            })
            .await
            .unwrap();
        dal.schedule_execution()
            .update_workflow_execution_id(exec1.id, wf_exec.id)
            .await
            .unwrap();

        let stats = dal
            .schedule_execution()
            .get_execution_stats(since)
            .await
            .unwrap();

        assert_eq!(stats.total_executions, 2);
        assert_eq!(stats.successful_executions, 1);
        assert_eq!(stats.success_rate, 50.0);
    }
}