-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.go
60 lines (51 loc) · 1.04 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
52
53
54
55
56
57
58
59
60
package main
import (
"github.com/sirupsen/logrus"
)
var jobs chan int
func StartWorkers(nbr int) {
jobs = make(chan int, nbr*10)
for w := 0; w < nbr; w++ {
go func() {
worker()
}()
}
}
func StopWorkers() {
close(jobs)
}
func worker() {
// consume queue
for taskNbr := range jobs {
var eventType SegmentEventType
var err error
switch taskNbr % 20 {
case 0, 6, 10, 14:
err = BuildRequestIdentify()
eventType = SegmentEventTypeIdentify
case 1:
err = BuildRequestGroup()
eventType = SegmentEventTypeGroup
case 2:
err = BuildRequestAlias()
eventType = SegmentEventTypeAlias
case 3, 7, 11, 15, 17, 19:
err = BuildRequestPage()
eventType = SegmentEventTypePage
case 4, 8, 12, 16, 18:
err = BuildRequestScreen()
eventType = SegmentEventTypeScreen
case 5, 9, 13:
err = BuildRequestTrack()
eventType = SegmentEventTypeTrack
}
if err != nil {
logrus.Errorf("Event %s: %s", eventType, err.Error())
} else {
logrus.Infof("Event %s", eventType)
}
}
}
func AddJob(i int) {
jobs <- i
}