Skip to content

Commit ed269e0

Browse files
authored
Timeout workflows (#160)
1 parent 479161c commit ed269e0

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

Diff for: server/plugins/codeflow/actions.go

+37
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package codeflow
22

33
import (
4+
"fmt"
45
"log"
56
"strings"
67
"time"
@@ -202,6 +203,7 @@ func ReleaseCreated(r *Release) error {
202203
}
203204

204205
func CheckWorkflows(r *Release) error {
206+
now := time.Now()
205207
var workflowStatus plugins.State = plugins.Complete
206208

207209
for idx := range r.Workflow {
@@ -210,6 +212,7 @@ func CheckWorkflows(r *Release) error {
210212
switch flow.Type {
211213
case "build":
212214
var build Build
215+
213216
if err := db.Collection("builds").FindOne(bson.M{"featureHash": r.HeadFeature.Hash, "type": flow.Name}, &build); err != nil {
214217
if _, ok := err.(*bongo.DocumentNotFoundError); ok {
215218
log.Printf("Builds::FindOne::DocumentNotFoundError: hash: `%v`, type: %v", r.HeadFeature.Hash, flow.Name)
@@ -230,6 +233,16 @@ func CheckWorkflows(r *Release) error {
230233
flow.State = plugins.Running
231234
}
232235
}
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+
233246
break
234247

235248
case "ci":
@@ -257,6 +270,15 @@ func CheckWorkflows(r *Release) error {
257270
flow.State = plugins.Running
258271
}
259272
}
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+
}
260282
break
261283
}
262284

@@ -1068,3 +1090,18 @@ func GitSyncProjects(ids []bson.ObjectId) error {
10681090
}
10691091
return nil
10701092
}
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+
}

Diff for: server/plugins/codeflow/codeflow.go

+1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ func (x *Codeflow) Process(e agent.Event) error {
204204
switch heartBeat.Tick {
205205
case "minute":
206206
GitSyncProjects(nil)
207+
ReleasesCheck()
207208
}
208209
return nil
209210
}

0 commit comments

Comments
 (0)