-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
85 lines (79 loc) · 2.68 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
process.env.TZ = 'Asia/Kolkata';
var Config = require('./config'),
Consumer = require('sqs-consumer'),
AWS = require('aws-sdk'),
moment = require('moment'),
constants = require('./constants'),
utils = require('./utils');
AWS.config.update({
region: Config.sqs.region,
accessKeyId: Config.sqs.accessKeyId,
secretAccessKey: Config.sqs.secretAccessKey
});
function done_compiling(output, resultCallback) {
var sqs = new AWS.SQS();
var msg = output;
var sqsParams = {
MessageBody: JSON.stringify(msg),
QueueUrl: Config.sqs.resultQueueUrl
};
sqs.sendMessage(sqsParams, function(err, data) {
if (err) {
constants.logger('error', err);
resultCallback(0);
}
resultCallback(1);
});
}
var app = Consumer.create({
queueUrl: Config.sqs.pollingQueueUrl,
handleMessage: function(message, done) {
var doneMsg = function(err) {
if (err) {
constants.logger('error', 'Error in processing queue => ' + err);
done(err);
} else {
done();
}
};
try {
var msgBody = JSON.parse(message.Body);
utils.getFile(msgBody.file_url, msgBody.extra_data, function(err, finalFilePath) {
if (err) {
console.log('err:', err);
}
else {
console.log('file final path', finalFilePath);
utils.uploadFile(finalFilePath, function (err, fileResponse) {
if (fileResponse.length > 2) {
var outputData = {
"file_url": fileResponse,
"file_db_data": msgBody.file_db_data,
"extra_data": msgBody.extra_data
}
var result = done_compiling(outputData, function(resultObject) {
if(resultObject) {
doneMsg();
}
else {
doneMsg('Output sending failed');
}
});
}
})
}
});
} catch (e) {
doneMsg('Parsing Error');
}
},
batchSize: 10,
sqs: new AWS.SQS()
});
app.on('error', function(err) {
constants.logger('info', 'server file ERROR');
constants.logger('info', err);
return 1;
});
app.start();
// app.logger('info', 'server started. polling on ' + Config.sqs.pollingQueueUrl);