#aws-lambda #alibaba #tasks #lambda #aws #task #fc

bin+lib elf_rust

云平台的简单任务执行器

4 个版本

0.1.3 2024年7月30日
0.1.2 2024年7月29日
0.1.1 2024年7月28日
0.1.0 2024年7月28日

1389网络编程

Download history 380/week @ 2024-07-28 5/week @ 2024-08-04

每月下载 385
elf_utils 中使用

MIT 许可

18KB
203

elf_rust

elf_rust 是一个 Rust 库,旨在与 cloud_task_executor 一起使用,为各种云平台(如 AWS Lambda 和阿里巴巴云函数计算)提供简单的任务执行器。该库允许用户根据提供的有效负载定义和执行任务(脚本),非常适合无服务器计算环境。

基于此 cloud_task_executor

特性

  • 使用特定名称定义任务,可以根据提供的有效负载进行调度和执行。
  • 注册多个任务,并根据有效负载中指定的任务名称执行它们。

使用方法

要使用 elf_rust,请将以下依赖项添加到您的 Cargo.toml 文件中

[dependencies]
elf_rust = "0.1.3"

以下是使用 elf_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())
}

本地开发

要本地运行示例,请使用以下命令

cargo run -- --payload '{"tasks":["t","tf"]}'

您还可以使用以下命令运行示例

cargo run -- --payload '{"tasks":["test","test_func"]}'

许可

本项目采用 MIT 许可 - 请参阅 LICENSE 文件以获取详细信息。

依赖项

~15–28MB
~443K SLoC