use conductor::{
client::ConductorClient,
configuration::Configuration,
error::Result,
models::{StartWorkflowRequest, Task, WorkflowDef, WorkflowTask},
schema::generate_schema,
worker::{FnWorker, TaskHandler, WorkerOutput},
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use tracing::info;
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
struct ConfiguredTaskInput {
name: String,
priority: i32,
#[serde(default)]
tags: Vec<String>,
}
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
struct ConfiguredTaskOutput {
result: String,
processed_at: String,
worker_id: String,
}
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::from_default_env()
.add_directive("conductor=info".parse().unwrap()),
)
.init();
let config = Configuration::default();
info!("Connecting to Conductor at {}", config.server_api_url);
let client = ConductorClient::new(config.clone())?;
register_workflow(&client).await?;
let mut handler = TaskHandler::new(config.clone())?;
let configured_worker = FnWorker::new("configured_task", |task: Task| async move {
let name = task
.get_input_string("name")
.unwrap_or_else(|| "Unknown".to_owned());
let priority: i32 = task.get_input("priority").unwrap_or(5);
info!(
"[Configured Worker] Processing: name={}, priority={}",
name, priority
);
let delay = std::time::Duration::from_millis((100 * (11 - priority)) as u64);
tokio::time::sleep(delay).await;
Ok(WorkerOutput::completed_with_result(serde_json::json!({
"result": format!("Processed {} with priority {}", name, priority),
"processed_at": chrono::Utc::now().to_rfc3339(),
"worker_id": hostname::get().map_or_else(|_| "unknown".to_owned(), |h| h.to_string_lossy().into_owned())
})))
})
.with_thread_count(5) .with_poll_interval_millis(200) .with_domain("default") .with_identity("rust-worker-configured") .with_input_schema(generate_schema::<ConfiguredTaskInput>(true)) .with_output_schema(generate_schema::<ConfiguredTaskOutput>(true));
let schema_worker = FnWorker::new("schema_task", |task: Task| async move {
let input: ConfiguredTaskInput = task.get_input("input").unwrap_or(ConfiguredTaskInput {
name: "default".to_owned(),
priority: 5,
tags: vec![],
});
info!(
"[Schema Worker] Input: name={}, priority={}, tags={:?}",
input.name, input.priority, input.tags
);
Ok(WorkerOutput::completed_with_result(serde_json::json!({
"result": format!("Processed {}", input.name),
"processed_at": chrono::Utc::now().to_rfc3339(),
"worker_id": "schema-worker"
})))
})
.with_thread_count(3)
.with_input_schema_from::<ConfiguredTaskInput>(true)
.with_output_schema_from::<ConfiguredTaskOutput>(true);
let minimal_worker = FnWorker::new("minimal_task", |task: Task| async move {
let data = task.get_input_string("data").unwrap_or_default();
info!("[Minimal Worker] Processing: {}", data);
Ok(WorkerOutput::completed_with_result(format!(
"Processed: {data}"
)))
});
let domain_worker = FnWorker::new("domain_task", |_task: Task| async move {
info!("[Domain Worker] Processing task from domain");
Ok(WorkerOutput::completed_with_result("domain task completed"))
})
.with_domain("special_domain") .with_thread_count(2);
handler.add_worker(configured_worker);
handler.add_worker(schema_worker);
handler.add_worker(minimal_worker);
handler.add_worker(domain_worker);
info!("\nStarting task handler...");
info!("Configuration hierarchy:");
info!(" 1. Worker-specific env: CONDUCTOR_WORKER_<NAME>_<PROPERTY>");
info!(" 2. Global env: CONDUCTOR_WORKER_ALL_<PROPERTY>");
info!(" 3. Code defaults");
handler.start().await?;
println!("\n{}", "=".repeat(70));
println!("Worker Configuration Example");
println!("{}", "=".repeat(70));
println!("\nRegistered Workers:");
println!(" 1. configured_task - Full configuration with JSON Schema");
println!(" - thread_count: 5 (or CONDUCTOR_WORKER_CONFIGURED_TASK_THREAD_COUNT)");
println!(" - poll_interval: 200ms");
println!(" - domain: default");
println!(" - input/output schema: enabled (strict mode)");
println!("\n 2. schema_task - Type-derived JSON Schema");
println!(" - Uses with_input_schema_from<T>()");
println!(" - Automatic schema from Rust struct");
println!("\n 3. minimal_task - Default configuration");
println!(" - thread_count: 1 (default)");
println!(" - poll_interval: 100ms (default)");
println!(" - no domain, no schema");
println!("\n 4. domain_task - Domain-specific routing");
println!(" - domain: special_domain");
println!(" - Only processes tasks with matching domain");
println!("\nEnvironment Variable Examples:");
println!(" CONDUCTOR_WORKER_ALL_THREAD_COUNT=10");
println!(" CONDUCTOR_WORKER_CONFIGURED_TASK_THREAD_COUNT=20");
println!(" CONDUCTOR_WORKER_CONFIGURED_TASK_PAUSED=true");
println!("{}", "=".repeat(70));
let workflow_client = client.workflow_client();
let request = StartWorkflowRequest::new("worker_config_demo")
.with_version(1)
.with_input_value("name", "Test User")
.with_input_value("priority", 8);
info!("\nStarting test workflow...");
let workflow_id = workflow_client.start_workflow(&request).await?;
info!("Workflow started: {}", workflow_id);
info!("View at: {}", config.execution_url(&workflow_id));
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
let workflow = workflow_client.get_workflow(&workflow_id, false).await?;
info!("Workflow status: {:?}", workflow.status);
info!("\nWorkers are running. Press Ctrl+C to stop...");
tokio::signal::ctrl_c().await.ok();
handler.stop().await?;
info!("Done!");
Ok(())
}
async fn register_workflow(client: &ConductorClient) -> Result<()> {
let metadata = client.metadata_client();
let workflow = WorkflowDef::new("worker_config_demo")
.with_description("Demonstrates worker configuration options")
.with_version(1)
.with_task(
WorkflowTask::simple("configured_task", "configured_ref")
.with_input_param("name", "${workflow.input.name}")
.with_input_param("priority", "${workflow.input.priority}"),
)
.with_task(
WorkflowTask::simple("schema_task", "schema_ref").with_input_param(
"input",
serde_json::json!({
"name": "${workflow.input.name}",
"priority": "${workflow.input.priority}",
"tags": ["demo", "config"]
}),
),
)
.with_task(
WorkflowTask::simple("minimal_task", "minimal_ref")
.with_input_param("data", "simple data"),
)
.with_output_param("configured_result", "${configured_ref.output}")
.with_output_param("schema_result", "${schema_ref.output}")
.with_output_param("minimal_result", "${minimal_ref.output}");
info!("Registering workflow: {}", workflow.name);
metadata
.register_or_update_workflow_def(&workflow, true)
.await?;
Ok(())
}