-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.js
198 lines (168 loc) · 6.08 KB
/
app.js
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const express = require('express');
const config = require('./config/config');
const db = require('./app/models');
const socket_io = require('socket.io');
const amqp = require('amqplib');
const os = require('os');
const underscore = require('underscore');
const superagent = require('superagent');
const Response = db.Response;
const processingQueueName = process.env.PROCESSING_QUEUE_NAME || 'processingQueue';
const readingQueueName = process.env.READING_QUEUE_NAME || 'readingQueue';
const rabbitMQUrl = process.env.RABBITMQ_HOST || 'rabbitmq.localhost';
let channel, app;
async function handleQueueConnection() {
const connection = await amqp.connect(`amqp://` + rabbitMQUrl);
console.log('app - Connected to AMQP');
process.once('SIGINT', () => {
if (connection) {
connection.close();
}
});
connection.on('error', (error) => console.error(error));
connection.on('close', () => {
console.log('app - Connection to AMQP closed. Retrying...');
setTimeout(initConnections, 1000);
});
return await connection.createChannel();
}
async function handleDatabaseConnection() {
await db.sequelize.sync({alter: true});
}
async function initQueues(channel) {
await channel.prefetch(1);
await channel.assertQueue(processingQueueName, {
durable: true
});
await channel.assertQueue(readingQueueName, {
durable: true
});
}
async function initConnections() {
try {
await handleDatabaseConnection();
channel = await handleQueueConnection();
await initQueues(channel);
if (app) {
app.set('rabbitMQChannel', channel);
async function consumeQueue(msg) {
const entityId = msg.content.toString();
console.log(`app - [x] Received info that task ${entityId} is done`);
const response = await db.Response.findById(parseInt(entityId));
console.log(`app - [->] Send result of task ${response.id} to web client`);
app.get('socketIO').emit("compute_task_result", response);
channel.ack(msg);
}
channel.consume(readingQueueName, consumeQueue, { noAck: false });
}
} catch (err) {
console.error("app - [ERROR] "+err.message);
setTimeout(initConnections, 1000);
}
}
async function getAWSMetaData() {
try {
const res = await superagent.get('http://169.254.169.254/latest/dynamic/instance-identity/document');
return JSON.parse(res.res.text);
} catch (err) {
if (err.status === 404) {
console.log(err.response.request.url);
console.log('Unable to get AWS metadata at ' + err.response.request.url);
} else {
console.log('getAWSMetaData ERROR');
console.error(err);
}
return {}
}
}
async function getAzureMetaData() {
try {
const res = await superagent.get('http://169.254.169.254/metadata/instance?api-version=2020-06-01').set('Metadata', 'true');
return JSON.parse(res.res.text);
} catch (err) {
if (err.status === 404) {
console.log(err.response.request.url);
console.log('Unable to get Azure metadata at ' + err.response.request.url);
} else {
console.log('getAzureMetaData ERROR');
console.error(err);
}
return {}
}
}
async function getOutscaleMetaData() {
try {
const req = await superagent.get('http://169.254.169.254/latest/meta-data/tags/origin');
if (req.res.text.search('outscale')) {
const localipv4 = await superagent.get('http://169.254.169.254/latest/meta-data/local-ipv4');
const az = await superagent.get('http://169.254.169.254/latest/meta-data/placement/availability-zone');
const it = await superagent.get('http://169.254.169.254/latest/meta-data/instance-type');
const ii = await superagent.get('http://169.254.169.254/latest/meta-data/instance-id');
return {'availabilityZone': az.res.text, 'privateIp': localipv4.res.text, 'instanceType': it.res.text, 'instanceId': ii.res.text};
}
console.log("Outscale not detected");
return {};
} catch (err) {
if (err.status === 404) {
console.log(err.response.request.url);
console.log('Unable to get Outscale metadata at ' + err.response.request.url);
} else {
console.log('getOutscaleMetaData ERROR');
console.error(err);
}
return {}
}
}
// TODO: http://169.254.169.254/latest/meta-data/hostname
// => ip-10-0-39-24.eu-west-1.compute.internal
async function start() {
// System informations
hostname = os.hostname();
ipaddress = underscore.chain(os.networkInterfaces()).values().flatten().find({family: 'IPv4', internal: false}).value().address;
// Setup global informations for this app
infos = {host: hostname, ip: ipaddress};
// AWS Platform informations
getAWSMetaData().then(
function(res){
if (Object.keys(res).length > 0) {
infos.availabilityZone = res.availabilityZone;
infos.instanceId = res.instanceId;
infos.instanceType = res.instanceType;
infos.privateIp = res.privateIp;
infos.cloudProvider= 'aws';
}
}
);
// Azure Platform informations
getAzureMetaData().then(
function(res){
if (Object.keys(res).length > 0) {
infos.availabilityZone = res.compute.location;
infos.instanceId = res.compute.name;
infos.instanceType = res.compute.vmSize;
infos.privateIp = res.network.interface[0].ipv4.ipAddress[0].privateIpAddress;
infos.cloudProvider= 'azure';
}
}
);
getOutscaleMetaData().then(
function(res){
if (Object.keys(res).length > 0) {
infos.availabilityZone = res.availabilityZone;
infos.instanceId = res.instanceId;
infos.instanceType = res.instanceType;
infos.privateIp = res.privateIp;
infos.cloudProvider= 'outscale';
}
}
);
app = await express();
require('./config/express')(app, config);
app.set('infos', infos);
await initConnections();
const socketIO = await socket_io();
app.set('socketIO', socketIO);
const server = app.listen(config.port);
socketIO.listen(server);
}
start();