2019-10-21 22:38:27 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
const CronJob = require('cron').CronJob;
|
2019-10-30 20:11:57 +00:00
|
|
|
const TimeFormat = require('hh-mm-ss');
|
2019-10-21 22:38:27 +00:00
|
|
|
|
|
|
|
// load env variables from file
|
|
|
|
require('dotenv').config();
|
|
|
|
|
|
|
|
const ChangeDetection = require('./scrapers/scraper');
|
|
|
|
|
|
|
|
async function run() {
|
|
|
|
const cdScraper = new ChangeDetection();
|
2019-10-30 18:35:06 +00:00
|
|
|
let startTime = new Date();
|
|
|
|
let startTimeStamp = startTime.getTime();
|
|
|
|
const delay = process.env.DELAY || 600000;
|
2019-10-21 22:38:27 +00:00
|
|
|
|
2020-07-09 15:34:01 +00:00
|
|
|
console.log('RUN MODE::', process.env.NODE_ENV || 'NOT SET!!');
|
|
|
|
|
2019-10-21 22:38:27 +00:00
|
|
|
if (typeof(process.env.CD_CRON) === 'string' ) {
|
|
|
|
console.log(`${cdScraper.id} cron set for ${process.env.CD_CRON}`);
|
2019-10-29 12:25:06 +00:00
|
|
|
await cdScraper.run();
|
|
|
|
new CronJob(process.env.CD_CRON, async () => {
|
2019-10-21 22:38:27 +00:00
|
|
|
console.log('go');
|
|
|
|
await cdScraper.run();
|
2019-10-29 12:25:06 +00:00
|
|
|
console.log('ready to go again..');
|
2019-10-21 22:38:27 +00:00
|
|
|
}, null, true);
|
|
|
|
}
|
|
|
|
|
2019-10-29 12:25:06 +00:00
|
|
|
if (typeof(process.env.CD_EVERY) === 'string' ) {
|
|
|
|
console.log(`${cdScraper.id}_EVERY set for ${process.env.CD_EVERY} seconds`);
|
|
|
|
|
|
|
|
console.log('++');
|
|
|
|
await cdScraper.run();
|
|
|
|
console.log('--');
|
|
|
|
setInterval(async () => {
|
2019-10-30 18:35:06 +00:00
|
|
|
const now = new Date();
|
|
|
|
const nowTS = now.getTime();
|
2019-10-30 20:11:57 +00:00
|
|
|
const m = parseInt(process.env.CD_EVERY, 10) - (nowTS - startTimeStamp);
|
|
|
|
const remains = TimeFormat.fromMs(m, 'hh:mm:ss');
|
2019-10-30 18:35:06 +00:00
|
|
|
|
2019-10-30 20:11:57 +00:00
|
|
|
console.log(`${remains} to go...`);
|
2019-10-30 18:35:06 +00:00
|
|
|
|
|
|
|
if (nowTS - startTimeStamp > process.env.CD_EVERY) {
|
|
|
|
await cdScraper.run();
|
|
|
|
console.log('ready to go again..');
|
|
|
|
startTime = new Date();
|
|
|
|
startTimeStamp = startTime.getTime();
|
|
|
|
}
|
|
|
|
}, delay);
|
2019-10-29 12:25:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (process.env.SCRAPE_START === cdScraper.id) {
|
2019-10-21 22:38:27 +00:00
|
|
|
console.log('go');
|
|
|
|
await cdScraper.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('Change Detection Launched');
|
|
|
|
}
|
|
|
|
|
|
|
|
process.once('uncaughtException', function caught(err) {
|
|
|
|
console.error('Uncaught', err);
|
|
|
|
});
|
|
|
|
|
|
|
|
run();
|
|
|
|
|