elf_rust

Crates.ioelf_rust
lib.rself_rust
version0.1.3
sourcesrc
created_at2024-07-28 10:29:27.826891
updated_at2024-07-30 06:14:05.307342
descriptionA simple task executor for cloud platform
homepage
repositoryhttps://github.com/aprchen/elf_rust
max_upload_size
id1317928
size62,687
chen su (aprchen)

documentation

README

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

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.

Usage

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())
}

Local Development

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"]}'

License

This project is licensed under the MIT License - see the LICENSE file for details.

Commit count: 0

cargo fmt