MCPcopy Index your code
hub / github.com/coder/coder / Run

Method Run

enterprise/coderd/prebuilds/reconcile.go:172–274  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

170}
171
172func (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()

Callers 1

TestRunLoopFunction · 0.95

Calls 15

ReconcileAllMethod · 0.95
AsPrebuildsOrchestratorFunction · 0.92
PostJobFunction · 0.92
BackgroundFetchMethod · 0.80
ErrMethod · 0.80
StopMethod · 0.65
WaitMethod · 0.65
AddMethod · 0.65
ValueMethod · 0.45
InfoMethod · 0.45
StringMethod · 0.45
LockMethod · 0.45

Tested by 1

TestRunLoopFunction · 0.76