feat: add cron support
This commit is contained in:
30
bin/index.js
30
bin/index.js
@@ -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);
|
||||
|
||||
@@ -17,6 +17,7 @@ function getIndexName(index) {
|
||||
|
||||
export async function createIndex(client, index) {
|
||||
const realIndex = getIndexName(index);
|
||||
|
||||
return client.indices
|
||||
.create({
|
||||
index: realIndex,
|
||||
|
||||
Reference in New Issue
Block a user