startCacheRefreshLoop runs a background goroutine that periodically refreshes the cached workspace fields. This is primarily needed to handle prebuild claims where the owner_id and other fields change while the agent connection persists.
(ctx context.Context)
| 331 | // the cached workspace fields. This is primarily needed to handle prebuild claims |
| 332 | // where the owner_id and other fields change while the agent connection persists. |
| 333 | func (a *API) startCacheRefreshLoop(ctx context.Context) { |
| 334 | // Refresh every 5 minutes. This provides a reasonable balance between: |
| 335 | // - Keeping cache fresh for prebuild claims and other workspace updates |
| 336 | // - Minimizing unnecessary database queries |
| 337 | ticker := a.opts.Clock.TickerFunc(ctx, workspaceCacheRefreshInterval, func() error { |
| 338 | a.refreshCachedWorkspace(ctx) |
| 339 | return nil |
| 340 | }, "cache_refresh") |
| 341 | |
| 342 | // We need to wait on the ticker exiting. |
| 343 | _ = ticker.Wait() |
| 344 | |
| 345 | a.opts.Log.Debug(ctx, "cache refresh loop exited, invalidating the workspace cache on agent API", |
| 346 | slog.F("workspace_id", a.cachedWorkspaceFields.identity.ID), |
| 347 | slog.F("owner_id", a.cachedWorkspaceFields.identity.OwnerUsername), |
| 348 | slog.F("name", a.cachedWorkspaceFields.identity.Name)) |
| 349 | a.cachedWorkspaceFields.Clear() |
| 350 | } |
| 351 | |
| 352 | func (a *API) publishWorkspaceUpdate(ctx context.Context, agentID uuid.UUID, kind wspubsub.WorkspaceEventKind) error { |
| 353 | a.opts.PublishWorkspaceUpdateFn(ctx, a.opts.OwnerID, wspubsub.WorkspaceEvent{ |
no test coverage detected