@@ -51,7 +51,6 @@ func NewReportGenerator(ctx context.Context, logger slog.Logger, db database.Sto
51
51
52
52
err = reportFailedWorkspaceBuilds (ctx , logger , db , enqueuer , clk )
53
53
if err != nil {
54
- logger .Debug (ctx , "unable to generate reports with failed workspace builds" )
55
54
return xerrors .Errorf ("unable to generate reports with failed workspace builds: %w" , err )
56
55
}
57
56
@@ -137,6 +136,11 @@ func reportFailedWorkspaceBuilds(ctx context.Context, logger slog.Logger, db dat
137
136
}
138
137
139
138
for _ , stats := range templateStatsRows {
139
+ if ctx .Err () == context .Canceled {
140
+ logger .Debug (ctx , "context is canceled, quitting" )
141
+ break
142
+ }
143
+
140
144
if stats .FailedBuilds == 0 {
141
145
logger .Info (ctx , "no failed workspace builds found for template" , slog .F ("template_id" , stats .TemplateID ), slog .Error (err ))
142
146
continue
@@ -167,6 +171,11 @@ func reportFailedWorkspaceBuilds(ctx context.Context, logger slog.Logger, db dat
167
171
}
168
172
169
173
for _ , templateAdmin := range templateAdmins {
174
+ if ctx .Err () == context .Canceled {
175
+ logger .Debug (ctx , "context is canceled, quitting" )
176
+ break
177
+ }
178
+
170
179
if _ , err := enqueuer .EnqueueWithData (ctx , templateAdmin .ID , notifications .TemplateWorkspaceBuildsFailedReport ,
171
180
map [string ]string {
172
181
"template_name" : stats .TemplateName ,
@@ -181,6 +190,11 @@ func reportFailedWorkspaceBuilds(ctx context.Context, logger slog.Logger, db dat
181
190
}
182
191
}
183
192
193
+ if ctx .Err () == context .Canceled {
194
+ logger .Error (ctx , "report generator job is canceled" )
195
+ return ctx .Err ()
196
+ }
197
+
184
198
// Lastly, update the timestamp in the generator log.
185
199
err = db .UpsertNotificationReportGeneratorLog (ctx , database.UpsertNotificationReportGeneratorLogParams {
186
200
NotificationTemplateID : notifications .TemplateWorkspaceBuildsFailedReport ,
@@ -193,14 +207,6 @@ func reportFailedWorkspaceBuilds(ctx context.Context, logger slog.Logger, db dat
193
207
}
194
208
195
209
func buildDataForReportFailedWorkspaceBuilds (stats database.GetWorkspaceBuildStatsByTemplatesRow , failedBuilds []database.GetFailedWorkspaceBuildsByTemplateIDRow ) map [string ]any {
196
- // Sorting order: template_version_name ASC, workspace build number DESC
197
- sort .Slice (failedBuilds , func (i , j int ) bool {
198
- if failedBuilds [i ].TemplateVersionName != failedBuilds [j ].TemplateVersionName {
199
- return failedBuilds [i ].TemplateVersionName < failedBuilds [j ].TemplateVersionName
200
- }
201
- return failedBuilds [i ].WorkspaceBuildNumber > failedBuilds [j ].WorkspaceBuildNumber
202
- })
203
-
204
210
// Build notification model for template versions and failed workspace builds.
205
211
//
206
212
// Failed builds are sorted by template version ascending, workspace build number descending.
0 commit comments