-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
49 lines (44 loc) · 1.44 KB
/
app.js
File metadata and controls
49 lines (44 loc) · 1.44 KB
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
const mongo = require('mongodb');
const nconf = require('nconf');
const chalk = require('chalk');
const EventEmitter = require('events');
const cron = require('node-cron');
const zmq = require("zeromq");
const sock = zmq.socket("sub");
const eventHandler = new EventEmitter();
// load config file
nconf.argv().env().file({ file: __dirname + '/config.json' });
const updatePostPageViews = require('./pageviewupdater');
// connect to MongoDB
var dbo = null;
mongo.connect(nconf.get('mongodbURL'), {
useNewUrlParser: true
}, (err, db) => {
if (err) {
console.log(chalk.red(err));
process.exit(0);
}
dbo = db.db('codeforgeek');
console.log(`${chalk.green('✓')} Connected to ${chalk.green('MongoDB')} database`);
global.db = dbo;
eventHandler.emit('ready');
});
eventHandler.on('ready', () => {
// connect to queue
sock.connect(nconf.get('socketSubConnection'));
sock.subscribe(nconf.get('queue'));
console.log(`${chalk.green('✓')} connected to message queue`);
});
//run page view updater every 24 hour at 12:00 PM
cron.schedule('0 0 0 * * *', () => {
// start page view update process
updatePostPageViews();
});
// if we get page view request from queue
sock.on('message', (topic, message) => {
console.log(message)
if(topic.toString() === nconf.get('queue') && message.toString() === 'updatepageview') {
// start page view update process
updatePostPageViews();
}
});