Crates.io | cloud_task_executor |
lib.rs | cloud_task_executor |
version | 0.1.4 |
source | src |
created_at | 2024-07-26 12:39:31.502982 |
updated_at | 2024-07-30 05:58:05.659716 |
description | 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. |
homepage | |
repository | https://github.com/aprchen/cloud_task_executor |
max_upload_size | |
id | 1316090 |
size | 66,425 |
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. Upper application link elf_rust
The Cloud Task Executor is built around a modular architecture that includes the following key components:
The Cloud Task Executor can be easily integrated into your Rust projects using the provided crate. To get started, add the following dependency to your Cargo.toml file:
[dependencies]
cloud_task_executor = "0.1.4"
Next, import the crate into your project:
use cloud_task_executor::*;
Tasks are registered using a custom procedural macro cte_task. This macro simplifies the task registration process by automatically generating the necessary boilerplate code. To register a task, define a function with the #[cte_task] attribute and specify the task name and description.
#[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())
}
The executor is set up with context initializers, before-actions, after-actions, and tasks.
let mut executor = Executor::new();
executor.set_initializer(|ctx| {
ctx.set("sample_key", "sample_value".to_string());
});
executor.set_before_action(|ctx, payload| {
ctx.set("modified_key", "test".to_string());
let mut new_payload = json!({"test": 1});
if let Value::Object(map) = payload {
if let Value::Object(new_map) = &mut new_payload {
for (k, v) in map {
new_map.insert(k.clone(), v.clone());
}
}
}
new_payload
});
executor.set_after_action(|ctx, payload, result| {
let context = ctx;
println!("Task executed with payload: {:?}, result: {:?}", payload, result);
result.map(|res| format!("{} - after action", res))
});
executor.set_task(my_task());
The executor can be run in different environments, including local development, AWS Lambda, and Alibaba Cloud FC.
#[tokio::main]
async fn main() {
let result = executor.run().await;
match result {
Ok(msg) => println!("{}", msg),
Err(err) => eprintln!("Error: {}", err),
}
}
cargo run -- --payload '{}'
The Cloud Task Executor is a powerful tool for developers looking to streamline their cloud task execution workflows. With its unified interface, robust context management, and flexible pre- and post-actions, it simplifies the process of writing, registering, and executing tasks in various cloud environments.
This description should provide a clear and comprehensive overview of your project, highlighting its features, architecture, and usage. Let me know if you need any further adjustments or additional details!
This project is licensed under the MIT License - see the LICENSE file for details.