Crates.io | elf_rust |
lib.rs | elf_rust |
version | 0.1.3 |
source | src |
created_at | 2024-07-28 10:29:27.826891 |
updated_at | 2024-07-30 06:14:05.307342 |
description | A simple task executor for cloud platform |
homepage | |
repository | https://github.com/aprchen/elf_rust |
max_upload_size | |
id | 1317928 |
size | 62,687 |
elf_rust is a Rust library designed to work in conjunction with cloud_task_executor to provide a simple task executor for various cloud platforms, such as AWS Lambda and Alibaba Cloud Function Compute. The library allows users to define and execute tasks (scripts) based on the payload provided, making it suitable for serverless computing environments.
Based on this cloud_task_executor
To use elf_rust, add the following dependency to your Cargo.toml
file:
[dependencies]
elf_rust = "0.1.3"
Below is an example of how to use elf_rust to define and execute tasks:
use log::{error, info};
use serde_json::Value;
use tokio::time::sleep;
use elf_rust::*;
#[tokio::main]
async fn main() {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"))
.format(json_format)
.init();
let mut app = App::new();
app.add_script(MyScript::new("test", test));
app.add_script(MyScript::new("test_func", test1));
let mut executor = Executor::new();
executor.set_task(app.into());
executor.set_after_action(|_ctx, payload, result| {
let runtime = _ctx.get(KEY_RUNTIME).unwrap();
info!("task executed with payload: {}, result: {:?}, runtime {}", payload.to_string(), result.clone().unwrap(),runtime);
result
});
// cargo run -- --payload '{"tasks":["t","tf"]}'
executor.run().await.expect("executor failed to run");
}
async fn test(ctx: Context, input: Value) -> Result<String, String> {
info!("test {} runtime {}", input,ctx.get(KEY_RUNTIME).unwrap().to_string());
sleep(std::time::Duration::from_secs(2)).await;
Ok("test".into())
}
async fn test1(_ctx: Context, input: Value) -> Result<String, String> {
error!("test_1 {}", input);
Err("test".into())
}
To run the example locally, use the following command:
cargo run -- --payload '{"tasks":["t","tf"]}'
You can also use the following command to run the example:
cargo run -- --payload '{"tasks":["test","test_func"]}'
This project is licensed under the MIT License - see the LICENSE file for details.