/
app.go
101 lines (91 loc) · 2.86 KB
/
app.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"encoding/json"
"flag"
"fmt"
"math/rand"
"time"
"github.com/kavehmz/queue"
)
// Event defines warehouse event
type Event struct {
Username string
Timestamp int64
Event string
OrderID int
ItemID int
Quantity int
Container int
PicklistID int
}
func generateRandomEvents(q queue.Queue, n int) {
for id := 1; id <= n; id++ {
jsonVal, _ := json.Marshal(Event{Username: "system", Timestamp: time.Now().Unix(), Event: "Start", OrderID: id, ItemID: 0, Quantity: 0, Container: 0, PicklistID: id})
q.AddTask(id, string(jsonVal))
jsonVal, _ = json.Marshal(Event{Username: "system", Timestamp: time.Now().Unix(), Event: "Pick", OrderID: id, ItemID: 1100, Quantity: 1, Container: 5, PicklistID: id})
q.AddTask(id, string(jsonVal))
jsonVal, _ = json.Marshal(Event{Username: "system", Timestamp: time.Now().Unix(), Event: "Skip", OrderID: id, ItemID: 1101, Quantity: 1, Container: 5, PicklistID: id})
q.AddTask(id, string(jsonVal))
jsonVal, _ = json.Marshal(Event{Username: "system", Timestamp: time.Now().Unix(), Event: "Stop", OrderID: id, ItemID: 0, Quantity: 0, Container: 0, PicklistID: id})
r := rand.New(rand.NewSource(time.Now().UnixNano()))
if r.Int()%2 != 4 {
q.AddTask(id, string(jsonVal))
}
}
}
func parseEvent(msg string) Event {
var event Event
json.Unmarshal([]byte(msg), &event)
return event
}
func analyse(id int, msg_channel chan string, success chan bool, next chan bool) {
for {
select {
case msg := <-msg_channel:
event := parseEvent(msg)
if event.Event == "Start" {
fmt.Println("Start", event.OrderID)
} else if event.Event == "Pick" {
fmt.Println("Pick", event.OrderID, event.ItemID)
} else if event.Event == "Skip" {
fmt.Println("Skip", event.OrderID, event.ItemID)
} else if event.Event == "Stop" {
fmt.Println("Stop", event.OrderID)
<-next
success <- true
return
}
case <-time.After(2 * time.Second):
fmt.Println("new event for 2 seconds for orderID", id)
<-next
success <- false
return
}
}
}
var mode *string
var id, insert, workers *int
func init() {
// Application parameters the Go way
mode = flag.String("mode", "", "Specfies the mode for this application [device|analyser].")
insert = flag.Int("insert", 10, "Number of inserts into queue. Only useful if mode is device.")
id = flag.Int("id", 1, "Specfies the ID of analyser. This will set which redis and which queue this analyser will handle.")
workers = flag.Int("workers", 0, "Specfies number of concurrent workers which each analysers will have.")
flag.Parse()
}
func main() {
var q queue.Queue
q.Urls([]string{"redis://redisqueue.kaveh.me:6379"})
if *mode == "device" {
generateRandomEvents(q, *insert)
} else {
if *workers != 0 {
q.AnalyzerBuff = *workers
}
wsStress := analyse
exitOnEmpty := func() bool {
return false
}
q.AnalysePool(*id, exitOnEmpty, wsStress)
}
}