aion-server 0.18.0

Aion workflow server library: HTTP, gRPC, WebSocket, and worker endpoints. Run it with the `aion` binary from the aion-cli crate.
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
use std::{collections::HashMap, sync::Arc};

use aion_core::{
    ActivityId, DISPLAY_NAME_ATTRIBUTE, Event, EventEnvelope, PackageVersion, Payload, RunId,
    SearchAttributeValue, WorkflowId,
};
use aion_store::{EventStore, WriteToken};
use axum::http::StatusCode;
use chrono::Utc;
use serde_json::{Value, json};
use tower::ServiceExt;

use super::router::workflow_router;
use super::test_support::{
    NAMESPACE, json_request, read_json, runtime_config, server_state, shared_engine,
};
use crate::{
    NamespaceResolver, StaticScheduleNamespaces, StaticWorkflowNamespaces, config::NamespaceMode,
};

type TestResult = Result<(), Box<dyn std::error::Error>>;

#[tokio::test]
async fn parent_run_derives_its_visible_leg_from_recorded_history() -> TestResult {
    let parent_id = WorkflowId::new(uuid::Uuid::from_u128(0x244));
    let parent_run = RunId::new(uuid::Uuid::from_u128(0x2440));
    let child_id = WorkflowId::new(uuid::Uuid::from_u128(0x2441));
    let child_run = RunId::new(uuid::Uuid::from_u128(0x2442));
    let (engine, store, _visibility) = shared_engine().await?;
    seed_parent(&store, &parent_id, &parent_run, &child_id).await?;
    seed_child(&store, &child_id, &child_run).await?;

    let ownership = StaticWorkflowNamespaces::default();
    ownership.record(parent_id.clone(), NAMESPACE)?;
    ownership.record(child_id.clone(), NAMESPACE)?;
    let resolver = NamespaceResolver::from_parts(
        NamespaceMode::SharedEngine,
        Some(engine),
        Arc::new(ownership),
        Arc::new(StaticScheduleNamespaces::default()),
    );
    let router = workflow_router(server_state(resolver, runtime_config()).await?);

    let response = router
        .clone()
        .oneshot(json_request(
            "/workflows/children",
            &json!({
                "namespace": NAMESPACE,
                "workflow_id": parent_id,
                "run_id": parent_run,
            }),
        )?)
        .await?;
    assert_eq!(response.status(), StatusCode::OK);
    let body: Value = read_json(response).await?;
    assert_eq!(
        body,
        json!({
            "children": [{
                "workflow_id": child_id,
                "run_id": child_run,
                "workflow_type": "builder_leg",
                "display_name": "Builder L1",
                "status": "Running",
                "current_activity_id": 7,
                "current_attempt": 2,
            }]
        })
    );

    let current_run_response = router
        .oneshot(json_request(
            "/workflows/children",
            &json!({
                "namespace": NAMESPACE,
                "workflow_id": parent_id,
            }),
        )?)
        .await?;
    assert_eq!(current_run_response.status(), StatusCode::OK);
    let current_run_body: Value = read_json(current_run_response).await?;
    assert_eq!(current_run_body, body);
    Ok(())
}

#[tokio::test]
async fn unknown_parent_run_is_not_reported_as_an_empty_child_list() -> TestResult {
    let parent_id = WorkflowId::new(uuid::Uuid::from_u128(0x247));
    let parent_run = RunId::new(uuid::Uuid::from_u128(0x2470));
    let unknown_run = RunId::new(uuid::Uuid::from_u128(0x247f));
    let child_id = WorkflowId::new(uuid::Uuid::from_u128(0x2471));
    let (engine, store, _visibility) = shared_engine().await?;
    seed_parent(&store, &parent_id, &parent_run, &child_id).await?;

    let ownership = StaticWorkflowNamespaces::default();
    ownership.record(parent_id.clone(), NAMESPACE)?;
    let resolver = NamespaceResolver::from_parts(
        NamespaceMode::SharedEngine,
        Some(engine),
        Arc::new(ownership),
        Arc::new(StaticScheduleNamespaces::default()),
    );
    let router = workflow_router(server_state(resolver, runtime_config()).await?);

    let response = router
        .oneshot(json_request(
            "/workflows/children",
            &json!({
                "namespace": NAMESPACE,
                "workflow_id": parent_id,
                "run_id": unknown_run,
            }),
        )?)
        .await?;

    assert_eq!(response.status(), StatusCode::NOT_FOUND);
    Ok(())
}

#[tokio::test]
async fn explicit_and_implicit_parent_runs_project_different_generations() -> TestResult {
    let parent_id = WorkflowId::new(uuid::Uuid::from_u128(0x248));
    let old_run = RunId::new(uuid::Uuid::from_u128(0x2480));
    let new_run = RunId::new(uuid::Uuid::from_u128(0x2481));
    let old_child = WorkflowId::new(uuid::Uuid::from_u128(0x2482));
    let new_child = WorkflowId::new(uuid::Uuid::from_u128(0x2483));
    let (engine, store, _visibility) = shared_engine().await?;
    seed_parent(&store, &parent_id, &old_run, &old_child).await?;
    store
        .append(
            WriteToken::recorder(),
            &parent_id,
            &[
                started(3, &parent_id, &new_run, "fleet_dev")?,
                Event::ChildWorkflowStarted {
                    envelope: envelope(4, &parent_id),
                    child_workflow_id: new_child.clone(),
                    workflow_type: "new_leg".to_owned(),
                    input: payload()?,
                    package_version: version(),
                },
            ],
            2,
        )
        .await?;

    let ownership = StaticWorkflowNamespaces::default();
    for workflow_id in [&parent_id, &old_child, &new_child] {
        ownership.record(workflow_id.clone(), NAMESPACE)?;
    }
    let resolver = NamespaceResolver::from_parts(
        NamespaceMode::SharedEngine,
        Some(engine),
        Arc::new(ownership),
        Arc::new(StaticScheduleNamespaces::default()),
    );
    let router = workflow_router(server_state(resolver, runtime_config()).await?);

    let explicit = router
        .clone()
        .oneshot(json_request(
            "/workflows/children",
            &json!({"namespace": NAMESPACE, "workflow_id": parent_id, "run_id": old_run}),
        )?)
        .await?;
    assert_eq!(explicit.status(), StatusCode::OK);
    let explicit_body: Value = read_json(explicit).await?;
    assert_eq!(
        explicit_body["children"][0]["workflow_id"],
        json!(old_child)
    );

    let implicit = router
        .oneshot(json_request(
            "/workflows/children",
            &json!({"namespace": NAMESPACE, "workflow_id": parent_id}),
        )?)
        .await?;
    assert_eq!(implicit.status(), StatusCode::OK);
    let implicit_body: Value = read_json(implicit).await?;
    assert_eq!(
        implicit_body["children"][0]["workflow_id"],
        json!(new_child)
    );
    Ok(())
}

#[tokio::test]
async fn unauthorized_child_is_omitted_without_projecting_its_history() -> TestResult {
    let parent_id = WorkflowId::new(uuid::Uuid::from_u128(0x245));
    let parent_run = RunId::new(uuid::Uuid::from_u128(0x2450));
    let child_id = WorkflowId::new(uuid::Uuid::from_u128(0x2451));
    let child_run = RunId::new(uuid::Uuid::from_u128(0x2452));
    let (engine, store, _visibility) = shared_engine().await?;
    seed_parent(&store, &parent_id, &parent_run, &child_id).await?;
    seed_child(&store, &child_id, &child_run).await?;

    let ownership = StaticWorkflowNamespaces::default();
    ownership.record(parent_id.clone(), NAMESPACE)?;
    let resolver = NamespaceResolver::from_parts(
        NamespaceMode::SharedEngine,
        Some(engine),
        Arc::new(ownership),
        Arc::new(StaticScheduleNamespaces::default()),
    );
    let router = workflow_router(server_state(resolver, runtime_config()).await?);

    let response = router
        .oneshot(json_request(
            "/workflows/children",
            &json!({
                "namespace": NAMESPACE,
                "workflow_id": parent_id,
                "run_id": parent_run,
            }),
        )?)
        .await?;

    assert_eq!(response.status(), StatusCode::OK);
    let body: Value = read_json(response).await?;
    assert_eq!(
        body,
        json!({
            "children": []
        })
    );
    Ok(())
}

#[tokio::test]
async fn recorded_child_without_a_started_run_has_null_status() -> TestResult {
    let parent_id = WorkflowId::new(uuid::Uuid::from_u128(0x246));
    let parent_run = RunId::new(uuid::Uuid::from_u128(0x2460));
    let child_id = WorkflowId::new(uuid::Uuid::from_u128(0x2461));
    let (engine, store, _visibility) = shared_engine().await?;
    seed_parent(&store, &parent_id, &parent_run, &child_id).await?;

    let ownership = StaticWorkflowNamespaces::default();
    ownership.record(parent_id.clone(), NAMESPACE)?;
    ownership.record(child_id.clone(), NAMESPACE)?;
    let resolver = NamespaceResolver::from_parts(
        NamespaceMode::SharedEngine,
        Some(engine),
        Arc::new(ownership),
        Arc::new(StaticScheduleNamespaces::default()),
    );
    let router = workflow_router(server_state(resolver, runtime_config()).await?);

    let response = router
        .oneshot(json_request(
            "/workflows/children",
            &json!({
                "namespace": NAMESPACE,
                "workflow_id": parent_id,
                "run_id": parent_run,
            }),
        )?)
        .await?;

    assert_eq!(response.status(), StatusCode::OK);
    let body: Value = read_json(response).await?;
    assert_eq!(
        body,
        json!({
            "children": [{
                "workflow_id": child_id,
                "run_id": null,
                "workflow_type": "builder_leg",
                "display_name": null,
                "status": null,
                "current_activity_id": null,
                "current_attempt": null,
            }]
        })
    );
    Ok(())
}

#[tokio::test]
async fn duplicate_child_start_is_projected_once_in_first_discovery_position() -> TestResult {
    let parent_id = WorkflowId::new(uuid::Uuid::from_u128(0x249));
    let parent_run = RunId::new(uuid::Uuid::from_u128(0x2490));
    let child_id = WorkflowId::new(uuid::Uuid::from_u128(0x2491));
    let child_run = RunId::new(uuid::Uuid::from_u128(0x2492));
    let (engine, store, _visibility) = shared_engine().await?;
    seed_parent(&store, &parent_id, &parent_run, &child_id).await?;
    store
        .append(
            WriteToken::recorder(),
            &parent_id,
            &[Event::ChildWorkflowStarted {
                envelope: envelope(3, &parent_id),
                child_workflow_id: child_id.clone(),
                workflow_type: "duplicate_should_not_replace".to_owned(),
                input: payload()?,
                package_version: version(),
            }],
            2,
        )
        .await?;
    seed_child(&store, &child_id, &child_run).await?;

    let ownership = StaticWorkflowNamespaces::default();
    ownership.record(parent_id.clone(), NAMESPACE)?;
    ownership.record(child_id.clone(), NAMESPACE)?;
    let resolver = NamespaceResolver::from_parts(
        NamespaceMode::SharedEngine,
        Some(engine),
        Arc::new(ownership),
        Arc::new(StaticScheduleNamespaces::default()),
    );
    let response = workflow_router(server_state(resolver, runtime_config()).await?)
        .oneshot(json_request(
            "/workflows/children",
            &json!({"namespace": NAMESPACE, "workflow_id": parent_id, "run_id": parent_run}),
        )?)
        .await?;

    assert_eq!(response.status(), StatusCode::OK);
    let body: Value = read_json(response).await?;
    assert_eq!(body["children"].as_array().map(Vec::len), Some(1));
    assert_eq!(body["children"][0]["workflow_type"], "builder_leg");
    Ok(())
}

async fn seed_parent(
    store: &Arc<dyn EventStore>,
    parent_id: &WorkflowId,
    parent_run: &RunId,
    child_id: &WorkflowId,
) -> TestResult {
    let events = vec![
        started(1, parent_id, parent_run, "fleet_dev")?,
        Event::ChildWorkflowStarted {
            envelope: envelope(2, parent_id),
            child_workflow_id: child_id.clone(),
            workflow_type: "builder_leg".to_owned(),
            input: payload()?,
            package_version: version(),
        },
    ];
    store
        .append(WriteToken::recorder(), parent_id, &events, 0)
        .await?;
    Ok(())
}

async fn seed_child(
    store: &Arc<dyn EventStore>,
    child_id: &WorkflowId,
    child_run: &RunId,
) -> TestResult {
    let mut attributes = HashMap::new();
    attributes.insert(
        DISPLAY_NAME_ATTRIBUTE.to_owned(),
        SearchAttributeValue::String("Builder L1".to_owned()),
    );
    let activity_id = ActivityId::from_sequence_position(7);
    let events = vec![
        started(1, child_id, child_run, "builder_leg")?,
        Event::SearchAttributesUpdated {
            envelope: envelope(2, child_id),
            workflow_id: child_id.clone(),
            attributes,
        },
        Event::ActivityScheduled {
            envelope: envelope(3, child_id),
            activity_id: activity_id.clone(),
            activity_type: "build".to_owned(),
            input: payload()?,
            task_queue: "builders".to_owned(),
            node: None,
        },
        Event::ActivityStarted {
            envelope: envelope(4, child_id),
            activity_id,
            attempt: 2,
        },
    ];
    store
        .append(WriteToken::recorder(), child_id, &events, 0)
        .await?;
    Ok(())
}

fn started(
    seq: u64,
    workflow_id: &WorkflowId,
    run_id: &RunId,
    workflow_type: &str,
) -> Result<Event, aion_core::PayloadError> {
    Ok(Event::WorkflowStarted {
        envelope: envelope(seq, workflow_id),
        workflow_type: workflow_type.to_owned(),
        input: payload()?,
        run_id: run_id.clone(),
        parent_run_id: None,
        package_version: version(),
    })
}

fn envelope(seq: u64, workflow_id: &WorkflowId) -> EventEnvelope {
    EventEnvelope {
        seq,
        recorded_at: Utc::now(),
        workflow_id: workflow_id.clone(),
    }
}

fn payload() -> Result<Payload, aion_core::PayloadError> {
    Payload::from_json(&json!({ "fixture": true }))
}

fn version() -> PackageVersion {
    PackageVersion::new("a".repeat(64))
}