progressTrackingExample 进度跟踪示例
(ctx context.Context)
| 69 | |
| 70 | // progressTrackingExample 进度跟踪示例 |
| 71 | func progressTrackingExample(ctx context.Context) { |
| 72 | executor := tools.NewLongRunningExecutor() |
| 73 | tool := NewMockFileUploadTool(executor) |
| 74 | |
| 75 | // 启动任务 |
| 76 | taskID, _ := tool.StartAsync(ctx, map[string]any{ |
| 77 | "file_size": 10000, |
| 78 | "chunk_size": 1000, |
| 79 | }) |
| 80 | |
| 81 | fmt.Printf("✅ Upload started: %s\n", taskID) |
| 82 | |
| 83 | // 轮询进度 |
| 84 | ticker := time.NewTicker(200 * time.Millisecond) |
| 85 | defer ticker.Stop() |
| 86 | |
| 87 | for range 10 { |
| 88 | <-ticker.C |
| 89 | |
| 90 | status, err := executor.GetStatus(ctx, taskID) |
| 91 | if err != nil { |
| 92 | log.Fatalf("Failed to get status: %v", err) |
| 93 | } |
| 94 | |
| 95 | fmt.Printf(" Progress: %.1f%% (%s)\n", status.Progress*100, status.State) |
| 96 | |
| 97 | if status.State.IsTerminal() { |
| 98 | break |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | fmt.Println("✅ Upload completed") |
| 103 | } |
| 104 | |
| 105 | // cancellationExample 任务取消示例 |
| 106 | func cancellationExample(ctx context.Context) { |
no test coverage detected