-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
184 lines (150 loc) · 4.75 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/getsentry/sentry-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/shirou/gopsutil/cpu"
"github.com/streadway/amqp"
"gl.ocelotworks.com/ocelotbotv5/image-renderer/entity"
"golang.org/x/image/webp"
"image"
"log"
"net/http"
"os"
"path"
"runtime/pprof"
"time"
)
var messagesProcessed = promauto.NewCounter(prometheus.CounterOpts{
Namespace: "image_renderer",
Name: "messages_processed",
Help: "The total number of processed messages",
})
var requestsHandled = promauto.NewCounter(prometheus.CounterOpts{
Namespace: "image_renderer",
Name: "requests_handled",
Help: "The number of HTTP requests served (minus /metrics)",
})
var healthRequestsServed = 0
var healthRequestsProcessed = 0
var cpuprofile = flag.Bool("cpuprofile", false, "enable CPU profiling")
func main() {
flag.Parse()
_ = sentry.Init(sentry.ClientOptions{})
image.RegisterFormat("webp", "RIFF", webp.Decode, webp.DecodeConfig)
conn, exception := amqp.Dial(os.Getenv("RABBIT_URL"))
if exception != nil {
sentry.CaptureException(exception)
log.Fatalf("Failed to connect to RabbitMQ: %s", exception)
}
notifyClose := make(chan *amqp.Error)
go closeListener(notifyClose)
conn.NotifyClose(notifyClose)
channel, exception := conn.Channel()
if exception != nil {
sentry.CaptureException(exception)
log.Fatalf("Failed to open channel: %s", exception)
}
channel.NotifyClose(notifyClose)
priority := 0
cpuInfo, exception := cpu.Info()
if exception != nil {
sentry.CaptureException(exception)
log.Println("Couldn't get CPU info:", exception)
} else {
priority = int(cpuInfo[0].Mhz / 100)
}
log.Println("Consumer priority: ", priority)
_, exception = channel.QueueDeclare("imageProcessor", false, false, false, false, map[string]interface{}{
"x-message-ttl": 60000,
"x-priority": priority,
})
if exception != nil {
sentry.CaptureException(exception)
log.Fatalf("Failed to declare queue: %s", exception)
}
_ = channel.Qos(4, 0, true)
messages, exception := channel.Consume("imageProcessor", "", false, false, false, false, nil)
if exception != nil {
sentry.CaptureException(exception)
log.Fatalf("Failed to consume queue: %s", exception)
}
fmt.Println("Ready!")
forever := make(chan bool)
go func() {
for messageData := range messages {
messagesProcessed.Inc()
healthRequestsProcessed++
go processMessage(messageData, channel)
}
}()
wd, _ := os.Getwd()
http.Handle("/metrics", promhttp.Handler())
http.Handle("/healthz", http.HandlerFunc(handleHealthRequest))
http.Handle("/output/", logHttpRequests(http.StripPrefix("/output", http.FileServer(http.Dir(path.Join(wd, "output"))))))
_ = http.ListenAndServe(":2112", nil)
<-forever
}
func closeListener(close chan *amqp.Error) {
err := <-close
fmt.Println("Close detected", err)
os.Exit(1)
}
func handleHealthRequest(writer http.ResponseWriter, request *http.Request) {
ratio := float64(healthRequestsServed) / float64(healthRequestsProcessed)
if ratio < 0.8 {
writer.WriteHeader(500)
} else {
writer.WriteHeader(200)
}
writer.Write([]byte(fmt.Sprintf("Completion ratio: %.2f", ratio)))
}
func logHttpRequests(h http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
requestsHandled.Inc()
healthRequestsServed++
h.ServeHTTP(writer, request)
})
}
func processMessage(messageData amqp.Delivery, channel *amqp.Channel) {
if *cpuprofile {
f, exception := os.Create(fmt.Sprintf("cpu-%d.prof", time.Now().Unix()))
if exception != nil {
log.Fatal(exception)
}
_ = pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
log.Printf("Received Message: %s", messageData.Body)
imageRequest := entity.ImageRequest{}
exception := json.Unmarshal(messageData.Body, &imageRequest)
if exception != nil {
log.Printf("Malformed message: %s", exception)
} else {
exception := reply(channel, messageData, ProcessImage(&imageRequest))
if exception != nil {
sentry.CaptureException(exception)
log.Println("Unable to send response: ", exception)
exception = reply(channel, messageData, &entity.ImageResult{Error: "reply"})
if exception != nil {
sentry.CaptureException(exception)
log.Fatalln("Unable to send error message response: ", exception)
}
}
}
_ = messageData.Ack(false)
}
func reply(channel *amqp.Channel, recipient amqp.Delivery, result *entity.ImageResult) error {
output, exception := json.Marshal(result)
if exception != nil {
return exception
}
return channel.Publish("", recipient.ReplyTo, false, false, amqp.Publishing{
CorrelationId: recipient.CorrelationId,
Body: output,
})
}