feat: add cron support

This commit is contained in:
2018-11-01 19:40:38 -07:00
parent 571e6dfd75
commit 0834274166
2 changed files with 28 additions and 3 deletions

View File

@@ -1,23 +1,47 @@
/* eslint no-global-assign: 0 no-console: 0 */
require = require('esm')(module);
const getopts = require('getopts');
const cron = require('node-cron');
const mod = require('../src/index.mjs').default;
const logger = require('../src/lib/logger.mjs').default;
const { index, ...elasticsearch } = getopts(process.argv.slice(2), {
const { index, interval, ...elasticsearch } = getopts(process.argv.slice(2), {
string: ['host', 'log'],
alias: {
h: 'host',
l: 'log',
i: 'index',
t: 'interval',
},
default: {
host: 'localhost:9200',
log: 'error',
index: 'adsb-data',
interval: 0,
},
});
mod(index, { elasticsearch }).catch(err => {
function handleError(err) {
console.error(err);
process.exit(1);
}
async function fetchAndIndex() {
return mod(index, { elasticsearch });
}
async function run() {
// initial kickoff
await fetchAndIndex().catch(handleError);
if (interval === 0) return;
// scheduled running
logger.debug(`Starting cron (${interval}s)...`);
cron.schedule(`${interval} * * * * *`, () => {
logger.debug('Running cron...');
fetchAndIndex();
});
}
run().catch(handleError);

View File

@@ -17,6 +17,7 @@ function getIndexName(index) {
export async function createIndex(client, index) {
const realIndex = getIndexName(index);
return client.indices
.create({
index: realIndex,