Expand description
§Cloud Task Executor
The Cloud Task Executor is a versatile and powerful framework designed to simplify the execution of tasks in cloud environments such as AWS Lambda and Alibaba Cloud Function Compute (FC). It provides a unified interface for registering and executing tasks, managing execution contexts, and handling pre- and post-execution actions. This flexibility allows developers to focus on the core logic of their tasks without worrying about the underlying cloud infrastructure.
§Features
- Unified Task Registration: Register tasks using a simple and consistent interface, making it easy to manage tasks across different cloud environments.
- Context Management: Efficiently manage execution contexts with thread-safe access to shared data.
- Pre- and Post-Execution Actions: Define actions to be executed before and after the main task execution, enabling additional processing and context modification.
- Cloud-Agnostic Execution: Seamlessly execute tasks on AWS Lambda, Alibaba Cloud FC, or local development environments without changing the core task logic.
- Automatic Payload Handling: Simplify task payload handling with built-in JSON parsing and context initialization.
§Example
use cloud_task_executor::*;
use serde_json::{json,Value};
#[cte_task(name = "my_task")]
async fn my_task(ctx: Context, payload: Value) -> Result<String, String> {
let sample_value = ctx.get("sample_key").expect("sample_key not found");
let payload_str = payload.get("payload_key").and_then(Value::as_str).unwrap_or("default_value");
let runtime = ctx.get(KEY_RUNTIME).unwrap();
println!("Task running with sample value: {}, payload: {}, runtime {}", sample_value, payload_str, runtime);
Ok("Task result".to_string())
}
#[tokio::main]
async fn main(){
let mut executor = Executor::new();
executor.set_initializer(|ctx| {
ctx.set("sample_key", "sample_value".to_string());
});
executor.set_after_action(|_ctx, payload, result| {
println!("Task executed with payload: {:?}, result: {:?}", payload, result);
result.map(|res| format!("{} - after action", res))
});
executor.set_before_action(|ctx, payload| {
ctx.set("modified_key", "test".to_string());
json!({"test":1})
});
// 注册任务
executor.set_task(my_task());
executor.run().await.expect("Executor failed to run");
}
Re-exports§
pub use executor::Executor;
pub use executor::Task;
pub use executor::Context;
pub use executor::Runtime;
pub use executor::KEY_RUNTIME;
pub use cloud_providers::handle_lambda_event;
pub use cloud_providers::create_fc_route;
pub use args::Args;