ironflow-engine 2.10.0

Workflow orchestration engine for ironflow with FSM-based run lifecycle
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
//! Domain events emitted throughout the ironflow lifecycle.

use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use ironflow_store::models::{RunStatus, StepKind};

/// A domain event emitted by the ironflow system.
///
/// Covers the full lifecycle: runs, steps, approvals, and authentication.
/// Subscribers receive these via [`EventPublisher`](super::EventPublisher)
/// and pattern-match on the variants they care about.
///
/// # Examples
///
/// ```
/// use ironflow_engine::notify::Event;
/// use ironflow_store::models::RunStatus;
/// use uuid::Uuid;
///
/// let event = Event::RunStatusChanged {
///     run_id: Uuid::now_v7(),
///     workflow_name: "deploy".to_string(),
///     from: RunStatus::Running,
///     to: RunStatus::Completed,
///     error: None,
///     cost_usd: rust_decimal::Decimal::ZERO,
///     duration_ms: 5000,
///     at: chrono::Utc::now(),
/// };
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Event {
    // -- Run lifecycle --
    /// A new run was created (status: Pending).
    RunCreated {
        /// Run identifier.
        run_id: Uuid,
        /// Workflow name.
        workflow_name: String,
        /// When the run was created.
        at: DateTime<Utc>,
    },

    /// A run changed status.
    RunStatusChanged {
        /// Run identifier.
        run_id: Uuid,
        /// Workflow name.
        workflow_name: String,
        /// Previous status.
        from: RunStatus,
        /// New status.
        to: RunStatus,
        /// Error message (when transitioning to Failed).
        error: Option<String>,
        /// Aggregated cost in USD at the time of transition.
        cost_usd: Decimal,
        /// Aggregated duration in milliseconds at the time of transition.
        duration_ms: u64,
        /// When the transition occurred.
        at: DateTime<Utc>,
    },

    /// A run transitioned to [`Failed`](ironflow_store::models::RunStatus::Failed).
    ///
    /// This is a convenience event emitted alongside [`RunStatusChanged`](Event::RunStatusChanged)
    /// when the target status is `Failed`. Subscribe to this instead of
    /// `RUN_STATUS_CHANGED` when you only care about failures.
    RunFailed {
        /// Run identifier.
        run_id: Uuid,
        /// Workflow name.
        workflow_name: String,
        /// Error message.
        error: Option<String>,
        /// Aggregated cost in USD at the time of failure.
        cost_usd: Decimal,
        /// Aggregated duration in milliseconds at the time of failure.
        duration_ms: u64,
        /// When the failure occurred.
        at: DateTime<Utc>,
    },

    // -- Step lifecycle --
    /// A step completed successfully.
    StepCompleted {
        /// Run identifier.
        run_id: Uuid,
        /// Step identifier.
        step_id: Uuid,
        /// Human-readable step name.
        step_name: String,
        /// Step operation kind.
        #[cfg_attr(feature = "openapi", schema(value_type = String))]
        kind: StepKind,
        /// Step duration in milliseconds.
        duration_ms: u64,
        /// Step cost in USD.
        cost_usd: Decimal,
        /// When the step completed.
        at: DateTime<Utc>,
    },

    /// A step failed.
    StepFailed {
        /// Run identifier.
        run_id: Uuid,
        /// Step identifier.
        step_id: Uuid,
        /// Human-readable step name.
        step_name: String,
        /// Step operation kind.
        #[cfg_attr(feature = "openapi", schema(value_type = String))]
        kind: StepKind,
        /// Error message.
        error: String,
        /// When the step failed.
        at: DateTime<Utc>,
    },

    // -- Approval --
    /// A run is waiting for human approval.
    ApprovalRequested {
        /// Run identifier.
        run_id: Uuid,
        /// Approval step identifier.
        step_id: Uuid,
        /// Message displayed to reviewers.
        message: String,
        /// When the approval was requested.
        at: DateTime<Utc>,
    },

    /// A run was approved by a human.
    ApprovalGranted {
        /// Run identifier.
        run_id: Uuid,
        /// User who approved (ID or username).
        approved_by: String,
        /// When the approval was granted.
        at: DateTime<Utc>,
    },

    /// A run was rejected by a human.
    ApprovalRejected {
        /// Run identifier.
        run_id: Uuid,
        /// User who rejected (ID or username).
        rejected_by: String,
        /// When the rejection occurred.
        at: DateTime<Utc>,
    },

    // -- Authentication --
    /// A user signed in.
    UserSignedIn {
        /// User identifier.
        user_id: Uuid,
        /// Username.
        username: String,
        /// When the sign-in occurred.
        at: DateTime<Utc>,
    },

    /// A new user signed up.
    UserSignedUp {
        /// User identifier.
        user_id: Uuid,
        /// Username.
        username: String,
        /// When the sign-up occurred.
        at: DateTime<Utc>,
    },

    /// A user signed out.
    UserSignedOut {
        /// User identifier.
        user_id: Uuid,
        /// When the sign-out occurred.
        at: DateTime<Utc>,
    },
}

impl Event {
    /// Event type constant for [`RunCreated`](Event::RunCreated).
    pub const RUN_CREATED: &'static str = "run_created";
    /// Event type constant for [`RunStatusChanged`](Event::RunStatusChanged).
    pub const RUN_STATUS_CHANGED: &'static str = "run_status_changed";
    /// Event type constant for [`RunFailed`](Event::RunFailed).
    pub const RUN_FAILED: &'static str = "run_failed";
    /// Event type constant for [`StepCompleted`](Event::StepCompleted).
    pub const STEP_COMPLETED: &'static str = "step_completed";
    /// Event type constant for [`StepFailed`](Event::StepFailed).
    pub const STEP_FAILED: &'static str = "step_failed";
    /// Event type constant for [`ApprovalRequested`](Event::ApprovalRequested).
    pub const APPROVAL_REQUESTED: &'static str = "approval_requested";
    /// Event type constant for [`ApprovalGranted`](Event::ApprovalGranted).
    pub const APPROVAL_GRANTED: &'static str = "approval_granted";
    /// Event type constant for [`ApprovalRejected`](Event::ApprovalRejected).
    pub const APPROVAL_REJECTED: &'static str = "approval_rejected";
    /// Event type constant for [`UserSignedIn`](Event::UserSignedIn).
    pub const USER_SIGNED_IN: &'static str = "user_signed_in";
    /// Event type constant for [`UserSignedUp`](Event::UserSignedUp).
    pub const USER_SIGNED_UP: &'static str = "user_signed_up";
    /// Event type constant for [`UserSignedOut`](Event::UserSignedOut).
    pub const USER_SIGNED_OUT: &'static str = "user_signed_out";

    /// All event types. Pass this to
    /// [`EventPublisher::subscribe`](super::EventPublisher::subscribe) to
    /// receive every event.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use ironflow_engine::notify::{Event, EventPublisher, WebhookSubscriber};
    ///
    /// let mut publisher = EventPublisher::new();
    /// publisher.subscribe(
    ///     WebhookSubscriber::new("https://example.com/all"),
    ///     Event::ALL,
    /// );
    /// ```
    pub const ALL: &'static [&'static str] = &[
        Self::RUN_CREATED,
        Self::RUN_STATUS_CHANGED,
        Self::RUN_FAILED,
        Self::STEP_COMPLETED,
        Self::STEP_FAILED,
        Self::APPROVAL_REQUESTED,
        Self::APPROVAL_GRANTED,
        Self::APPROVAL_REJECTED,
        Self::USER_SIGNED_IN,
        Self::USER_SIGNED_UP,
        Self::USER_SIGNED_OUT,
    ];

    /// Returns the event type as a static string (e.g. `"run_status_changed"`).
    ///
    /// Useful for filtering and logging without deserializing.
    ///
    /// # Examples
    ///
    /// ```
    /// use ironflow_engine::notify::Event;
    /// use uuid::Uuid;
    /// use chrono::Utc;
    ///
    /// let event = Event::UserSignedIn {
    ///     user_id: Uuid::now_v7(),
    ///     username: "alice".to_string(),
    ///     at: Utc::now(),
    /// };
    /// assert_eq!(event.event_type(), "user_signed_in");
    /// ```
    pub fn event_type(&self) -> &'static str {
        match self {
            Event::RunCreated { .. } => Self::RUN_CREATED,
            Event::RunStatusChanged { .. } => Self::RUN_STATUS_CHANGED,
            Event::RunFailed { .. } => Self::RUN_FAILED,
            Event::StepCompleted { .. } => Self::STEP_COMPLETED,
            Event::StepFailed { .. } => Self::STEP_FAILED,
            Event::ApprovalRequested { .. } => Self::APPROVAL_REQUESTED,
            Event::ApprovalGranted { .. } => Self::APPROVAL_GRANTED,
            Event::ApprovalRejected { .. } => Self::APPROVAL_REJECTED,
            Event::UserSignedIn { .. } => Self::USER_SIGNED_IN,
            Event::UserSignedUp { .. } => Self::USER_SIGNED_UP,
            Event::UserSignedOut { .. } => Self::USER_SIGNED_OUT,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn run_status_changed_serde_roundtrip() {
        let event = Event::RunStatusChanged {
            run_id: Uuid::now_v7(),
            workflow_name: "deploy".to_string(),
            from: RunStatus::Running,
            to: RunStatus::Completed,
            error: None,
            cost_usd: Decimal::new(42, 2),
            duration_ms: 5000,
            at: Utc::now(),
        };

        let json = serde_json::to_string(&event).expect("serialize");
        let back: Event = serde_json::from_str(&json).expect("deserialize");

        assert_eq!(back.event_type(), "run_status_changed");
        assert!(json.contains("\"type\":\"run_status_changed\""));
    }

    #[test]
    fn run_failed_serde_roundtrip() {
        let event = Event::RunFailed {
            run_id: Uuid::now_v7(),
            workflow_name: "deploy".to_string(),
            error: Some("step crashed".to_string()),
            cost_usd: Decimal::new(10, 2),
            duration_ms: 3000,
            at: Utc::now(),
        };

        let json = serde_json::to_string(&event).expect("serialize");
        let back: Event = serde_json::from_str(&json).expect("deserialize");

        assert_eq!(back.event_type(), "run_failed");
        assert!(json.contains("\"type\":\"run_failed\""));
        assert!(json.contains("step crashed"));
    }

    #[test]
    fn user_signed_in_serde_roundtrip() {
        let event = Event::UserSignedIn {
            user_id: Uuid::now_v7(),
            username: "alice".to_string(),
            at: Utc::now(),
        };

        let json = serde_json::to_string(&event).expect("serialize");
        let back: Event = serde_json::from_str(&json).expect("deserialize");

        assert_eq!(back.event_type(), "user_signed_in");
        assert!(json.contains("alice"));
    }

    #[test]
    fn step_failed_serde_roundtrip() {
        let event = Event::StepFailed {
            run_id: Uuid::now_v7(),
            step_id: Uuid::now_v7(),
            step_name: "build".to_string(),
            kind: StepKind::Shell,
            error: "exit code 1".to_string(),
            at: Utc::now(),
        };

        let json = serde_json::to_string(&event).expect("serialize");
        let back: Event = serde_json::from_str(&json).expect("deserialize");

        assert_eq!(back.event_type(), "step_failed");
    }

    #[test]
    fn approval_requested_serde_roundtrip() {
        let event = Event::ApprovalRequested {
            run_id: Uuid::now_v7(),
            step_id: Uuid::now_v7(),
            message: "Deploy to prod?".to_string(),
            at: Utc::now(),
        };

        let json = serde_json::to_string(&event).expect("serialize");
        assert!(json.contains("approval_requested"));
    }

    #[test]
    fn event_type_all_variants() {
        let id = Uuid::now_v7();
        let now = Utc::now();

        let cases: Vec<(Event, &str)> = vec![
            (
                Event::RunCreated {
                    run_id: id,
                    workflow_name: "w".to_string(),
                    at: now,
                },
                "run_created",
            ),
            (
                Event::RunStatusChanged {
                    run_id: id,
                    workflow_name: "w".to_string(),
                    from: RunStatus::Pending,
                    to: RunStatus::Running,
                    error: None,
                    cost_usd: Decimal::ZERO,
                    duration_ms: 0,
                    at: now,
                },
                "run_status_changed",
            ),
            (
                Event::RunFailed {
                    run_id: id,
                    workflow_name: "w".to_string(),
                    error: Some("boom".to_string()),
                    cost_usd: Decimal::ZERO,
                    duration_ms: 0,
                    at: now,
                },
                "run_failed",
            ),
            (
                Event::StepCompleted {
                    run_id: id,
                    step_id: id,
                    step_name: "s".to_string(),
                    kind: StepKind::Shell,
                    duration_ms: 0,
                    cost_usd: Decimal::ZERO,
                    at: now,
                },
                "step_completed",
            ),
            (
                Event::StepFailed {
                    run_id: id,
                    step_id: id,
                    step_name: "s".to_string(),
                    kind: StepKind::Shell,
                    error: "err".to_string(),
                    at: now,
                },
                "step_failed",
            ),
            (
                Event::ApprovalRequested {
                    run_id: id,
                    step_id: id,
                    message: "ok?".to_string(),
                    at: now,
                },
                "approval_requested",
            ),
            (
                Event::ApprovalGranted {
                    run_id: id,
                    approved_by: "alice".to_string(),
                    at: now,
                },
                "approval_granted",
            ),
            (
                Event::ApprovalRejected {
                    run_id: id,
                    rejected_by: "bob".to_string(),
                    at: now,
                },
                "approval_rejected",
            ),
            (
                Event::UserSignedIn {
                    user_id: id,
                    username: "u".to_string(),
                    at: now,
                },
                "user_signed_in",
            ),
            (
                Event::UserSignedUp {
                    user_id: id,
                    username: "u".to_string(),
                    at: now,
                },
                "user_signed_up",
            ),
            (
                Event::UserSignedOut {
                    user_id: id,
                    at: now,
                },
                "user_signed_out",
            ),
        ];

        for (event, expected_type) in cases {
            assert_eq!(event.event_type(), expected_type);
        }
    }
}