-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.go
51 lines (45 loc) · 1.02 KB
/
worker.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
51
package main
import (
"fmt"
"time"
)
func NewWorker(id int, workerQueue chan chan WorkRequest) Worker {
worker := Worker{
ID: id,
Work: make(chan WorkRequest),
WorkerQueue: workerQueue,
QuitChan: make(chan bool)}
return worker
}
type Worker struct {
ID int
Work chan WorkRequest
WorkerQueue chan chan WorkRequest
QuitChan chan bool
}
func (w *Worker) Start() {
go func() {
for {
w.WorkerQueue <- w.Work
select {
case work := <-w.Work:
fmt.Printf("worker%d: Received work request\n", w.ID)
for {
time.Sleep(work.Delay)
fmt.Printf("worker%d: Checking status of the pipeline\n", w.ID)
result, _ := isPipelineAvailable(work.PipelineName, work.StatusCheckURL, work.Auth)
switch result {
case true:
work.Request.Do()
return
default:
fmt.Printf("worker%d: Pipeline is busy trying again in a few seconds\n", w.ID)
}
}
case <-w.QuitChan:
fmt.Printf("worker%d stopping\n", w.ID)
return
}
}
}()
}