@@ -2,7 +2,6 @@ package agentapi
22
33import (
44 "context"
5- "database/sql"
65 "io"
76 "net"
87 "net/url"
@@ -61,10 +60,6 @@ type CachedWorkspaceFields struct {
6160 Name string
6261 OwnerUsername string
6362 TemplateName string
64-
65- // Lifecycle fields needed for stats reporting
66- AutostartSchedule sql.NullString
67- DormantAt sql.NullTime
6863}
6964
7065// API implements the DRPC agent API interface from agent/proto. This struct is
@@ -132,23 +127,6 @@ func New(opts Options, workspace database.Workspace) *API {
132127
133128 api := & API {
134129 opts : opts ,
135- cachedWorkspaceFields : CachedWorkspaceFields {
136- ID : workspace .ID ,
137- OwnerID : workspace .OwnerID ,
138- OrganizationID : workspace .OrganizationID ,
139- TemplateID : workspace .TemplateID ,
140- Name : workspace .Name ,
141- OwnerUsername : workspace .OwnerUsername ,
142- TemplateName : workspace .TemplateName ,
143- AutostartSchedule : sql.NullString {
144- String : workspace .AutostartSchedule .String ,
145- Valid : workspace .AutostartSchedule .Valid ,
146- },
147- DormantAt : sql.NullTime {
148- Time : workspace .DormantAt .Time ,
149- Valid : workspace .DormantAt .Valid ,
150- },
151- },
152130 mu : sync.Mutex {},
153131 }
154132
@@ -164,6 +142,20 @@ func New(opts Options, workspace database.Workspace) *API {
164142 WorkspaceID : opts .WorkspaceID ,
165143 }
166144
145+ // Don't cache details for prebuilds, though the cached fields will eventually be updated
146+ // by the refresh routine once the prebuild workspace is claimed.
147+ if ! workspace .IsPrebuild () {
148+ api .cachedWorkspaceFields = CachedWorkspaceFields {
149+ ID : workspace .ID ,
150+ OwnerID : workspace .OwnerID ,
151+ OrganizationID : workspace .OrganizationID ,
152+ TemplateID : workspace .TemplateID ,
153+ Name : workspace .Name ,
154+ OwnerUsername : workspace .OwnerUsername ,
155+ TemplateName : workspace .TemplateName ,
156+ }
157+ }
158+
167159 api .AnnouncementBannerAPI = & AnnouncementBannerAPI {
168160 appearanceFetcher : opts .AppearanceFetcher ,
169161 }
@@ -322,8 +314,6 @@ func (a *API) workspace() database.Workspace {
322314 Name : a .cachedWorkspaceFields .Name ,
323315 OwnerUsername : a .cachedWorkspaceFields .OwnerUsername ,
324316 TemplateName : a .cachedWorkspaceFields .TemplateName ,
325- AutostartSchedule : a .cachedWorkspaceFields .AutostartSchedule ,
326- DormantAt : a .cachedWorkspaceFields .DormantAt ,
327317 }
328318}
329319
@@ -345,12 +335,14 @@ func (a *API) refreshCachedWorkspace(ctx context.Context) {
345335 return
346336 }
347337
338+ if ws .IsPrebuild () {
339+ a .opts .Log .Debug (ctx , "workspace is a prebuild, not caching in AgentAPI" )
340+ return
341+ }
342+
348343 // Update fields that can change during workspace lifecycle (e.g., prebuild claim)
349- a .cachedWorkspaceFields .OwnerID = ws .OwnerID
350- a .cachedWorkspaceFields .Name = ws .Name
351- a .cachedWorkspaceFields .OwnerUsername = ws .OwnerUsername
352- a .cachedWorkspaceFields .AutostartSchedule = ws .AutostartSchedule
353- a .cachedWorkspaceFields .DormantAt = ws .DormantAt
344+ a .cachedWorkspaceFields = CachedWorkspaceFields {}
345+
354346
355347 a .opts .Log .Debug (ctx , "refreshed cached workspace fields" ,
356348 slog .F ("workspace_id" , ws .ID ),
@@ -377,6 +369,8 @@ func (a *API) startCacheRefreshLoop(ctx context.Context) {
377369 slog .F ("workspace_id" , a .cachedWorkspaceFields .ID ),
378370 slog .F ("owner_id" , a .cachedWorkspaceFields .OwnerUsername ),
379371 slog .F ("name" , a .cachedWorkspaceFields .Name ))
372+ a .mu .Lock ()
373+ defer a .mu .Unlock ()
380374 a .cachedWorkspaceFields = CachedWorkspaceFields {}
381375}
382376
0 commit comments