#aws-lambda #cloud #alibaba #tasks #lambda #task #aws

bin+lib elf_rust

A simple task executor for cloud platform

4 releases

0.1.3 Jul 30, 2024
0.1.2 Jul 29, 2024
0.1.1 Jul 28, 2024
0.1.0 Jul 28, 2024

#1482 in Network programming


Used in elf_utils

MIT license

18KB
203 lines

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.

Dependencies

~14–28MB
~449K SLoC