Start will cause the detector to detect and unhang provisioner jobs on every tick from its channel. It will stop when its context is Done, or when its channel is closed. Start should only be called once.
()
| 140 | // |
| 141 | // Start should only be called once. |
| 142 | func (d *Detector) Start() { |
| 143 | go func() { |
| 144 | defer close(d.done) |
| 145 | defer d.cancel() |
| 146 | |
| 147 | for { |
| 148 | select { |
| 149 | case <-d.ctx.Done(): |
| 150 | return |
| 151 | case t, ok := <-d.tick: |
| 152 | if !ok { |
| 153 | return |
| 154 | } |
| 155 | stats := d.run(t) |
| 156 | if stats.Error != nil && !xerrors.As(stats.Error, &acquireLockError{}) { |
| 157 | d.log.Warn(d.ctx, "error running workspace build hang detector once", slog.Error(stats.Error)) |
| 158 | } |
| 159 | if d.stats != nil { |
| 160 | select { |
| 161 | case <-d.ctx.Done(): |
| 162 | return |
| 163 | case d.stats <- stats: |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | }() |
| 169 | } |
| 170 | |
| 171 | // Wait will block until the detector is stopped. |
| 172 | func (d *Detector) Wait() { |