(ctx context.Context)
| 156 | } |
| 157 | |
| 158 | func (r *Runner) watchWorkspaceUpdates(ctx context.Context) error { |
| 159 | shouldMarkConnectedDone := true |
| 160 | defer func() { |
| 161 | if shouldMarkConnectedDone { |
| 162 | r.cfg.ConnectedWaitGroup.Done() |
| 163 | } |
| 164 | }() |
| 165 | updates, err := r.client.watchWorkspace(ctx, r.workspaceID) |
| 166 | if err != nil { |
| 167 | return xerrors.Errorf("watch workspace: %w", err) |
| 168 | } |
| 169 | shouldMarkConnectedDone = false |
| 170 | r.cfg.ConnectedWaitGroup.Done() |
| 171 | defer func() { |
| 172 | r.mu.Lock() |
| 173 | defer r.mu.Unlock() |
| 174 | r.cfg.Metrics.MissingStatusUpdatesTotal. |
| 175 | WithLabelValues(r.cfg.MetricLabelValues...). |
| 176 | Add(float64(len(r.reportTimes))) |
| 177 | }() |
| 178 | for { |
| 179 | select { |
| 180 | case <-ctx.Done(): |
| 181 | return ctx.Err() |
| 182 | case workspace := <-updates: |
| 183 | if workspace.LatestAppStatus == nil { |
| 184 | continue |
| 185 | } |
| 186 | msgNo, ok := parseStatusMessage(workspace.LatestAppStatus.Message) |
| 187 | if !ok { |
| 188 | continue |
| 189 | } |
| 190 | |
| 191 | r.mu.Lock() |
| 192 | reportTime, ok := r.reportTimes[msgNo] |
| 193 | delete(r.reportTimes, msgNo) |
| 194 | allDone := r.doneReporting && len(r.reportTimes) == 0 |
| 195 | r.mu.Unlock() |
| 196 | |
| 197 | if !ok { |
| 198 | return xerrors.Errorf("report time not found for message %d", msgNo) |
| 199 | } |
| 200 | latency := r.clock.Since(reportTime, "watchWorkspaceUpdates") |
| 201 | r.cfg.Metrics.TaskStatusToWorkspaceUpdateLatencySeconds. |
| 202 | WithLabelValues(r.cfg.MetricLabelValues...). |
| 203 | Observe(latency.Seconds()) |
| 204 | if allDone { |
| 205 | return nil |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | func (r *Runner) reportTaskStatus(ctx context.Context) error { |
| 212 | defer func() { |
no test coverage detected