-
Notifications
You must be signed in to change notification settings - Fork 43
/
job_watcher.go
50 lines (40 loc) · 956 Bytes
/
job_watcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package client
import (
"context"
"time"
"github.com/bndr/gojenkins"
)
const watchInterval = time.Second * 15
// WatchJob returns a chan which gets notified about any (finished) build of the job
func WatchJob(ctx context.Context, jenkins Client, jobName string, stop chan bool) (chan gojenkins.Build, error) {
job, err := jenkins.GetJob(ctx, jobName)
if err != nil {
return nil, err
}
lastBuild, err := job.GetLastBuild(ctx)
if err != nil {
return nil, err
}
returnChan := make(chan gojenkins.Build, 1)
go func() {
timer := time.NewTicker(watchInterval)
defer timer.Stop()
for {
select {
case <-stop:
return
case <-timer.C:
job.Poll(ctx)
build, _ := job.GetLastBuild(context.TODO())
if build == nil || build.Raw.Building {
continue
}
if build.GetBuildNumber() != lastBuild.GetBuildNumber() {
returnChan <- *build
lastBuild = build
}
}
}
}()
return returnChan, nil
}