Skip to main content

CustomTaskHandler

Trait CustomTaskHandler 

Source
pub trait CustomTaskHandler: Send + Sync {
    // Required methods
    fn task_type(&self) -> &str;
    fn handle<'life0, 'life1, 'life2, 'life3, 'life4, 'life5, 'async_trait>(
        &'life0 self,
        task_name: &'life1 str,
        task_type: &'life2 str,
        task_config: &'life3 Value,
        input: &'life4 Value,
        context: &'life5 HandlerContext,
    ) -> Pin<Box<dyn Future<Output = WorkflowResult<Value>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait,
             'life4: 'async_trait,
             'life5: 'async_trait;
}
Expand description

Handler for custom/extension task types.

Implement this trait to provide support for custom task types that are not part of the built-in Serverless Workflow specification. Register handlers with WorkflowRunner::with_custom_task_handler().

§Example

use async_trait::async_trait;
use serde_json::Value;
use swf_runtime::{CustomTaskHandler, HandlerContext, WorkflowResult};

struct UppercaseHandler;

#[async_trait]
impl CustomTaskHandler for UppercaseHandler {
    fn task_type(&self) -> &str { "uppercase" }

    async fn handle(
        &self,
        task_name: &str,
        task_type: &str,
        task_config: &Value,
        input: &Value,
        context: &HandlerContext,
    ) -> WorkflowResult<Value> {
        let text = input["text"].as_str().unwrap_or("");
        Ok(serde_json::json!({ "result": text.to_uppercase() }))
    }
}

Required Methods§

Source

fn task_type(&self) -> &str

Returns the custom task type this handler supports (e.g., “myCustomTask”)

Source

fn handle<'life0, 'life1, 'life2, 'life3, 'life4, 'life5, 'async_trait>( &'life0 self, task_name: &'life1 str, task_type: &'life2 str, task_config: &'life3 Value, input: &'life4 Value, context: &'life5 HandlerContext, ) -> Pin<Box<dyn Future<Output = WorkflowResult<Value>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait, 'life4: 'async_trait, 'life5: 'async_trait,

Executes the custom task with the given configuration, input, and workflow context.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§