52 lines
1.3 KiB
JavaScript
52 lines
1.3 KiB
JavaScript
/* 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 { index, interval, lat, lon, radius, ...elasticsearch } = getopts(process.argv.slice(2), {
|
|
string: ['host', 'auth', 'log', 'index'],
|
|
alias: {
|
|
h: 'host',
|
|
u: 'auth',
|
|
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() {
|
|
logger.debug('Fetching and indexing data...');
|
|
return mod(index, { elasticsearch, filter: { lat, lon, radius } });
|
|
}
|
|
|
|
async function run() {
|
|
// initial kickoff
|
|
await fetchAndIndex().catch(handleError);
|
|
|
|
// scheduled running
|
|
if (interval === 0) return;
|
|
logger.debug(`Starting interval (${interval}s)...`);
|
|
runInterval(() => fetchAndIndex().catch(handleError), interval * 1000);
|
|
}
|
|
|
|
run().catch(handleError);
|