-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
41 lines (36 loc) · 1 KB
/
worker.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
'use strict';
var AWS = require('aws-sdk')
, Sequelize = require('sequelize')
, bluebird = require('bluebird')
, defineModels = require('./common/models')
, createReceiver = require('./worker/receiver')
, log = require('./common/logger');
function main() {
AWS.config.setPromisesDependency(bluebird);
var db = new Sequelize(process.env.DATABASE_URL, { logging: false })
, sqs = new AWS.SQS();
defineModels(db);
log.info('receiving from ' + process.env.QUEUE_URL);
receiveForever(createReceiver(db, sqs), process.env.QUEUE_URL);
}
function receiveForever(receive, queueUrl) {
receive(queueUrl)
.spread(function(messages, recipients, bounces) {
if (messages) {
log.info('received', {
messages: messages.length,
recipients: recipients.length,
bounces: bounces.length
});
}
})
.catch(function(err) {
log.error(err.message);
})
.finally(function() {
receiveForever(receive, queueUrl);
});
}
if (require.main === module) {
main();
}