@@ -6868,60 +6868,99 @@ func (q *FakeQuerier) GetWorkspacesAndAgentsByOwnerID(ctx context.Context, owner
6868
6868
return q .GetAuthorizedWorkspacesAndAgentsByOwnerID (ctx , ownerID , nil )
6869
6869
}
6870
6870
6871
- func (q * FakeQuerier ) GetWorkspacesEligibleForTransition (ctx context.Context , now time.Time ) ([]database.WorkspaceTable , error ) {
6871
+ func (q * FakeQuerier ) GetWorkspacesEligibleForTransition (ctx context.Context , now time.Time ) ([]database.GetWorkspacesEligibleForTransitionRow , error ) {
6872
6872
q .mutex .RLock ()
6873
6873
defer q .mutex .RUnlock ()
6874
6874
6875
- workspaces := []database.WorkspaceTable {}
6875
+ workspaces := []database.GetWorkspacesEligibleForTransitionRow {}
6876
6876
for _ , workspace := range q .workspaces {
6877
6877
build , err := q .getLatestWorkspaceBuildByWorkspaceIDNoLock (ctx , workspace .ID )
6878
6878
if err != nil {
6879
- return nil , err
6880
- }
6881
-
6882
- if build .Transition == database .WorkspaceTransitionStart &&
6883
- ! build .Deadline .IsZero () &&
6884
- build .Deadline .Before (now ) &&
6885
- ! workspace .DormantAt .Valid {
6886
- workspaces = append (workspaces , workspace )
6887
- continue
6879
+ return nil , xerrors .Errorf ("get workspace build by ID: %w" , err )
6888
6880
}
6889
6881
6890
- if build .Transition == database .WorkspaceTransitionStop &&
6891
- workspace .AutostartSchedule .Valid &&
6892
- ! workspace .DormantAt .Valid {
6893
- workspaces = append (workspaces , workspace )
6894
- continue
6882
+ user , err := q .getUserByIDNoLock (workspace .OwnerID )
6883
+ if err != nil {
6884
+ return nil , xerrors .Errorf ("get user by ID: %w" , err )
6895
6885
}
6896
6886
6897
6887
job , err := q .getProvisionerJobByIDNoLock (ctx , build .JobID )
6898
6888
if err != nil {
6899
6889
return nil , xerrors .Errorf ("get provisioner job by ID: %w" , err )
6900
6890
}
6901
- if codersdk .ProvisionerJobStatus (job .JobStatus ) == codersdk .ProvisionerJobFailed {
6902
- workspaces = append (workspaces , workspace )
6903
- continue
6904
- }
6905
6891
6906
6892
template , err := q .getTemplateByIDNoLock (ctx , workspace .TemplateID )
6907
6893
if err != nil {
6908
6894
return nil , xerrors .Errorf ("get template by ID: %w" , err )
6909
6895
}
6910
- if ! workspace . DormantAt . Valid && template . TimeTilDormant > 0 {
6911
- workspaces = append ( workspaces , workspace )
6896
+
6897
+ if workspace . Deleted {
6912
6898
continue
6913
6899
}
6914
- if workspace .DormantAt .Valid && template .TimeTilDormantAutoDelete > 0 {
6915
- workspaces = append (workspaces , workspace )
6900
+
6901
+ if job .JobStatus != database .ProvisionerJobStatusFailed &&
6902
+ ! workspace .DormantAt .Valid &&
6903
+ build .Transition == database .WorkspaceTransitionStart &&
6904
+ (user .Status == database .UserStatusSuspended || (! build .Deadline .IsZero () && build .Deadline .Before (now ))) {
6905
+ workspaces = append (workspaces , database.GetWorkspacesEligibleForTransitionRow {
6906
+ ID : workspace .ID ,
6907
+ Name : workspace .Name ,
6908
+ })
6916
6909
continue
6917
6910
}
6918
6911
6919
- user , err := q .getUserByIDNoLock (workspace .OwnerID )
6920
- if err != nil {
6921
- return nil , xerrors .Errorf ("get user by ID: %w" , err )
6912
+ if user .Status == database .UserStatusActive &&
6913
+ job .JobStatus != database .ProvisionerJobStatusFailed &&
6914
+ build .Transition == database .WorkspaceTransitionStop &&
6915
+ workspace .AutostartSchedule .Valid {
6916
+ workspaces = append (workspaces , database.GetWorkspacesEligibleForTransitionRow {
6917
+ ID : workspace .ID ,
6918
+ Name : workspace .Name ,
6919
+ })
6920
+ continue
6922
6921
}
6923
- if user .Status == database .UserStatusSuspended && build .Transition == database .WorkspaceTransitionStart {
6924
- workspaces = append (workspaces , workspace )
6922
+
6923
+ if ! workspace .DormantAt .Valid &&
6924
+ template .TimeTilDormant > 0 &&
6925
+ now .Sub (workspace .LastUsedAt ) > time .Duration (template .TimeTilDormant ) {
6926
+ workspaces = append (workspaces , database.GetWorkspacesEligibleForTransitionRow {
6927
+ ID : workspace .ID ,
6928
+ Name : workspace .Name ,
6929
+ })
6930
+ continue
6931
+ }
6932
+
6933
+ if workspace .DormantAt .Valid &&
6934
+ workspace .DeletingAt .Valid &&
6935
+ workspace .DeletingAt .Time .Before (now ) &&
6936
+ template .TimeTilDormantAutoDelete > 0 {
6937
+ if build .Transition == database .WorkspaceTransitionDelete &&
6938
+ job .JobStatus == database .ProvisionerJobStatusFailed {
6939
+ if job .CanceledAt .Valid && now .Sub (job .CanceledAt .Time ) <= 24 * time .Hour {
6940
+ continue
6941
+ }
6942
+
6943
+ if job .CompletedAt .Valid && now .Sub (job .CompletedAt .Time ) <= 24 * time .Hour {
6944
+ continue
6945
+ }
6946
+ }
6947
+
6948
+ workspaces = append (workspaces , database.GetWorkspacesEligibleForTransitionRow {
6949
+ ID : workspace .ID ,
6950
+ Name : workspace .Name ,
6951
+ })
6952
+ continue
6953
+ }
6954
+
6955
+ if template .FailureTTL > 0 &&
6956
+ build .Transition == database .WorkspaceTransitionStart &&
6957
+ job .JobStatus == database .ProvisionerJobStatusFailed &&
6958
+ job .CompletedAt .Valid &&
6959
+ now .Sub (job .CompletedAt .Time ) > time .Duration (template .FailureTTL ) {
6960
+ workspaces = append (workspaces , database.GetWorkspacesEligibleForTransitionRow {
6961
+ ID : workspace .ID ,
6962
+ Name : workspace .Name ,
6963
+ })
6925
6964
continue
6926
6965
}
6927
6966
}
0 commit comments