#[tokio::main]
async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
use asynq::inspector::{Inspector, InspectorTrait};
println!("🔍 Task Cancellation Example\n");
let redis_config = asynq::backend::RedisConnectionType::single("redis://localhost:6379")?;
let inspector = std::sync::Arc::new(Inspector::new(redis_config).await?);
println!("📋 Listing active tasks...\n");
let active_tasks = inspector.list_active_tasks("default").await?;
if active_tasks.is_empty() {
println!("⚠️ No active tasks found. Please run:");
println!(" cargo run --example enqueue_long_tasks");
println!(" cargo run --example server_with_cancellation");
return Ok(());
}
println!("Found {} active task(s):\n", active_tasks.len());
for (i, task) in active_tasks.iter().enumerate() {
println!(" {}. Task ID: {}", i + 1, task.id);
println!(" Type: {}", task.task_type);
println!(" Queue: {}", task.queue);
println!(" State: {:?}", task.state);
println!(" Next: {:?}", task.next_process_at);
println!();
}
if let Some(task) = active_tasks.first() {
println!("🎯 Cancelling task: {}\n", task.id);
match inspector.cancel_processing(&task.id).await {
Ok(_) => {
println!("✅ Cancellation request sent successfully!");
println!(" The task will be cancelled shortly.");
println!(" Check the server logs to see the cancellation.");
}
Err(e) => {
println!("❌ Failed to cancel task: {e}");
}
}
}
println!("\n⏳ Waiting 2 seconds...\n");
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
println!("📋 Listing active tasks again...\n");
let active_tasks_after = inspector.list_active_tasks("default").await?;
println!("Active tasks now: {}\n", active_tasks_after.len());
println!("💡 Tips:");
println!(" - Cancelled tasks are gracefully stopped");
println!(" - The cancellation event is sent via Redis pub/sub");
println!(" - The server receives the event and cancels the running task");
Ok(())
}