use std::time::Duration;
use uuid::Uuid;
use conductor::{Configuration, OrkesClients, StartWorkflowRequest, WorkflowDef, WorkflowTask};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
let config = Configuration::default();
let clients = OrkesClients::new(config)?;
let workflow_client = clients.get_workflow_client();
let metadata_client = clients.get_metadata_client();
let http_poll = WorkflowTask::http_poll(
"http_poll_ref",
"https://orkes-api-tester.orkesconductor.com/api",
)
.with_polling_strategy("EXPONENTIAL_BACKOFF")
.with_polling_interval(1)
.with_termination_condition("(function(){ return $.output.response.body.randomInt < 10;})();");
let workflow_name = format!("http_poll_example_{}", Uuid::new_v4());
let workflow_def = WorkflowDef::new(&workflow_name)
.with_description("HTTP Poll example - polls until condition met")
.with_version(1)
.with_task(http_poll)
.with_output_param("result", "${http_poll_ref.output}");
metadata_client
.register_or_update_workflow_def(&workflow_def, true)
.await?;
println!("Workflow registered: {workflow_name}");
println!("\nStarting HTTP Poll workflow...");
println!("The workflow will poll until randomInt < 10\n");
let request = StartWorkflowRequest::new(&workflow_name).with_version(1);
let result = workflow_client
.execute_workflow(&request, Duration::from_secs(60))
.await?;
println!("Workflow status: {:?}", result.status);
println!("\nFinal result:");
println!("{}", serde_json::to_string_pretty(&result.output)?);
metadata_client
.delete_workflow_def(&workflow_name, 1)
.await?;
println!("\nWorkflow definition cleaned up.");
Ok(())
}