Skip to content

Instantly share code, notes, and snippets.

@yatishbalaji
Last active March 17, 2021 05:26
Show Gist options
  • Select an option

  • Save yatishbalaji/486ade9dad4892c888b14f5cfecfb278 to your computer and use it in GitHub Desktop.

Select an option

Save yatishbalaji/486ade9dad4892c888b14f5cfecfb278 to your computer and use it in GitHub Desktop.
node cron
#!/usr/bin/env node
const debug = require('debug');
const program = require('commander');
const bluebird = require('bluebird');
const {
GRONIT_URL, CALCULATE_TAT_GRONIT_ID, NODE_ENV,
} = require('../config/environment');
const log = (...args) => {
console.log(...args);
debug('cron:client: nodecron')(...args);
};
module.exports = class Nodecron {
constructor() {
this.init();
this.parallel = +this.program.parallel || 5;
}
init() {
this.program = program;
program
.version('1.0.0')
.option('-p, --parallel <d>', 'Number of parallel processes')
.parse(process.argv);
}
async main() {
try {
try {
if (NODE_ENV !== 'development') {
await rp.get(`${GRONIT_URL}/run/${CALCULATE_TAT_GRONIT_ID}`);
}
} catch (err) {
logger.error('Gronit error: ', err);
}
await bluebird.map([{email: 'yat@gmail.com'}, {email: 'yat1@gmail.com'}], async (data) => {
await this.sendMail(data);
}, { concurrency: this.parallel });
log('Nodecron completed successfully');
} catch (err) {
log('Nodecron failed: ', err);
} finally {
if (NODE_ENV !== 'development') {
await rp.get(`${GRONIT_URL}/complete/${DAILY_MAIL_GRONIT_ID}`);
}
}
return true;
}
static async sendMail(data) {
try {
log("mail sent", data);
return true;
} catch (err) {
log('Nodecron mail failed for : ', data, err);
return true;
}
}
};
// node cron runner: https://gist.github.com/yatishbalaji/56ceffa48c928aec1e2b8ca1cf532d54
// run command: ./cron nodecron -p 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment