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
/*
 *  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.
 */

//! Integration tests for workflow pause/resume functionality.

use async_trait::async_trait;
use cloacina::executor::workflow_executor::{WorkflowExecution, WorkflowStatus};
use cloacina::executor::WorkflowExecutor;
use cloacina::runner::DefaultRunner;
use cloacina::*;
use serde_json::Value;
use std::sync::Arc;
use std::time::Duration;
use tokio::time;

use crate::fixtures::get_or_init_fixture;

/// Helper to wait for a specific workflow execution status without consuming the execution handle.
/// Useful when you need to keep using the handle after waiting (e.g., to call pause/resume).
async fn wait_for_status(
    execution: &WorkflowExecution,
    target: impl Fn(&WorkflowStatus) -> bool,
    timeout: Duration,
) -> Result<WorkflowStatus, String> {
    let start = std::time::Instant::now();
    loop {
        let status = execution
            .get_status()
            .await
            .map_err(|e| format!("Failed to get status: {}", e))?;
        if target(&status) {
            return Ok(status);
        }
        if start.elapsed() > timeout {
            return Err(format!(
                "Timeout waiting for target status, current status: {:?}",
                status
            ));
        }
        time::sleep(Duration::from_millis(50)).await;
    }
}

/// Wait for the workflow execution to reach a terminal state (Completed, Failed, or Cancelled)
async fn wait_for_terminal(
    execution: &WorkflowExecution,
    timeout: Duration,
) -> Result<WorkflowStatus, String> {
    wait_for_status(execution, |s| s.is_terminal(), timeout).await
}

// Simple task for workflow construction
#[derive(Debug)]
#[allow(dead_code)]
struct WorkflowTask {
    id: String,
    dependencies: Vec<TaskNamespace>,
}

impl WorkflowTask {
    #[allow(dead_code)]
    fn new(id: &str, deps: Vec<&str>) -> Self {
        Self {
            id: id.to_string(),
            dependencies: deps
                .into_iter()
                .map(|s| TaskNamespace::from_string(s).unwrap())
                .collect(),
        }
    }
}

#[async_trait]
impl Task for WorkflowTask {
    async fn execute(
        &self,
        context: Context<serde_json::Value>,
    ) -> Result<Context<serde_json::Value>, TaskError> {
        Ok(context) // No-op for workflow building
    }

    fn id(&self) -> &str {
        &self.id
    }

    fn dependencies(&self) -> &[TaskNamespace] {
        &self.dependencies
    }
}

#[task(
    id = "quick_task",
    dependencies = []
)]
async fn quick_task(context: &mut Context<Value>) -> Result<(), TaskError> {
    context.insert("quick_result", Value::String("done".to_string()))?;
    Ok(())
}

#[task(
    id = "slow_first_task",
    dependencies = []
)]
async fn slow_first_task(context: &mut Context<Value>) -> Result<(), TaskError> {
    // Simulate a slow task that takes a few seconds
    time::sleep(Duration::from_secs(2)).await;
    context.insert("slow_first_result", Value::String("completed".to_string()))?;
    Ok(())
}

#[task(
    id = "slow_second_task",
    dependencies = ["slow_first_task"]
)]
async fn slow_second_task(context: &mut Context<Value>) -> Result<(), TaskError> {
    // Simulate another slow task
    time::sleep(Duration::from_secs(2)).await;
    context.insert("slow_second_result", Value::String("completed".to_string()))?;
    Ok(())
}

#[tokio::test]
async fn test_pause_running_workflow() {
    let fixture = get_or_init_fixture().await;
    let mut fixture = fixture.lock().unwrap_or_else(|e| e.into_inner());

    fixture.reset_database().await;
    fixture.initialize().await;

    let database_url = fixture.get_database_url();
    let database = fixture.get_database();

    // Create a workflow with slow tasks to give us time to pause
    let workflow_name = format!(
        "pause_test_pipeline_{}",
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    );

    let first_ns = TaskNamespace::new("public", "embedded", &workflow_name, "slow_first_task");

    let workflow = Workflow::builder(&workflow_name)
        .description("Test workflow for pause/resume")
        .add_task(Arc::new(slow_first_task_task()))
        .unwrap()
        .add_task(Arc::new(
            slow_second_task_task().with_dependencies(vec![first_ns.clone()]),
        ))
        .unwrap()
        .build()
        .unwrap();

    // Register tasks in a test-scoped runtime.
    let runtime = cloacina::Runtime::empty();
    let namespace1 = TaskNamespace::new(
        workflow.tenant(),
        workflow.package(),
        workflow.name(),
        "slow_first_task",
    );
    runtime.register_task(namespace1, || {
        Arc::new(slow_first_task_task()) as Arc<dyn cloacina::Task>
    });

    let namespace2 = TaskNamespace::new(
        workflow.tenant(),
        workflow.package(),
        workflow.name(),
        "slow_second_task",
    );
    let first_ns_clone = first_ns.clone();
    runtime.register_task(namespace2, move || {
        Arc::new(slow_second_task_task().with_dependencies(vec![first_ns_clone.clone()]))
            as Arc<dyn cloacina::Task>
    });

    // Register workflow
    runtime.register_workflow(workflow.name().to_string(), {
        let workflow = workflow.clone();
        move || workflow.clone()
    });

    // Create runner
    let schema = fixture.get_schema();
    let runner = DefaultRunner::builder()
        .database_url(&database_url)
        .schema(&schema)
        .runtime(runtime)
        .build()
        .await
        .unwrap();

    // Start execution
    let input_context = Context::new();
    let execution = runner
        .execute_async(&workflow_name, input_context)
        .await
        .unwrap();
    let exec_id = execution.execution_id;

    // Wait a moment for scheduler to pick up the workflow execution
    // Note: Workflow executions stay in "Pending" status while tasks execute, so we just wait briefly
    time::sleep(Duration::from_millis(200)).await;

    // Pause the workflow execution (works on both Pending and Running status)
    execution.pause(Some("Test pause")).await.unwrap();

    // Verify the workflow execution is paused
    let status = execution.get_status().await.unwrap();
    assert_eq!(
        status,
        WorkflowStatus::Paused,
        "Workflow execution should be paused"
    );

    // Verify via DAL that pause metadata is set
    let dal = cloacina::dal::DAL::new(database.clone());
    let wf_exec = dal
        .workflow_execution()
        .get_by_id(UniversalUuid(exec_id))
        .await
        .unwrap();
    assert_eq!(wf_exec.status, "Paused");
    assert!(wf_exec.paused_at.is_some(), "paused_at should be set");
    assert_eq!(
        wf_exec.pause_reason,
        Some("Test pause".to_string()),
        "pause_reason should be set"
    );

    // Cleanup
    runner.shutdown().await.unwrap();
}

#[tokio::test]
async fn test_resume_paused_workflow() {
    let fixture = get_or_init_fixture().await;
    let mut fixture = fixture.lock().unwrap_or_else(|e| e.into_inner());

    fixture.reset_database().await;
    fixture.initialize().await;

    let database_url = fixture.get_database_url();
    let database = fixture.get_database();

    // Create a workflow with slow tasks to give us time to pause and resume
    let workflow_name = format!(
        "resume_test_pipeline_{}",
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    );

    let first_ns = TaskNamespace::new("public", "embedded", &workflow_name, "slow_first_task");

    let workflow = Workflow::builder(&workflow_name)
        .description("Test workflow for resume")
        .add_task(Arc::new(slow_first_task_task()))
        .unwrap()
        .add_task(Arc::new(
            slow_second_task_task().with_dependencies(vec![first_ns.clone()]),
        ))
        .unwrap()
        .build()
        .unwrap();

    // Register tasks in a test-scoped runtime.
    let runtime = cloacina::Runtime::empty();
    let namespace1 = TaskNamespace::new(
        workflow.tenant(),
        workflow.package(),
        workflow.name(),
        "slow_first_task",
    );
    runtime.register_task(namespace1, || {
        Arc::new(slow_first_task_task()) as Arc<dyn cloacina::Task>
    });

    let namespace2 = TaskNamespace::new(
        workflow.tenant(),
        workflow.package(),
        workflow.name(),
        "slow_second_task",
    );
    let first_ns_clone = first_ns.clone();
    runtime.register_task(namespace2, move || {
        Arc::new(slow_second_task_task().with_dependencies(vec![first_ns_clone.clone()]))
            as Arc<dyn cloacina::Task>
    });

    // Register workflow
    runtime.register_workflow(workflow.name().to_string(), {
        let workflow = workflow.clone();
        move || workflow.clone()
    });

    // Create runner
    let schema = fixture.get_schema();
    let runner = DefaultRunner::builder()
        .database_url(&database_url)
        .schema(&schema)
        .runtime(runtime)
        .build()
        .await
        .unwrap();

    // Start execution
    let input_context = Context::new();
    let execution = runner
        .execute_async(&workflow_name, input_context)
        .await
        .unwrap();
    let exec_id = execution.execution_id;

    // Wait a moment for scheduler to pick up the workflow execution
    time::sleep(Duration::from_millis(200)).await;

    // Pause the workflow execution
    execution.pause(None).await.unwrap();
    let status = execution.get_status().await.unwrap();
    assert_eq!(status, WorkflowStatus::Paused);

    // Resume the workflow execution
    execution.resume().await.unwrap();

    // Verify the workflow execution is active again (either Pending or Running)
    // Note: Resume sets status back to "Running" but the scheduler may not have
    // picked it up yet, or it may have already processed tasks
    let status = execution.get_status().await.unwrap();
    assert!(
        status == WorkflowStatus::Running || status == WorkflowStatus::Pending,
        "Workflow execution should be active after resume, got {:?}",
        status
    );

    // Verify via DAL that pause metadata is cleared
    let dal = cloacina::dal::DAL::new(database.clone());
    let wf_exec = dal
        .workflow_execution()
        .get_by_id(UniversalUuid(exec_id))
        .await
        .unwrap();
    // Resume sets status to "Running"
    assert_eq!(wf_exec.status, "Running");
    assert!(
        wf_exec.paused_at.is_none(),
        "paused_at should be cleared after resume"
    );
    assert!(
        wf_exec.pause_reason.is_none(),
        "pause_reason should be cleared after resume"
    );

    // Wait for completion using event-based polling instead of arbitrary sleep
    wait_for_terminal(&execution, Duration::from_secs(30))
        .await
        .expect("Workflow execution should complete after resume");

    // Cleanup
    runner.shutdown().await.unwrap();
}

#[tokio::test]
async fn test_pause_non_running_workflow_fails() {
    let fixture = get_or_init_fixture().await;
    let mut fixture = fixture.lock().unwrap_or_else(|e| e.into_inner());

    fixture.reset_database().await;
    fixture.initialize().await;

    let database_url = fixture.get_database_url();

    // Create a simple workflow
    let workflow_name = format!(
        "pause_fail_test_pipeline_{}",
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    );

    let workflow = Workflow::builder(&workflow_name)
        .description("Test workflow for pause failure")
        .add_task(Arc::new(quick_task_task()))
        .unwrap()
        .build()
        .unwrap();

    // Register task in a test-scoped runtime.
    let runtime = cloacina::Runtime::empty();
    let namespace = TaskNamespace::new(
        workflow.tenant(),
        workflow.package(),
        workflow.name(),
        "quick_task",
    );
    runtime.register_task(namespace, || {
        Arc::new(quick_task_task()) as Arc<dyn cloacina::Task>
    });

    // Register workflow
    runtime.register_workflow(workflow.name().to_string(), {
        let workflow = workflow.clone();
        move || workflow.clone()
    });

    // Create runner
    let schema = fixture.get_schema();
    let runner = DefaultRunner::builder()
        .database_url(&database_url)
        .schema(&schema)
        .runtime(runtime)
        .build()
        .await
        .unwrap();

    // Start execution
    let input_context = Context::new();
    let execution = runner
        .execute_async(&workflow_name, input_context)
        .await
        .unwrap();

    // Wait for workflow execution to complete using event-based polling
    wait_for_terminal(&execution, Duration::from_secs(30))
        .await
        .expect("Workflow execution should complete");

    // Try to pause a completed workflow execution - should fail
    let result = execution.pause(None).await;
    assert!(
        result.is_err(),
        "Pausing a completed workflow execution should fail"
    );

    // Cleanup
    runner.shutdown().await.unwrap();
}

#[tokio::test]
async fn test_resume_non_paused_workflow_fails() {
    let fixture = get_or_init_fixture().await;
    let mut fixture = fixture.lock().unwrap_or_else(|e| e.into_inner());

    fixture.reset_database().await;
    fixture.initialize().await;

    let database_url = fixture.get_database_url();

    // Create a simple workflow
    let workflow_name = format!(
        "resume_fail_test_pipeline_{}",
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    );

    let workflow = Workflow::builder(&workflow_name)
        .description("Test workflow for resume failure")
        .add_task(Arc::new(slow_first_task_task()))
        .unwrap()
        .build()
        .unwrap();

    // Register task in a test-scoped runtime.
    let runtime = cloacina::Runtime::empty();
    let namespace = TaskNamespace::new(
        workflow.tenant(),
        workflow.package(),
        workflow.name(),
        "slow_first_task",
    );
    runtime.register_task(namespace, || {
        Arc::new(slow_first_task_task()) as Arc<dyn cloacina::Task>
    });

    // Register workflow
    runtime.register_workflow(workflow.name().to_string(), {
        let workflow = workflow.clone();
        move || workflow.clone()
    });

    // Create runner
    let schema = fixture.get_schema();
    let runner = DefaultRunner::builder()
        .database_url(&database_url)
        .schema(&schema)
        .runtime(runtime)
        .build()
        .await
        .unwrap();

    // Start execution
    let input_context = Context::new();
    let execution = runner
        .execute_async(&workflow_name, input_context)
        .await
        .unwrap();

    // Wait for workflow execution to be picked up by scheduler (status becomes Running or stays Pending)
    // We just need it to be non-Paused for the test
    wait_for_status(
        &execution,
        |s| *s == WorkflowStatus::Running || *s == WorkflowStatus::Pending,
        Duration::from_secs(5),
    )
    .await
    .expect("Workflow execution should be scheduled");

    // Try to resume a running workflow execution (not paused) - should fail
    let result = execution.resume().await;
    assert!(
        result.is_err(),
        "Resuming a running (not paused) workflow execution should fail"
    );

    // Cleanup
    runner.shutdown().await.unwrap();
}