-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
executable file
·47 lines (41 loc) · 1.15 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
require('colors');
const moment = require('moment');
const crawlNodes = require('./src/crawlNodes');
const geolocateNodes = require('./src/geolocateNodes');
const save = require('./src/saveCrawl');
const config = require('./src/config');
const db = require('knex')(config.get('db'));
const log = require('./src/logger');
const CRAWL_INTERVAL = 120 * 1000;
const INTERVAL = 24 * 60 * 60 * 1000;
// geolocate nodes
const geolocate = () => {
geolocateNodes().catch(log.error);
};
// run crawl
const doCrawl = () => {
crawlNodes(config.get('entry'))
.then(save)
.catch(error => {
log.error(error);
log.info('failed to record crawl');
});
};
// purge old data
const purge = () => {
const MAX = moment.utc().subtract(90, 'days');
const TABLES = [ 'crawls', 'location'];
log.info('purging old data');
TABLES.forEach(name => {
const column = name === 'crawls' ? 'start' : 'updated';
db(name)
.where(column, '<', MAX)
.del()
.then(count => log.info(`${count} '${name}' rows deleted`))
.catch(log.error);
});
};
doCrawl();
setInterval(doCrawl, CRAWL_INTERVAL);
setInterval(geolocate, INTERVAL);
setInterval(purge, INTERVAL);