#!/usr/bin/env node const CronJob = require('cron').CronJob; const TimeFormat = require('hh-mm-ss'); // load env variables from file require('dotenv').config(); const ChangeDetection = require('./scrapers/scraper'); async function run() { const cdScraper = new ChangeDetection(); let startTime = new Date(); let startTimeStamp = startTime.getTime(); const delay = process.env.DELAY || 600000; console.log('RUN MODE::', process.env.NODE_ENV || 'NOT SET!!'); if (typeof(process.env.CD_CRON) === 'string' ) { console.log(`${cdScraper.id} cron set for ${process.env.CD_CRON}`); await cdScraper.run(); new CronJob(process.env.CD_CRON, async () => { console.log('go'); await cdScraper.run(); console.log('ready to go again..'); }, null, true); } 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 () => { const now = new Date(); const nowTS = now.getTime(); const m = parseInt(process.env.CD_EVERY, 10) - (nowTS - startTimeStamp); const remains = TimeFormat.fromMs(m, 'hh:mm:ss'); console.log(`${remains} to go...`); if (nowTS - startTimeStamp > process.env.CD_EVERY) { await cdScraper.run(); console.log('ready to go again..'); startTime = new Date(); startTimeStamp = startTime.getTime(); } }, delay); } if (process.env.SCRAPE_START === cdScraper.id) { console.log('go'); await cdScraper.run(); } console.log('Change Detection Launched'); } process.once('uncaughtException', function caught(err) { console.error('Uncaught', err); }); run();