update: Implement cancellation notification using mpsc::channel

This commit is contained in:
2026-01-10 22:45:01 +09:00
parent 16fda38039
commit a2f53d7879
4 changed files with 192 additions and 148 deletions
+6 -6
View File
@@ -30,10 +30,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("🚀 Starting Worker...");
println!("💡 Will cancel after 2 seconds\n");
// キャンセルトークンを先に取得(ロックを保持しない)
let cancel_token = {
// キャンセルSenderを先に取得(ロックを保持しない)
let cancel_tx = {
let w = worker.lock().await;
w.cancellation_token().clone()
w.cancel_sender()
};
// タスク1: Workerを実行
@@ -43,10 +43,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("📡 Sending request to LLM...");
match w.run("Tell me a very long story about a brave knight. Make it as detailed as possible with many paragraphs.").await {
Ok(WorkerResult::Finished(_)) => {
Ok(WorkerResult::Finished) => {
println!("✅ Task completed normally");
}
Ok(WorkerResult::Paused(_)) => {
Ok(WorkerResult::Paused) => {
println!("⏸️ Task paused");
}
Err(e) => {
@@ -59,7 +59,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
tokio::spawn(async move {
tokio::time::sleep(Duration::from_secs(2)).await;
println!("\n🛑 Cancelling worker...");
cancel_token.cancel();
let _ = cancel_tx.send(()).await;
});
// タスク完了を待つ