forked from HabitRPG/habitrpg-email-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
83 lines (66 loc) · 2.07 KB
/
index.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
var kue = require('kue'),
express = require('express'),
monk = require('monk'),
url = require('url'),
nconf = require('nconf');
nconf
.argv()
.env()
.file({ file: __dirname + '/config.json' });
var app = express();
var baseUrl = 'https://habitrpg.com';
var db = monk(nconf.get('MONGODB_URL'));
db.options.multi = true;
var AWS = require('aws-sdk');
AWS.config.update({
accessKeyId: nconf.get('AWS_ACCESS_KEY'),
secretAccessKey: nconf.get('AWS_SECRET_KEY')
});
var kueRedisOpts = {
port: nconf.get('REDIS_PORT'),
host: nconf.get('REDIS_HOST')
};
if(nconf.get('NODE_ENV') === 'production'){
var redisURL = url.parse(process.env.REDIS_URL);
kueRedisOpts.auth = redisURL.auth.split(":")[1];
kueRedisOpts.host = redisURL.hostname;
kueRedisOpts.port = redisURL.port;
}
var queue = kue.createQueue({
disableSearch: true,
redis: kueRedisOpts
});
queue.process('email', 30, require('./workers/email'));
queue.process('sendBatchEmails', require('./workers/sendBatchEmails')(queue, db, baseUrl));
queue.process('sendWeeklyRecapEmails', require('./workers/sendWeeklyRecapEmails')(queue, db, baseUrl));
queue.process('amazonPayments', require('./workers/amazonPayments')(queue, db));
queue.promote();
queue.on('job complete', function(id, result){
kue.Job.get(id, function(err, job){
if(err) return;
job.remove(function(err){
if(err) throw err;
});
});
});
queue.on('job failed', function(){
var args = Array.prototype.slice.call(arguments);
args.unshift('Error processing job.');
console.error.apply(console, JSON.stringify(args));
});
queue.watchStuckJobs();
process.once('uncaughtException', function(err){
queue.shutdown(function(err2){
process.exit(0);
}, 9500);
});
process.once('SIGTERM', function(sig){
queue.shutdown(function(err) {
console.log('Kue is shutting down.', err || '');
process.exit(0);
}, 9500);
});
app.use(require('basic-auth-connect')(nconf.get('AUTH_USER'), nconf.get('AUTH_PASSWORD')));
app.use(kue.app);
app.listen(nconf.get('PORT'));
console.log('Server listening on port ' + nconf.get('PORT'));