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