Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8248fa3

Browse files
authored
fix(coderd): wake dormant workspace when attempting to start it (#21306)
Relates to#20925This PR modifies the `postWorkspaceBuild` handler to automatically unsetdormancy on a workspace when a start transition is requested.Previously, the client was responsible for unsetting the dormancy on theworkspace prior to posting a workspace build.
1 parentcac6d4c commit8248fa3

File tree

2 files changed

+69
-10
lines changed

2 files changed

+69
-10
lines changed

‎coderd/workspacebuilds.go‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,40 @@ func (api *API) postWorkspaceBuildsInternal(
399399
err:=api.Database.InTx(func(tx database.Store)error {
400400
varerrerror
401401

402+
// #20925: if the workspace is dormant and we are starting the workspace,
403+
// we need to unset that status before inserting a new build.
404+
// This is done inside the transaction for consistency, but it could also be
405+
// done outside the transaction so that an attempt to start a workspace will
406+
// also unset dormancy.
407+
ifworkspace.DormantAt.Valid&&transition==database.WorkspaceTransitionStart {
408+
if_,err:=tx.UpdateWorkspaceDormantDeletingAt(ctx, database.UpdateWorkspaceDormantDeletingAtParams{
409+
ID:workspace.ID,
410+
DormantAt: sql.NullTime{Valid:false},
411+
});err!=nil {
412+
returnhttperror.NewResponseError(http.StatusInternalServerError, codersdk.Response{
413+
Message:"Internal error unsetting workspace dormant status",
414+
Detail:err.Error(),
415+
})
416+
}
417+
// We need to audit this change separately.
418+
updatedWorkspace:=workspace.WorkspaceTable()
419+
updatedWorkspace.DormantAt= sql.NullTime{Valid:false}
420+
auditor:=api.Auditor.Load()
421+
bag:=audit.BaggageFromContext(ctx)
422+
audit.BackgroundAudit(ctx,&audit.BackgroundAuditParams[database.WorkspaceTable]{
423+
Audit:*auditor,
424+
Old:workspace.WorkspaceTable(),
425+
New:updatedWorkspace,
426+
Log:api.Logger,
427+
UserID:apiKey.UserID,
428+
OrganizationID:workspace.OrganizationID,
429+
RequestID:workspace.ID,
430+
IP:bag.IP,
431+
Action:database.AuditActionWrite,
432+
Status:http.StatusOK,
433+
})
434+
}
435+
402436
previousWorkspaceBuild,err=tx.GetLatestWorkspaceBuildByWorkspaceID(ctx,workspace.ID)
403437
iferr!=nil&&!xerrors.Is(err,sql.ErrNoRows) {
404438
api.Logger.Error(ctx,"failed fetching previous workspace build",slog.F("workspace_id",workspace.ID),slog.Error(err))

‎coderd/workspaces_test.go‎

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4244,10 +4244,16 @@ func TestWorkspaceDormant(t *testing.T) {
42444244
require.True(t,workspace.LastUsedAt.After(lastUsedAt))
42454245
})
42464246

4247-
t.Run("CannotStart",func(t*testing.T) {
4247+
// #20925: this test originally validated that you could **not** start a dormant workspace.
4248+
// The client was required to explicitly update the dormancy status before starting.
4249+
// This led to a 'whack-a-mole' situation where various code paths that create a workspace build
4250+
// would need to special case dormant workspaces.
4251+
// Now, a dormant workspace will automatically 'wake up' on start.
4252+
t.Run("StartWakesUpDormantWorkspace",func(t*testing.T) {
42484253
t.Parallel()
42494254
var (
4250-
client=coderdtest.New(t,&coderdtest.Options{IncludeProvisionerDaemon:true})
4255+
auditor=audit.NewMock()
4256+
client=coderdtest.New(t,&coderdtest.Options{IncludeProvisionerDaemon:true,Auditor:auditor})
42514257
user=coderdtest.CreateFirstUser(t,client)
42524258
version=coderdtest.CreateTemplateVersion(t,client,user.OrganizationID,nil)
42534259
_=coderdtest.AwaitTemplateVersionJobCompleted(t,client,version.ID)
@@ -4267,18 +4273,37 @@ func TestWorkspaceDormant(t *testing.T) {
42674273
// Should be able to stop a workspace while it is dormant.
42684274
coderdtest.MustTransitionWorkspace(t,client,workspace.ID,codersdk.WorkspaceTransitionStart,codersdk.WorkspaceTransitionStop)
42694275

4270-
// Should not be able to start a workspace while it is dormant.
4271-
_,err=client.CreateWorkspaceBuild(ctx,workspace.ID, codersdk.CreateWorkspaceBuildRequest{
4276+
// Reset the auditor
4277+
auditor.ResetLogs()
4278+
// Assert test invariant: workspace is dormant.
4279+
workspace,err=client.Workspace(ctx,workspace.ID)
4280+
require.NoError(t,err,"fetch dormant workspace")
4281+
ifassert.NotNil(t,workspace.DormantAt,"workspace must be dormant") {
4282+
require.WithinDuration(t,*workspace.DormantAt,time.Now(),10*time.Second)
4283+
}
4284+
// Starting a dormant workspace should 'wake' it.
4285+
wb,err:=client.CreateWorkspaceBuild(ctx,workspace.ID, codersdk.CreateWorkspaceBuildRequest{
42724286
TemplateVersionID:template.ActiveVersionID,
42734287
Transition:codersdk.WorkspaceTransition(database.WorkspaceTransitionStart),
42744288
})
4275-
require.Error(t,err)
4276-
4277-
err=client.UpdateWorkspaceDormancy(ctx,workspace.ID, codersdk.UpdateWorkspaceDormancy{
4278-
Dormant:false,
4279-
})
42804289
require.NoError(t,err)
4281-
coderdtest.MustTransitionWorkspace(t,client,workspace.ID,codersdk.WorkspaceTransitionStop,codersdk.WorkspaceTransitionStart)
4290+
coderdtest.AwaitWorkspaceBuildJobCompleted(t,client,wb.ID)
4291+
4292+
// After starting, the workspace should no longer be dormant.
4293+
updatedWs,err:=client.Workspace(ctx,workspace.ID)
4294+
require.NoError(t,err,"fetch updated workspace")
4295+
require.Nil(t,updatedWs.DormantAt)
4296+
4297+
// There should be an audit log for both the dormancy update and the start.
4298+
require.Len(t,auditor.AuditLogs(),2)
4299+
require.True(t,auditor.Contains(t, database.AuditLog{
4300+
Action:database.AuditActionWrite,
4301+
ResourceType:database.ResourceTypeWorkspace,
4302+
}))
4303+
require.True(t,auditor.Contains(t, database.AuditLog{
4304+
Action:database.AuditActionStart,
4305+
ResourceType:database.ResourceTypeWorkspaceBuild,
4306+
}))
42824307
})
42834308
}
42844309

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp