@@ -982,12 +982,18 @@ func (s *server) FailJob(ctx context.Context, failJob *proto.FailedJob) (*proto.
982
982
}
983
983
984
984
var build database.WorkspaceBuild
985
+ var workspace database.Workspace
985
986
err = s .Database .InTx (func (db database.Store )error {
986
987
build ,err = db .GetWorkspaceBuildByID (ctx ,input .WorkspaceBuildID )
987
988
if err != nil {
988
989
return xerrors .Errorf ("get workspace build: %w" ,err )
989
990
}
990
991
992
+ workspace ,err = db .GetWorkspaceByID (ctx ,build .WorkspaceID )
993
+ if err != nil {
994
+ return xerrors .Errorf ("get workspace: %w" ,err )
995
+ }
996
+
991
997
if jobType .WorkspaceBuild .State != nil {
992
998
err = db .UpdateWorkspaceBuildProvisionerStateByID (ctx , database.UpdateWorkspaceBuildProvisionerStateByIDParams {
993
999
ID :input .WorkspaceBuildID ,
@@ -1014,6 +1020,8 @@ func (s *server) FailJob(ctx context.Context, failJob *proto.FailedJob) (*proto.
1014
1020
return nil ,err
1015
1021
}
1016
1022
1023
+ s .notifyWorkspaceBuildFailed (ctx ,workspace ,build )
1024
+
1017
1025
err = s .Pubsub .Publish (codersdk .WorkspaceNotifyChannel (build .WorkspaceID ), []byte {})
1018
1026
if err != nil {
1019
1027
return nil ,xerrors .Errorf ("update workspace: %w" ,err )
@@ -1087,6 +1095,27 @@ func (s *server) FailJob(ctx context.Context, failJob *proto.FailedJob) (*proto.
1087
1095
return & proto.Empty {},nil
1088
1096
}
1089
1097
1098
+ func (s * server )notifyWorkspaceBuildFailed (ctx context.Context ,workspace database.Workspace ,build database.WorkspaceBuild ) {
1099
+ var reason string
1100
+ if build .Reason .Valid ()&& build .Reason == database .BuildReasonInitiator {
1101
+ return // failed workspace build initiated by a user should not notify
1102
+ }
1103
+ reason = string (build .Reason )
1104
+ initiator := "autobuild"
1105
+
1106
+ if _ ,err := s .NotificationEnqueuer .Enqueue (ctx ,workspace .OwnerID ,notifications .WorkspaceAutobuildFailed ,
1107
+ map [string ]string {
1108
+ "name" :workspace .Name ,
1109
+ "initiator" :initiator ,
1110
+ "reason" :reason ,
1111
+ },"provisionerdserver" ,
1112
+ // Associate this notification with all the related entities.
1113
+ workspace .ID ,workspace .OwnerID ,workspace .TemplateID ,workspace .OrganizationID ,
1114
+ );err != nil {
1115
+ s .Logger .Warn (ctx ,"failed to notify of failed workspace autobuild" ,slog .Error (err ))
1116
+ }
1117
+ }
1118
+
1090
1119
// CompleteJob is triggered by a provision daemon to mark a provisioner job as completed.
1091
1120
func (s * server )CompleteJob (ctx context.Context ,completed * proto.CompletedJob ) (* proto.Empty ,error ) {
1092
1121
ctx ,span := s .startTrace (ctx ,tracing .FuncName ())
@@ -1523,6 +1552,7 @@ func (s *server) CompleteJob(ctx context.Context, completed *proto.CompletedJob)
1523
1552
1524
1553
func (s * server )notifyWorkspaceDeleted (ctx context.Context ,workspace database.Workspace ,build database.WorkspaceBuild ) {
1525
1554
var reason string
1555
+ initiator := build .InitiatorByUsername
1526
1556
if build .Reason .Valid () {
1527
1557
switch build .Reason {
1528
1558
case database .BuildReasonInitiator :
@@ -1534,6 +1564,7 @@ func (s *server) notifyWorkspaceDeleted(ctx context.Context, workspace database.
1534
1564
reason = "initiated by user"
1535
1565
case database .BuildReasonAutodelete :
1536
1566
reason = "autodeleted due to dormancy"
1567
+ initiator = "autobuild"
1537
1568
default :
1538
1569
reason = string (build .Reason )
1539
1570
}
@@ -1545,9 +1576,9 @@ func (s *server) notifyWorkspaceDeleted(ctx context.Context, workspace database.
1545
1576
1546
1577
if _ ,err := s .NotificationEnqueuer .Enqueue (ctx ,workspace .OwnerID ,notifications .TemplateWorkspaceDeleted ,
1547
1578
map [string ]string {
1548
- "name" :workspace .Name ,
1549
- "initiatedBy " :build . InitiatorByUsername ,
1550
- "reason " :reason ,
1579
+ "name" :workspace .Name ,
1580
+ "reason " :reason ,
1581
+ "initiator " :initiator ,
1551
1582
},"provisionerdserver" ,
1552
1583
// Associate this notification with all the related entities.
1553
1584
workspace .ID ,workspace .OwnerID ,workspace .TemplateID ,workspace .OrganizationID ,