checker_parallel.go 958 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package executor
  2. import (
  3. "sync"
  4. fw "git.buran.team/main/fairwind"
  5. )
  6. type ParallelChecker struct {
  7. global *Global
  8. ticket *Ticket
  9. checks []Check
  10. }
  11. func NewParallelChecker(global *Global, ticket *Ticket, checks []Check) (*ParallelChecker, error) {
  12. return &ParallelChecker{
  13. global: global,
  14. ticket: ticket,
  15. checks: checks,
  16. }, nil
  17. }
  18. func (this *ParallelChecker) Check() *TaskResult {
  19. this.global.Log.Debug("beginning checks parallel", fw.LogValue("uuid", this.ticket.UUID))
  20. defer this.global.Log.Debug("parallel checks ended", fw.LogValue("uuid", this.ticket.UUID))
  21. var mutex sync.Mutex
  22. report := map[string]CheckResult{}
  23. var waitGroup sync.WaitGroup
  24. waitGroup.Add(len(this.checks))
  25. for _, check := range this.checks {
  26. go func(check Check) {
  27. defer waitGroup.Done()
  28. result := check.Check()
  29. mutex.Lock()
  30. report[check.ID()] = result
  31. mutex.Unlock()
  32. }(check)
  33. }
  34. waitGroup.Wait()
  35. return NewTaskResultSuccess(report)
  36. }