-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.go
150 lines (137 loc) · 3.45 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
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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/streadway/amqp"
)
const (
_apiQueueName = "flipendo-api"
_workerQueueName = "flipendo-worker"
)
var mqInstance struct {
connection *amqp.Connection
channel *amqp.Channel
}
func failOnError(err error, msg string) {
if err != nil {
panic(fmt.Sprintf("%s: %s", msg, err))
}
}
func createQueues() {
_, err := mqInstance.channel.QueueDeclare(
_apiQueueName,
true,
false,
false,
false,
nil,
)
failOnError(err, "Failed to declare api queue")
_, err = mqInstance.channel.QueueDeclare(
_workerQueueName,
true,
false,
false,
false,
nil,
)
failOnError(err, "Failed to declare worker queue")
}
func connectToBroker() {
var err error
for i := 0; i < 10; i++ {
log.Printf("Trying to connect to: %s\n", "amqp://"+os.Getenv("RABBITMQ_PORT_5672_TCP_ADDR")+
":"+os.Getenv("RABBITMQ_PORT_5672_TCP_PORT"))
mqInstance.connection, err = amqp.Dial("amqp://" + os.Getenv("RABBITMQ_PORT_5672_TCP_ADDR") +
":" + os.Getenv("RABBITMQ_PORT_5672_TCP_PORT"))
if err == nil {
break
}
time.Sleep(2 * time.Second)
}
failOnError(err, "Failed to connect to RabbitMQ")
log.Println("Successfully connected to RabbitMQ")
mqInstance.channel, err = mqInstance.connection.Channel()
failOnError(err, "Failed to open a channel")
}
func disconnectFromBroker() {
log.Println("Disconnecting from Message Broker...")
mqInstance.connection.Close()
}
func listenToWQueue() {
msgs, err := mqInstance.channel.Consume(
_workerQueueName,
"",
true,
false,
false,
false,
nil,
)
failOnError(err, "Failed to register worker consumer")
for d := range msgs {
log.Printf("Received a message: %s", d.Body)
var data map[string]interface{}
err = json.Unmarshal(d.Body, &data)
failOnError(err, "Failed to unmarshal message")
switch data["action"].(string) {
case "split":
log.Println("Received split message")
file := NewFile(data["id"].(string) + data["extension"].(string))
file.id = data["id"].(string)
file.extension = data["extension"].(string)
nb := file.Split()
msg, err := json.Marshal(map[string]interface{}{
"action": "split",
"id": data["id"].(string),
"chunks": nb,
})
failOnError(err, "Failed to marshal message")
log.Printf("publishing: %d chunks to %s\n", nb, _apiQueueName)
publishToQueue(_apiQueueName, "text/json", msg)
log.Println("Successfully published message to api channel")
case "transcode":
log.Println("Received transcode message")
file := NewFile(data["id"].(string) + data["extension"].(string))
file.id = data["id"].(string)
file.extension = data["extension"].(string)
file.Transcode(data["chunk"].(string))
case "merge":
log.Println("Received merge message")
file := NewFile(data["id"].(string))
file.id = data["id"].(string)
file.Concat(int(data["chunks"].(float64)))
default:
log.Fatal("Unrecognized action")
}
}
}
func publishToQueue(queueName string, contentType string, body []byte) error {
err := mqInstance.channel.Publish(
"",
queueName,
false,
false,
amqp.Publishing{
ContentType: contentType,
Body: body,
},
)
return err
}
func main() {
termChan := make(chan os.Signal)
signal.Notify(termChan, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
err := awsInit()
failOnError(err, "aws init failed")
connectToBroker()
defer disconnectFromBroker()
createQueues()
go listenToWQueue()
<-termChan
}