refreshCachedWorkspace periodically updates the cached workspace fields. This ensures that changes like prebuild claims (which modify owner_id, name, etc.) are eventually reflected in the cache without requiring agent reconnection.
(ctx context.Context)
| 301 | // This ensures that changes like prebuild claims (which modify owner_id, name, etc.) |
| 302 | // are eventually reflected in the cache without requiring agent reconnection. |
| 303 | func (a *API) refreshCachedWorkspace(ctx context.Context) { |
| 304 | ws, err := a.opts.Database.GetWorkspaceByID(ctx, a.opts.WorkspaceID) |
| 305 | if err != nil { |
| 306 | // Do not clear the cache on transient DB errors. Stale data is |
| 307 | // preferable to no data, which forces callers to fall back to |
| 308 | // expensive queries like GetWorkspaceByAgentID. |
| 309 | a.opts.Log.Warn(ctx, "failed to refresh cached workspace fields", slog.Error(err)) |
| 310 | return |
| 311 | } |
| 312 | |
| 313 | if ws.IsPrebuild() { |
| 314 | return |
| 315 | } |
| 316 | |
| 317 | // If we still have the same values, skip the update and logging calls. |
| 318 | if a.cachedWorkspaceFields.identity.Equal(database.WorkspaceIdentityFromWorkspace(ws)) { |
| 319 | return |
| 320 | } |
| 321 | // Update fields that can change during workspace lifecycle (e.g., AutostartSchedule) |
| 322 | a.cachedWorkspaceFields.UpdateValues(ws) |
| 323 | |
| 324 | a.opts.Log.Debug(ctx, "refreshed cached workspace fields", |
| 325 | slog.F("workspace_id", ws.ID), |
| 326 | slog.F("owner_id", ws.OwnerID), |
| 327 | slog.F("name", ws.Name)) |
| 328 | } |
| 329 | |
| 330 | // startCacheRefreshLoop runs a background goroutine that periodically refreshes |
| 331 | // the cached workspace fields. This is primarily needed to handle prebuild claims |
no test coverage detected