| 170 | } |
| 171 | |
| 172 | func (c *StoreReconciler) Run(ctx context.Context) { |
| 173 | reconciliationInterval := c.cfg.ReconciliationInterval.Value() |
| 174 | if reconciliationInterval <= 0 { // avoids a panic |
| 175 | reconciliationInterval = 5 * time.Minute |
| 176 | } |
| 177 | |
| 178 | c.logger.Info(ctx, "starting reconciler", |
| 179 | slog.F("interval", reconciliationInterval), |
| 180 | slog.F("backoff_interval", c.cfg.ReconciliationBackoffInterval.String()), |
| 181 | slog.F("backoff_lookback", c.cfg.ReconciliationBackoffLookback.String()), |
| 182 | slog.F("preset_concurrency", c.reconciliationConcurrency)) |
| 183 | |
| 184 | // Create a child context that will be canceled when: |
| 185 | // 1. The parent context is canceled, OR |
| 186 | // 2. c.cancelFn() is called to trigger shutdown |
| 187 | // nolint:gocritic // Reconciliation Loop needs Prebuilds Orchestrator permissions. |
| 188 | ctx, cancel := context.WithCancelCause(dbauthz.AsPrebuildsOrchestrator(ctx)) |
| 189 | |
| 190 | // If the reconciler was already stopped, exit early and release the context. |
| 191 | // Otherwise, mark it as running and store the cancel function for shutdown. |
| 192 | c.mu.Lock() |
| 193 | if c.stopped || c.running { |
| 194 | c.mu.Unlock() |
| 195 | cancel(nil) |
| 196 | return |
| 197 | } |
| 198 | c.running = true |
| 199 | c.cancelFn = cancel |
| 200 | c.mu.Unlock() |
| 201 | |
| 202 | ticker := c.clock.NewTicker(reconciliationInterval) |
| 203 | defer ticker.Stop() |
| 204 | // Wait for all background goroutines to exit before signaling completion. |
| 205 | var wg sync.WaitGroup |
| 206 | defer func() { |
| 207 | wg.Wait() |
| 208 | c.done <- struct{}{} |
| 209 | }() |
| 210 | |
| 211 | // Start updating metrics in the background. |
| 212 | if c.metrics != nil { |
| 213 | wg.Add(1) |
| 214 | go func() { |
| 215 | defer wg.Done() |
| 216 | c.metrics.BackgroundFetch(ctx, metricsUpdateInterval, metricsUpdateTimeout) |
| 217 | }() |
| 218 | } |
| 219 | |
| 220 | // Publish provisioning jobs outside of database transactions. |
| 221 | // A connection is held while a database transaction is active; PGPubsub also tries to acquire a new connection on |
| 222 | // Publish, so we can exhaust available connections. |
| 223 | // |
| 224 | // A single worker dequeues from the channel, which should be sufficient. |
| 225 | // If any messages are missed due to congestion or errors, provisionerdserver has a backup polling mechanism which |
| 226 | // will periodically pick up any queued jobs (see poll(time.Duration) in coderd/provisionerdserver/acquirer.go). |
| 227 | wg.Add(1) |
| 228 | go func() { |
| 229 | defer wg.Done() |