cleanupExample 任务清理示例
(ctx context.Context)
| 167 | |
| 168 | // cleanupExample 任务清理示例 |
| 169 | func cleanupExample(ctx context.Context) { |
| 170 | executor := tools.NewLongRunningExecutor() |
| 171 | tool := NewMockDataProcessingTool(executor) |
| 172 | |
| 173 | // 创建一些任务 |
| 174 | for range 3 { |
| 175 | taskID, _ := tool.StartAsync(ctx, map[string]any{ |
| 176 | "data_size": 100, |
| 177 | "delay_ms": 100, |
| 178 | }) |
| 179 | _, _ = tools.WaitForCompletion(executor, taskID, 50*time.Millisecond, 2*time.Second) |
| 180 | } |
| 181 | |
| 182 | fmt.Println("✅ Created 3 completed tasks") |
| 183 | |
| 184 | // 清理 1 秒前的任务 |
| 185 | deleted := executor.Cleanup(time.Now().Add(-1 * time.Second)) |
| 186 | fmt.Printf("✅ Cleaned up %d old tasks\n", deleted) |
| 187 | |
| 188 | // 验证 |
| 189 | remaining := executor.ListTasks(nil) |
| 190 | fmt.Printf("✅ Remaining tasks: %d\n", len(remaining)) |
| 191 | } |
| 192 | |
| 193 | // ============================================================ |
| 194 | // 模拟工具实现 |
no test coverage detected