kruxiaflow-worker 0.12.1

Worker SDK for Kruxia Flow — run Rust activities in budgeted workflows with engine-enforced cost limits
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
//! Activity handler registration and dispatch.

use crate::context::ActivityContext;
use crate::error::ActivityError;
use crate::result::ActivityResult;
use crate::types::PendingActivity;
use async_trait::async_trait;
use serde::de::DeserializeOwned;
use serde_json::Value;
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;

/// An activity handler.
///
/// Activities are matched to workflow definitions by the `(worker, name)`
/// pair — the definition's `worker:` and `name:` fields.
///
/// For handlers with a typed parameter struct, implement [`TypedActivity`]
/// instead; for one-off closures, use
/// [`ActivityRegistry::register_fn`].
#[async_trait]
pub trait ActivityImpl: Send + Sync {
    /// Execute the activity.
    async fn execute(
        &self,
        parameters: Value,
        ctx: &ActivityContext,
    ) -> Result<ActivityResult, ActivityError>;

    /// Activity name (the workflow definition's `name:` field).
    fn name(&self) -> &str;

    /// Worker type (the workflow definition's `worker:` field).
    fn worker(&self) -> &str;
}

/// An activity handler with a typed, `serde`-deserialized parameter struct.
///
/// The queued activity's JSON parameters are deserialized into
/// [`Self::Params`]; a deserialization failure is reported as a
/// **non-retryable** `INVALID_PARAMETERS` failure (bad input will not improve
/// on retry). Register with [`ActivityRegistry::register_typed`].
///
/// ```
/// use kruxiaflow_worker::{ActivityContext, ActivityError, ActivityResult, TypedActivity};
/// use serde_json::json;
///
/// #[derive(serde::Deserialize)]
/// struct GreetParams {
///     name: String,
/// }
///
/// struct Greet;
///
/// #[async_trait::async_trait]
/// impl TypedActivity for Greet {
///     type Params = GreetParams;
///
///     fn worker(&self) -> &str {
///         "demo"
///     }
///
///     fn name(&self) -> &str {
///         "greet"
///     }
///
///     async fn execute(
///         &self,
///         params: GreetParams,
///         _ctx: &ActivityContext,
///     ) -> Result<ActivityResult, ActivityError> {
///         Ok(ActivityResult::value("greeting", json!(format!("Hello, {}!", params.name))))
///     }
/// }
/// ```
#[async_trait]
pub trait TypedActivity: Send + Sync {
    /// Parameter struct the activity's JSON parameters deserialize into.
    type Params: DeserializeOwned + Send;

    /// Worker type (the workflow definition's `worker:` field).
    fn worker(&self) -> &str;

    /// Activity name (the workflow definition's `name:` field).
    fn name(&self) -> &str;

    /// Execute the activity with deserialized parameters.
    async fn execute(
        &self,
        params: Self::Params,
        ctx: &ActivityContext,
    ) -> Result<ActivityResult, ActivityError>;
}

fn invalid_parameters(key: &str, err: serde_json::Error) -> ActivityError {
    ActivityError::terminal(
        "INVALID_PARAMETERS",
        format!("Failed to deserialize parameters for {key}: {err}"),
    )
}

struct TypedAdapter<T>(T);

#[async_trait]
impl<T: TypedActivity> ActivityImpl for TypedAdapter<T> {
    async fn execute(
        &self,
        parameters: Value,
        ctx: &ActivityContext,
    ) -> Result<ActivityResult, ActivityError> {
        let params: T::Params = serde_json::from_value(parameters).map_err(|err| {
            invalid_parameters(&format!("{}.{}", self.worker(), self.name()), err)
        })?;
        self.0.execute(params, ctx).await
    }

    fn name(&self) -> &str {
        self.0.name()
    }

    fn worker(&self) -> &str {
        self.0.worker()
    }
}

type BoxedHandler = Box<
    dyn Fn(
            Value,
            ActivityContext,
        ) -> Pin<Box<dyn Future<Output = Result<ActivityResult, ActivityError>> + Send>>
        + Send
        + Sync,
>;

struct FnActivity {
    worker: String,
    name: String,
    handler: BoxedHandler,
}

#[async_trait]
impl ActivityImpl for FnActivity {
    async fn execute(
        &self,
        parameters: Value,
        ctx: &ActivityContext,
    ) -> Result<ActivityResult, ActivityError> {
        (self.handler)(parameters, ctx.clone()).await
    }

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

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

/// Registry of activity handlers, keyed by `worker.name`.
#[derive(Default)]
pub struct ActivityRegistry {
    implementations: HashMap<String, Arc<dyn ActivityImpl>>,
}

impl ActivityRegistry {
    /// Create an empty registry.
    pub fn new() -> Self {
        Self::default()
    }

    /// Register an [`ActivityImpl`], keyed by its `worker()` and `name()`.
    pub fn register(&mut self, implementation: Arc<dyn ActivityImpl>) {
        let key = format!("{}.{}", implementation.worker(), implementation.name());
        tracing::info!(activity = %key, "Registering activity");
        self.implementations.insert(key, implementation);
    }

    /// Register a [`TypedActivity`].
    pub fn register_typed<T: TypedActivity + 'static>(&mut self, activity: T) {
        self.register(Arc::new(TypedAdapter(activity)));
    }

    /// Register an async closure as an activity handler.
    ///
    /// The parameter type `P` may be any `serde::Deserialize` struct (typed
    /// parameters) or `serde_json::Value` (raw parameters). Deserialization
    /// failures are reported as non-retryable `INVALID_PARAMETERS` failures.
    ///
    /// ```
    /// use kruxiaflow_worker::{ActivityContext, ActivityRegistry, ActivityResult};
    /// use serde_json::json;
    ///
    /// #[derive(serde::Deserialize)]
    /// struct EchoParams {
    ///     message: String,
    /// }
    ///
    /// let mut registry = ActivityRegistry::new();
    /// registry.register_fn("demo", "echo", |params: EchoParams, _ctx: ActivityContext| async move {
    ///     Ok(ActivityResult::value("echoed", json!(params.message)))
    /// });
    /// ```
    pub fn register_fn<P, F, Fut>(
        &mut self,
        worker: impl Into<String>,
        name: impl Into<String>,
        handler: F,
    ) where
        P: DeserializeOwned + Send + 'static,
        F: Fn(P, ActivityContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<ActivityResult, ActivityError>> + Send + 'static,
    {
        let worker = worker.into();
        let name = name.into();
        let key = format!("{worker}.{name}");
        let handler: BoxedHandler = Box::new(move |parameters, ctx| {
            let key = key.clone();
            match serde_json::from_value::<P>(parameters) {
                Ok(params) => Box::pin(handler(params, ctx)),
                Err(err) => Box::pin(async move { Err(invalid_parameters(&key, err)) }),
            }
        });
        self.register(Arc::new(FnActivity {
            worker,
            name,
            handler,
        }));
    }

    /// Get a handler by worker and name.
    pub fn get(&self, worker: &str, name: &str) -> Option<&Arc<dyn ActivityImpl>> {
        self.implementations.get(&format!("{worker}.{name}"))
    }

    /// All registered `worker.name` activity types.
    pub fn activity_types(&self) -> Vec<String> {
        self.implementations.keys().cloned().collect()
    }

    /// Distinct worker names across registered activities.
    pub fn worker_names(&self) -> Vec<String> {
        let mut names: Vec<String> = self
            .implementations
            .values()
            .map(|i| i.worker().to_string())
            .collect();
        names.sort();
        names.dedup();
        names
    }

    /// Whether the registry has no handlers.
    pub fn is_empty(&self) -> bool {
        self.implementations.is_empty()
    }
}

/// Executes claimed activities. The poller drives this for every activity it
/// claims.
///
/// [`ActivityRegistry`] implements this by dispatching to registered
/// handlers; implement it directly to put custom machinery (caching,
/// streaming, file staging) between the poll loop and your handlers.
///
/// The poller enforces `timeout` around the whole call and catches panics;
/// implementations receive it for their own bookkeeping (e.g., inner
/// deadlines) and need not enforce it.
#[async_trait]
pub trait ActivityExecutor: Send + Sync {
    /// Execute one claimed activity.
    async fn execute(
        &self,
        activity: &PendingActivity,
        ctx: &ActivityContext,
        timeout: Duration,
    ) -> Result<ActivityResult, ActivityError>;
}

#[async_trait]
impl ActivityExecutor for ActivityRegistry {
    async fn execute(
        &self,
        activity: &PendingActivity,
        ctx: &ActivityContext,
        _timeout: Duration,
    ) -> Result<ActivityResult, ActivityError> {
        let implementation = self
            .get(&activity.worker, &activity.activity_name)
            .ok_or_else(|| {
                ActivityError::retryable(
                    "ACTIVITY_NOT_FOUND",
                    format!(
                        "Activity implementation not found: {}.{}",
                        activity.worker, activity.activity_name
                    ),
                )
            })?;

        implementation
            .execute(activity.parameters.clone(), ctx)
            .await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use uuid::Uuid;

    fn test_ctx() -> ActivityContext {
        ActivityContext::new(Uuid::now_v7(), Uuid::now_v7(), "step")
    }

    fn pending(worker: &str, name: &str, parameters: Value) -> PendingActivity {
        serde_json::from_value(json!({
            "activity_id": Uuid::now_v7(),
            "workflow_id": Uuid::now_v7(),
            "activity_key": "step",
            "worker": worker,
            "activity_name": name,
            "parameters": parameters,
        }))
        .unwrap()
    }

    #[derive(serde::Deserialize)]
    struct EchoParams {
        message: String,
    }

    #[tokio::test]
    async fn register_fn_typed_params() {
        let mut registry = ActivityRegistry::new();
        registry.register_fn("demo", "echo", |params: EchoParams, _ctx| async move {
            Ok(ActivityResult::value("echoed", json!(params.message)))
        });

        let result = ActivityExecutor::execute(
            &registry,
            &pending("demo", "echo", json!({"message": "hi"})),
            &test_ctx(),
            Duration::from_secs(5),
        )
        .await
        .unwrap();
        assert_eq!(result.get_output("echoed").unwrap().value, json!("hi"));
    }

    #[tokio::test]
    async fn register_fn_bad_params_is_terminal() {
        let mut registry = ActivityRegistry::new();
        registry.register_fn("demo", "echo", |params: EchoParams, _ctx| async move {
            Ok(ActivityResult::value("echoed", json!(params.message)))
        });

        let err = ActivityExecutor::execute(
            &registry,
            &pending("demo", "echo", json!({"wrong_field": 1})),
            &test_ctx(),
            Duration::from_secs(5),
        )
        .await
        .unwrap_err();
        assert_eq!(err.code, "INVALID_PARAMETERS");
        assert!(!err.retryable);
    }

    #[tokio::test]
    async fn register_fn_raw_value_params() {
        let mut registry = ActivityRegistry::new();
        registry.register_fn("demo", "raw", |params: Value, _ctx| async move {
            Ok(ActivityResult::value("raw", params))
        });

        let result = ActivityExecutor::execute(
            &registry,
            &pending("demo", "raw", json!([1, 2, 3])),
            &test_ctx(),
            Duration::from_secs(5),
        )
        .await
        .unwrap();
        assert_eq!(result.get_output("raw").unwrap().value, json!([1, 2, 3]));
    }

    #[tokio::test]
    async fn missing_activity_is_retryable() {
        let registry = ActivityRegistry::new();
        let err = ActivityExecutor::execute(
            &registry,
            &pending("demo", "nope", json!({})),
            &test_ctx(),
            Duration::from_secs(5),
        )
        .await
        .unwrap_err();
        assert_eq!(err.code, "ACTIVITY_NOT_FOUND");
        assert!(err.retryable);
    }

    struct Greet;

    #[async_trait]
    impl TypedActivity for Greet {
        type Params = EchoParams;

        fn worker(&self) -> &str {
            "demo"
        }

        fn name(&self) -> &str {
            "greet"
        }

        async fn execute(
            &self,
            params: EchoParams,
            _ctx: &ActivityContext,
        ) -> Result<ActivityResult, ActivityError> {
            Ok(ActivityResult::value(
                "greeting",
                json!(format!("Hello, {}!", params.message)),
            ))
        }
    }

    #[tokio::test]
    async fn register_typed_trait() {
        let mut registry = ActivityRegistry::new();
        registry.register_typed(Greet);
        assert_eq!(registry.activity_types(), vec!["demo.greet".to_string()]);
        assert_eq!(registry.worker_names(), vec!["demo".to_string()]);

        let result = ActivityExecutor::execute(
            &registry,
            &pending("demo", "greet", json!({"message": "world"})),
            &test_ctx(),
            Duration::from_secs(5),
        )
        .await
        .unwrap();
        assert_eq!(
            result.get_output("greeting").unwrap().value,
            json!("Hello, world!")
        );
    }
}