MCPcopy Create free account
hub / github.com/astercloud/aster / progressTrackingExample

Function progressTrackingExample

examples/long-running-tools/main.go:71–103  ·  view source on GitHub ↗

progressTrackingExample 进度跟踪示例

(ctx context.Context)

Source from the content-addressed store, hash-verified

69
70// progressTrackingExample 进度跟踪示例
71func 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 任务取消示例
106func cancellationExample(ctx context.Context) {

Callers 1

mainFunction · 0.85

Calls 7

StartAsyncMethod · 0.95
GetStatusMethod · 0.95
NewLongRunningExecutorFunction · 0.92
NewMockFileUploadToolFunction · 0.85
PrintfMethod · 0.80
StopMethod · 0.65
IsTerminalMethod · 0.45

Tested by

no test coverage detected