(lifecycle Lifecycle)
| 41 | } |
| 42 | |
| 43 | func (p *pool) RunWithLifecycle(lifecycle Lifecycle) error { |
| 44 | p.mutex.Lock() |
| 45 | if p.running { |
| 46 | p.mutex.Unlock() |
| 47 | panic("bug: pool already running, cannot run again") |
| 48 | } |
| 49 | p.logger.Info(message.NewMessage(message.MServicesStarting, "Services are starting...")) |
| 50 | p.stopComplete = make(chan struct{}, len(p.services)) |
| 51 | p.running = true |
| 52 | p.stopping = false |
| 53 | p.mutex.Unlock() |
| 54 | defer func() { |
| 55 | p.mutex.Lock() |
| 56 | p.running = false |
| 57 | p.mutex.Unlock() |
| 58 | }() |
| 59 | |
| 60 | for _, service := range p.services { |
| 61 | p.runService(service) |
| 62 | } |
| 63 | |
| 64 | stopped := false |
| 65 | startedServices := len(p.services) |
| 66 | finished := false |
| 67 | for i := 0; i < len(p.services); i++ { |
| 68 | select { |
| 69 | case <-p.startupComplete: |
| 70 | case <-p.stopComplete: |
| 71 | stopped = true |
| 72 | startedServices-- |
| 73 | finished = true |
| 74 | } |
| 75 | if finished { |
| 76 | break |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | startedServices = p.processRunning(lifecycle, stopped, startedServices) |
| 81 | |
| 82 | for i := 0; i < startedServices; i++ { |
| 83 | <-p.stopComplete |
| 84 | } |
| 85 | p.logger.Info(message.NewMessage(message.MServicesStopped, "All services have stopped.")) |
| 86 | return p.lastError |
| 87 | } |
| 88 | |
| 89 | func (p *pool) processRunning(lifecycle Lifecycle, stopped bool, startedServices int) int { |
| 90 | if !stopped { |
nothing calls this directly
no test coverage detected