update api, fix tests and examples
This commit is contained in:
64
examples/simple_job_workflow.rs
Normal file
64
examples/simple_job_workflow.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
//! Simple job workflow example
|
||||
//!
|
||||
//! This example demonstrates the basic job lifecycle using the new API:
|
||||
//! 1. Create a job
|
||||
//! 2. Start the job
|
||||
//! 3. Monitor its progress
|
||||
//! 4. Get the result
|
||||
|
||||
use hero_supervisor_openrpc_client::{SupervisorClient, JobBuilder, JobResult};
|
||||
use std::time::Duration;
|
||||
use tokio::time::sleep;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("Simple Job Workflow Example");
|
||||
println!("============================\n");
|
||||
|
||||
// Create client
|
||||
let client = SupervisorClient::new("http://localhost:3030")?;
|
||||
let secret = "user-secret-456";
|
||||
|
||||
// Create a simple job
|
||||
let job = JobBuilder::new()
|
||||
.caller_id("simple_example")
|
||||
.context_id("demo")
|
||||
.payload("echo 'Hello from Hero Supervisor!' && sleep 3 && echo 'Job completed!'")
|
||||
.executor("osis")
|
||||
.runner("osis_runner_1")
|
||||
.timeout(60)
|
||||
.env_var("EXAMPLE_VAR", "example_value")
|
||||
.build()?;
|
||||
|
||||
println!("📝 Creating job...");
|
||||
let job_id = client.jobs_create(secret, job).await?;
|
||||
println!("✅ Job created: {}\n", job_id);
|
||||
|
||||
println!("🚀 Starting job...");
|
||||
client.job_start(secret, &job_id).await?;
|
||||
println!("✅ Job started\n");
|
||||
|
||||
println!("👀 Monitoring job progress...");
|
||||
loop {
|
||||
let status = client.job_status(&job_id).await?;
|
||||
println!(" Status: {}", status.status);
|
||||
|
||||
if status.status == "completed" || status.status == "failed" {
|
||||
break;
|
||||
}
|
||||
|
||||
sleep(Duration::from_secs(2)).await;
|
||||
}
|
||||
|
||||
println!("\n📋 Getting job result...");
|
||||
match client.job_result(&job_id).await? {
|
||||
JobResult::Success { success } => {
|
||||
println!("✅ Success: {}", success);
|
||||
},
|
||||
JobResult::Error { error } => {
|
||||
println!("❌ Error: {}", error);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user