-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcronjob.js
53 lines (45 loc) · 1.32 KB
/
cronjob.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
'use strict';
let glob = require('glob'),
_ = require('lodash'),
moment = require('moment'),
config = require('./config/config'),
mongoose = require('mongoose');
let cron = require('node-cron');
mongoose.connect(config.db);
let db = mongoose.connection;
db.on('error', function () {
throw new Error('unable to connect to database at ' + config.db);
});
db.once('open', function () {
console.log('CONNECTED TO MONGOOSE');
});
let models = glob.sync(config.root + '/app/models/*.js');
models.forEach(function (model) {
require(model);
});
let scrape = require('./app/modules/scrape');
console.log('CURRENT TIME:', moment().format());
cron.schedule('1 * * * *', () => {
console.time('test');
console.log('STARTED at:', moment().format());
let execute = () => {
let urls = config.urls;
let promises = [];
// lets fire 8 ajax calls to get data
urls.forEach(url => {
promises.push(scrape(url));
});
// console.log('promises', promises);
return Promise.all(promises);
};
execute()
.then(data => {
let dataLength = _.filter(data, obj => !_.isEmpty(obj)).length;
console.log('FINISHED DATA LENGTH', dataLength);
console.log('ENDED at:', moment().format());
console.timeEnd('test');
})
.catch(err => {
console.log('CRONJOB failed', err);
});
});