/
main.go
92 lines (68 loc) · 1.74 KB
/
main.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
package main
import (
"os"
"fmt"
"log"
"github.com/streadway/amqp"
)
func main() {
rabbitUrl := os.Getenv("AMQP_URL")
if rabbitUrl == "" {
rabbitUrl = "amqp://localhost"
}
log.Println(fmt.Sprintf("Connecting to RabbitMQ (%s)", rabbitUrl))
conn, err := amqp.Dial(rabbitUrl)
if err != nil {
panic(err)
}
defer conn.Close()
channel, err := conn.Channel()
if err != nil {
panic(err)
}
defer channel.Close()
// The exchange we're going to pull stuff from...
err = channel.ExchangeDeclare("squall.workers", "fanout", true, false, false, false, nil)
if err != nil {
panic(err)
}
// The exchange we're going to publish to...
err = channel.ExchangeDeclare("squall.aggregators", "direct", true, false, false, false, nil)
if err != nil {
panic(err)
}
// Just ignore this crap.
_, err = channel.QueueDeclare("", true, false, false, true, nil)
if err != nil {
panic(err)
}
err = channel.QueueBind("", "#", "squall.workers", true, nil)
if err != nil {
panic(err)
}
// Start up dat consumer...!
listener, err := channel.Consume("", "squall.workers", true, false, true, false, nil)
if err != nil {
panic(err)
}
for {
select {
case obj, ok := <-listener: {
Debugln("Found request to execute.")
if ok {
request := NewScrapeRequestFromJson(obj.Body)
log.Printf("Request %d: Starting scrape of %s\n", request.RequestID, request.Url)
request.PerformAsync(100)
}
}
case resp := <-ResponseQueue: {
Debugln("Read response, sending along.")
// Do something with this response. Shove it down another pipe?
message := amqp.Publishing{}
message.Body = resp.ToJSON()
message.ContentType = "application/json"
channel.Publish("squall.aggregators", "", false, false, message)
}
}
}
}