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 */ /* eslint no-global-assign: 0 no-console: 0 */
require = require('esm')(module); require = require('esm')(module);
const getopts = require('getopts'); const getopts = require('getopts');
const cron = require('node-cron');
const mod = require('../src/index.mjs').default; 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'], string: ['host', 'log'],
alias: { alias: {
h: 'host', h: 'host',
l: 'log', l: 'log',
i: 'index', i: 'index',
t: 'interval',
}, },
default: { default: {
host: 'localhost:9200', host: 'localhost:9200',
log: 'error', log: 'error',
index: 'adsb-data', index: 'adsb-data',
interval: 0,
}, },
}); });
mod(index, { elasticsearch }).catch(err => { function handleError(err) {
console.error(err); console.error(err);
process.exit(1); 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) { export async function createIndex(client, index) {
const realIndex = getIndexName(index); const realIndex = getIndexName(index);
return client.indices return client.indices
.create({ .create({
index: realIndex, index: realIndex,