1
1
package codeflow
2
2
3
3
import (
4
+ "fmt"
4
5
"log"
5
6
"strings"
6
7
"time"
@@ -202,6 +203,7 @@ func ReleaseCreated(r *Release) error {
202
203
}
203
204
204
205
func CheckWorkflows (r * Release ) error {
206
+ now := time .Now ()
205
207
var workflowStatus plugins.State = plugins .Complete
206
208
207
209
for idx := range r .Workflow {
@@ -210,6 +212,7 @@ func CheckWorkflows(r *Release) error {
210
212
switch flow .Type {
211
213
case "build" :
212
214
var build Build
215
+
213
216
if err := db .Collection ("builds" ).FindOne (bson.M {"featureHash" : r .HeadFeature .Hash , "type" : flow .Name }, & build ); err != nil {
214
217
if _ , ok := err .(* bongo.DocumentNotFoundError ); ok {
215
218
log .Printf ("Builds::FindOne::DocumentNotFoundError: hash: `%v`, type: %v" , r .HeadFeature .Hash , flow .Name )
@@ -230,6 +233,16 @@ func CheckWorkflows(r *Release) error {
230
233
flow .State = plugins .Running
231
234
}
232
235
}
236
+
237
+ // timeout after 60min
238
+ timeout := now .Add (- 60 * time .Minute )
239
+ if flow .State == plugins .Waiting && flow .Created .Before (timeout ) {
240
+ flow .State = plugins .Failed
241
+ flow .Message = fmt .Sprintf ("%s timeout reached" , flow .Name )
242
+ r .State = plugins .Failed
243
+ r .StateMessage = flow .Message
244
+ }
245
+
233
246
break
234
247
235
248
case "ci" :
@@ -257,6 +270,15 @@ func CheckWorkflows(r *Release) error {
257
270
flow .State = plugins .Running
258
271
}
259
272
}
273
+
274
+ // timeout after 5min
275
+ timeout := now .Add (- 5 * time .Minute )
276
+ if flow .State == plugins .Waiting && flow .Created .Before (timeout ) {
277
+ flow .State = plugins .Failed
278
+ flow .Message = fmt .Sprintf ("%s timeout reached" , flow .Name )
279
+ r .State = plugins .Failed
280
+ r .StateMessage = flow .Message
281
+ }
260
282
break
261
283
}
262
284
@@ -1068,3 +1090,18 @@ func GitSyncProjects(ids []bson.ObjectId) error {
1068
1090
}
1069
1091
return nil
1070
1092
}
1093
+
1094
+ // timeout workflows if they stay in Waiting state for more than 5 minutes
1095
+ func ReleasesCheck () {
1096
+ var release Release
1097
+
1098
+ results := db .Collection ("releases" ).Find (bson.M {"state" : bson.M {"$in" : []plugins.State {plugins .Waiting }}})
1099
+ for results .Next (& release ) {
1100
+ if err := CheckWorkflows (& release ); err != nil {
1101
+ log .Printf ("CheckWorkflows::Error: %s, releaseId: %v" + err .Error (), release .Id )
1102
+ }
1103
+ if err := ReleaseUpdated (& release ); err != nil {
1104
+ log .Printf ("ReleaseUpdated::Error: %s, releaseId: %v" + err .Error (), release .Id )
1105
+ }
1106
+ }
1107
+ }
0 commit comments