# elf_rust
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](https://github.com/aprchen/cloud_task_executor)
## Features
- Defining tasks with specific names that can be scheduled and executed based on the provided payload.
- Registering multiple tasks and executing them based on the task names specified in the payload.
- Logging task execution details, including success, failure, and execution time.
- Automatically initializing and configuring the runtime environment.
## Usage
To use elf_rust, add the following dependency to your `Cargo.toml` file:
```toml
[dependencies]
elf_rust = "0.1.1"
```
Below is an example of how to use elf_rust to define and execute tasks:
```rust
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())
}
```
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.