(files []string, numWorkers int, bar *progressbar.ProgressBar)
| 175 | } |
| 176 | |
| 177 | func processBatchConcurrently(files []string, numWorkers int, bar *progressbar.ProgressBar) *BatchStats { |
| 178 | if numWorkers < 1 { |
| 179 | numWorkers = 1 |
| 180 | } |
| 181 | if numWorkers > 20 { |
| 182 | numWorkers = 20 |
| 183 | } |
| 184 | |
| 185 | cfg, err := config.LoadConfig() |
| 186 | if err != nil { |
| 187 | logrus.Debugf("Failed to load config, using defaults: %v", err) |
| 188 | cfg = config.DefaultConfig() |
| 189 | } |
| 190 | |
| 191 | stats := &BatchStats{} |
| 192 | jobs := make(chan FileJob, len(files)) |
| 193 | results := make(chan FileResult, len(files)) |
| 194 | |
| 195 | ctx, cancel := context.WithCancel(context.Background()) |
| 196 | defer cancel() |
| 197 | |
| 198 | var wg sync.WaitGroup |
| 199 | for i := 0; i < numWorkers; i++ { |
| 200 | wg.Add(1) |
| 201 | go worker(ctx, jobs, results, &wg, cfg) |
| 202 | } |
| 203 | |
| 204 | go func() { |
| 205 | defer close(jobs) |
| 206 | for _, file := range files { |
| 207 | select { |
| 208 | case jobs <- FileJob{filepath: file}: |
| 209 | case <-ctx.Done(): |
| 210 | return |
| 211 | } |
| 212 | } |
| 213 | }() |
| 214 | |
| 215 | go func() { |
| 216 | wg.Wait() |
| 217 | close(results) |
| 218 | }() |
| 219 | |
| 220 | for result := range results { |
| 221 | stats.incrementProcessed() |
| 222 | |
| 223 | if bar != nil { |
| 224 | bar.Add(1) |
| 225 | } |
| 226 | |
| 227 | if result.error != nil { |
| 228 | stats.incrementErrors() |
| 229 | if bar != nil { |
| 230 | bar.Describe(filepath.Base(result.filepath) + " - ERROR") |
| 231 | } else { |
| 232 | logrus.Errorf("Failed to process %s: %v", filepath.Base(result.filepath), result.error) |
| 233 | } |
| 234 | } else if result.updated { |
no test coverage detected