/* eslint no-global-assign: 0 no-console: 0 */ require = require('esm')(module); const getopts = require('getopts'); const runInterval = require('interval-promise'); const mod = require('../src/index.mjs').default; const logger = require('../src/lib/logger.mjs').default; const esHost = process.env.ELASTICSEARCH_HOST || 'localhost'; const esPort = process.env.ELASTICSEARCH_PORT || '9200'; const auth = process.env.ELASTICSEARCH_AUTH || ''; const { index, interval, lat, lon, radius, ...esConfig } = getopts(process.argv.slice(2), { string: ['host', 'auth', 'log', 'index'], alias: { h: 'host', l: 'log', i: 'index', t: 'interval', }, default: { host: `${esHost}:${esPort}`, log: 'error', index: 'adsb-data', interval: 0, lat: 33.433638, lon: -112.008113, radius: 200, }, }); function handleError(err) { console.error(err); process.exit(1); } async function fetchAndIndex() { return mod(index, { elasticsearch: { ...esConfig, auth }, filter: { lat, lon, radius } }); } async function run() { // initial kickoff await fetchAndIndex().catch(handleError); // listen for termination const terminate = type => () => { console.log(`Terminating [${type}]`); process.exit(0); }; process.on('SIGINT', terminate('SIGINT')); process.on('SIGTERM', terminate('SIGTERM')); // scheduled running if (interval === 0) return; logger.debug(`Starting interval (${interval}s)...`); runInterval(() => fetchAndIndex().catch(handleError), interval * 1000); } run().catch(handleError);