hatchet-client 0.1.0

Rust SDK for Hatchet: run workers and trigger tasks
Documentation
//! A batch task: five runs started at once, handled in a single call, each
//! answered with its own output.
//!
//! `BatchConfig::max_size(5)` tells the engine to buffer up to five
//! concurrent runs of `word_lengths` and hand them to the worker as one
//! `START_BATCH`; the handler prints how many items and why the engine
//! flushed now (`batch.len()`, `batch.trigger_reason()`), so it is visible
//! that it only ran once for all five runs.
//!
//! Needs an engine that batches (`BATCH_MIN_ENGINE_VERSION`, v0.98.0 and
//! newer); an older one is refused at `WorkerBuilder::build` rather than
//! silently handing the runs over one at a time.
//!
//! Run against a local hatchet-lite engine (see `examples/README.md`):
//!
//!     cargo run -p hatchet-client --example batch

use hatchet_client::{BatchConfig, BatchOutput, Client, RunOpts, TaskBuilder};
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
struct Word {
    text: String,
}

#[derive(Deserialize, Serialize)]
struct Length {
    n: usize,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    tracing_subscriber::fmt::init();

    let client = Client::from_env()?;

    let word_lengths = TaskBuilder::<Word, Length>::new("word-lengths").batch_handler(
        BatchConfig::max_size(5),
        |_ctx, batch| async move {
            println!(
                "batch handler called once with {} item(s), trigger reason: {:?}",
                batch.len(),
                batch.trigger_reason()
            );
            let mut output = BatchOutput::new();
            for (id, word) in batch.iter() {
                output = output.ok(id.clone(), Length { n: word.text.len() });
            }
            Ok(output)
        },
    );

    let worker = client
        .worker("batch-example")
        .register(&word_lengths)
        .build()
        .await?;
    let shutdown = worker.shutdown_handle();
    let running = tokio::spawn(worker.run());

    // Trigger five runs together with one `run_many` call so the engine buffers
    // them into a single batch, then wait for each run's own result.
    let words = ["hatchet", "batch", "task", "engine", "worker"];
    let handles = word_lengths
        .run_many(
            &client,
            words.iter().map(|word| {
                (
                    Word {
                        text: (*word).to_string(),
                    },
                    RunOpts::default(),
                )
            }),
        )
        .await?;

    let mut results = Vec::with_capacity(handles.len());
    for handle in &handles {
        results.push(handle.result().await?.n);
    }

    shutdown.shutdown();
    running.await??;

    println!(
        "batch of {} run(s) completed with lengths: {:?}",
        results.len(),
        results
    );
    Ok(())
}