@@ -20,7 +20,6 @@ import (
20
20
"golang.org/x/xerrors"
21
21
22
22
"github.com/coder/coder/v2/coderd/database"
23
- "github.com/coder/coder/v2/coderd/database/db2sdk"
24
23
"github.com/coder/coder/v2/coderd/database/dbtime"
25
24
"github.com/coder/coder/v2/coderd/httpapi"
26
25
"github.com/coder/coder/v2/coderd/rbac"
@@ -604,16 +603,6 @@ func (q *FakeQuerier) getGroupByIDNoLock(_ context.Context, id uuid.UUID) (datab
604
603
return database.Group {}, sql .ErrNoRows
605
604
}
606
605
607
- // isNull is only used in dbfake, so reflect is ok. Use this to make the logic
608
- // look more similar to the postgres.
609
- func isNull (v interface {}) bool {
610
- return ! isNotNull (v )
611
- }
612
-
613
- func isNotNull (v interface {}) bool {
614
- return reflect .ValueOf (v ).FieldByName ("Valid" ).Bool ()
615
- }
616
-
617
606
// ErrUnimplemented is returned by methods only used by the enterprise/tailnet.pgCoord. This coordinator explicitly
618
607
// depends on postgres triggers that announce changes on the pubsub. Implementing support for this in the fake
619
608
// database would strongly couple the FakeQuerier to the pubsub, which is undesirable. Furthermore, it makes little
@@ -695,6 +684,36 @@ func minTime(t, u time.Time) time.Time {
695
684
return u
696
685
}
697
686
687
+ func provisonerJobStatus (j database.ProvisionerJob ) database.ProvisionerJobStatus {
688
+ if isNotNull (j .CompletedAt ) {
689
+ if j .Error .String != "" {
690
+ return database .ProvisionerJobStatusFailed
691
+ }
692
+ if isNotNull (j .CanceledAt ) {
693
+ return database .ProvisionerJobStatusCanceled
694
+ }
695
+ return database .ProvisionerJobStatusSucceeded
696
+ }
697
+
698
+ if isNotNull (j .CanceledAt ) {
699
+ return database .ProvisionerJobStatusCanceling
700
+ }
701
+ if isNull (j .StartedAt ) {
702
+ return database .ProvisionerJobStatusPending
703
+ }
704
+ return database .ProvisionerJobStatusRunning
705
+ }
706
+
707
+ // isNull is only used in dbfake, so reflect is ok. Use this to make the logic
708
+ // look more similar to the postgres.
709
+ func isNull (v interface {}) bool {
710
+ return ! isNotNull (v )
711
+ }
712
+
713
+ func isNotNull (v interface {}) bool {
714
+ return reflect .ValueOf (v ).FieldByName ("Valid" ).Bool ()
715
+ }
716
+
698
717
func (* FakeQuerier ) AcquireLock (_ context.Context , _ int64 ) error {
699
718
return xerrors .New ("AcquireLock must only be called within a transaction" )
700
719
}
@@ -748,6 +767,7 @@ func (q *FakeQuerier) AcquireProvisionerJob(_ context.Context, arg database.Acqu
748
767
provisionerJob .StartedAt = arg .StartedAt
749
768
provisionerJob .UpdatedAt = arg .StartedAt .Time
750
769
provisionerJob .WorkerID = arg .WorkerID
770
+ provisionerJob .JobStatus = provisonerJobStatus (provisionerJob )
751
771
q .provisionerJobs [index ] = provisionerJob
752
772
return provisionerJob , nil
753
773
}
@@ -4077,7 +4097,7 @@ func (q *FakeQuerier) GetWorkspacesEligibleForTransition(ctx context.Context, no
4077
4097
if err != nil {
4078
4098
return nil , xerrors .Errorf ("get provisioner job by ID: %w" , err )
4079
4099
}
4080
- if db2sdk .ProvisionerJobStatus (job ) == codersdk .ProvisionerJobFailed {
4100
+ if codersdk .ProvisionerJobStatus (job . JobStatus ) == codersdk .ProvisionerJobFailed {
4081
4101
workspaces = append (workspaces , workspace )
4082
4102
continue
4083
4103
}
@@ -4464,6 +4484,7 @@ func (q *FakeQuerier) InsertProvisionerJob(_ context.Context, arg database.Inser
4464
4484
Input : arg .Input ,
4465
4485
Tags : arg .Tags ,
4466
4486
}
4487
+ job .JobStatus = provisonerJobStatus (job )
4467
4488
q .provisionerJobs = append (q .provisionerJobs , job )
4468
4489
return job , nil
4469
4490
}
@@ -5393,6 +5414,7 @@ func (q *FakeQuerier) UpdateProvisionerJobByID(_ context.Context, arg database.U
5393
5414
continue
5394
5415
}
5395
5416
job .UpdatedAt = arg .UpdatedAt
5417
+ job .JobStatus = provisonerJobStatus (job )
5396
5418
q .provisionerJobs [index ] = job
5397
5419
return nil
5398
5420
}
@@ -5413,6 +5435,7 @@ func (q *FakeQuerier) UpdateProvisionerJobWithCancelByID(_ context.Context, arg
5413
5435
}
5414
5436
job .CanceledAt = arg .CanceledAt
5415
5437
job .CompletedAt = arg .CompletedAt
5438
+ job .JobStatus = provisonerJobStatus (job )
5416
5439
q .provisionerJobs [index ] = job
5417
5440
return nil
5418
5441
}
@@ -5435,6 +5458,7 @@ func (q *FakeQuerier) UpdateProvisionerJobWithCompleteByID(_ context.Context, ar
5435
5458
job .CompletedAt = arg .CompletedAt
5436
5459
job .Error = arg .Error
5437
5460
job .ErrorCode = arg .ErrorCode
5461
+ job .JobStatus = provisonerJobStatus (job )
5438
5462
q .provisionerJobs [index ] = job
5439
5463
return nil
5440
5464
}
@@ -6604,61 +6628,30 @@ func (q *FakeQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg database.
6604
6628
// This logic should match the logic in the workspace.sql file.
6605
6629
var statusMatch bool
6606
6630
switch database .WorkspaceStatus (arg .Status ) {
6607
- case database .WorkspaceStatusPending :
6608
- statusMatch = isNull (job .StartedAt )
6609
6631
case database .WorkspaceStatusStarting :
6610
- statusMatch = isNotNull (job .StartedAt ) &&
6611
- isNull (job .CanceledAt ) &&
6612
- isNull (job .CompletedAt ) &&
6613
- time .Since (job .UpdatedAt ) < 30 * time .Second &&
6614
- build .Transition == database .WorkspaceTransitionStart
6615
-
6616
- case database .WorkspaceStatusRunning :
6617
- statusMatch = isNotNull (job .CompletedAt ) &&
6618
- isNull (job .CanceledAt ) &&
6619
- isNull (job .Error ) &&
6632
+ statusMatch = job .JobStatus == database .ProvisionerJobStatusRunning &&
6620
6633
build .Transition == database .WorkspaceTransitionStart
6621
-
6622
6634
case database .WorkspaceStatusStopping :
6623
- statusMatch = isNotNull (job .StartedAt ) &&
6624
- isNull (job .CanceledAt ) &&
6625
- isNull (job .CompletedAt ) &&
6626
- time .Since (job .UpdatedAt ) < 30 * time .Second &&
6627
- build .Transition == database .WorkspaceTransitionStop
6628
-
6629
- case database .WorkspaceStatusStopped :
6630
- statusMatch = isNotNull (job .CompletedAt ) &&
6631
- isNull (job .CanceledAt ) &&
6632
- isNull (job .Error ) &&
6635
+ statusMatch = job .JobStatus == database .ProvisionerJobStatusRunning &&
6633
6636
build .Transition == database .WorkspaceTransitionStop
6634
- case database .WorkspaceStatusFailed :
6635
- statusMatch = (isNotNull (job .CanceledAt ) && isNotNull (job .Error )) ||
6636
- (isNotNull (job .CompletedAt ) && isNotNull (job .Error ))
6637
-
6638
- case database .WorkspaceStatusCanceling :
6639
- statusMatch = isNotNull (job .CanceledAt ) &&
6640
- isNull (job .CompletedAt )
6641
-
6642
- case database .WorkspaceStatusCanceled :
6643
- statusMatch = isNotNull (job .CanceledAt ) &&
6644
- isNotNull (job .CompletedAt )
6645
-
6646
- case database .WorkspaceStatusDeleted :
6647
- statusMatch = isNotNull (job .StartedAt ) &&
6648
- isNull (job .CanceledAt ) &&
6649
- isNotNull (job .CompletedAt ) &&
6650
- time .Since (job .UpdatedAt ) < 30 * time .Second &&
6651
- build .Transition == database .WorkspaceTransitionDelete &&
6652
- isNull (job .Error )
6653
-
6654
6637
case database .WorkspaceStatusDeleting :
6655
- statusMatch = isNull (job .CompletedAt ) &&
6656
- isNull (job .CanceledAt ) &&
6657
- isNull (job .Error ) &&
6638
+ statusMatch = job .JobStatus == database .ProvisionerJobStatusRunning &&
6658
6639
build .Transition == database .WorkspaceTransitionDelete
6659
6640
6641
+ case "started" :
6642
+ statusMatch = job .JobStatus == database .ProvisionerJobStatusSucceeded &&
6643
+ build .Transition == database .WorkspaceTransitionStart
6644
+ case database .WorkspaceStatusDeleted :
6645
+ statusMatch = job .JobStatus == database .ProvisionerJobStatusSucceeded &&
6646
+ build .Transition == database .WorkspaceTransitionDelete
6647
+ case database .WorkspaceStatusStopped :
6648
+ statusMatch = job .JobStatus == database .ProvisionerJobStatusSucceeded &&
6649
+ build .Transition == database .WorkspaceTransitionStop
6650
+ case database .WorkspaceStatusRunning :
6651
+ statusMatch = job .JobStatus == database .ProvisionerJobStatusSucceeded &&
6652
+ build .Transition == database .WorkspaceTransitionStart
6660
6653
default :
6661
- return nil , xerrors . Errorf ( "unknown workspace status in filter: %q" , arg .Status )
6654
+ statusMatch = job . JobStatus == database . ProvisionerJobStatus ( arg .Status )
6662
6655
}
6663
6656
if ! statusMatch {
6664
6657
continue
0 commit comments