Skip to content
Snippets Groups Projects
Commit 3cbe0071 authored by Aditya Ghag's avatar Aditya Ghag
Browse files

cronchanges

parent 3a1aca97
No related branches found
No related tags found
1 merge request!3cronchanges
const { getValidatorArray } = require('./validators')
const { getValidatorArray } = require('./validators'); const {
const { sendNoticeEmails, sendErrorEmails, sendNotFoundEmails } = require('./email'); sendNoticeEmails,
const { getContactDetails } = require('./contacts'); sendErrorEmails,
const schedule = require('node-schedule'); sendNotFoundEmails,
const logger = require('./logger'); } = require('./email')
const cronstrue = require('cronstrue'); const { getContactDetails } = require('./contacts')
const schedule = require('node-schedule')
const logger = require('./logger')
const cronstrue = require('cronstrue')
const ERROR_CONTACTS = ['lawton@mpdl.mpg.de', 'ghag@mpdl.mpg.de'] const ERROR_CONTACTS = ['lawton@mpdl.mpg.de', 'ghag@mpdl.mpg.de']
const CC_CONTACTS = ['lawton@mpdl.mpg.de', 'ghag@mpdl.mpg.de'] const CC_CONTACTS = ['lawton@mpdl.mpg.de', 'ghag@mpdl.mpg.de']
const cronSchedule = '0 13 * * 1'; // const cronSchedule = '0 13 * * 1';
const cronSchedule = '0 */2 * * *'
// Set the schedule to run in cron format see helper https://crontab.guru/ // Set the schedule to run in cron format see helper https://crontab.guru/
// Format: // Format:
// minute hour day month day-of-week // minute hour day month day-of-week
// * 13 * * 1 // * 13 * * 1
// //
// 0 13 * * 1 ==> every Monday at 13:00 // 0 13 * * 1 ==> every Monday at 13:00
// 0 13 */5 * * ==> every fifth day of the month at 13:00 5th, 10th, 15th... // 0 13 */5 * * ==> every fifth day of the month at 13:00 5th, 10th, 15th...
// //
...@@ -22,22 +26,30 @@ if (process.env.NODE_ENV === 'development') { ...@@ -22,22 +26,30 @@ if (process.env.NODE_ENV === 'development') {
// schedule.scheduleJob('*/5 * * * * *', () => logger.log('Hi')); // schedule.scheduleJob('*/5 * * * * *', () => logger.log('Hi'));
checkValidatorsAndSendEmails() checkValidatorsAndSendEmails()
} else { } else {
schedule.scheduleJob(cronSchedule, checkValidatorsAndSendEmails); schedule.scheduleJob(cronSchedule, checkValidatorsAndSendEmails)
logger.log('The script is scheduled to run ' + cronstrue.toString(cronSchedule, { use24HourTimeFormat: true, verbose: true })); logger.log(
'The script is scheduled to run ' +
cronstrue.toString(cronSchedule, {
use24HourTimeFormat: true,
verbose: true,
}),
)
} }
function checkValidatorsAndSendEmails() { function checkValidatorsAndSendEmails() {
getValidatorArray() getValidatorArray()
.then(validatorsArray => validatorsArray.filter(validator => !validator.isUp3d)) // filter validators offline for 3 days. .then((validatorsArray) =>
.then(offlineValidatorsArray => { validatorsArray.filter((validator) => !validator.isUp3d),
logger.log("Got the online status for validators: "); ) // filter validators offline for 3 days.
return getContactDetails(offlineValidatorsArray); .then((offlineValidatorsArray) => {
logger.log('Got the online status for validators: ')
return getContactDetails(offlineValidatorsArray)
}) })
.then(({ offlineContacts, notFoundContacts }) => { .then(({ offlineContacts, notFoundContacts }) => {
return Promise.all([ return Promise.all([
sendNoticeEmails(offlineContacts, CC_CONTACTS), sendNoticeEmails(offlineContacts, CC_CONTACTS),
sendNotFoundEmails(notFoundContacts, ERROR_CONTACTS) sendNotFoundEmails(notFoundContacts, ERROR_CONTACTS),
]); ])
}) })
.then((promises) => { .then((promises) => {
// Debug SMTP responses. // Debug SMTP responses.
...@@ -45,11 +57,11 @@ function checkValidatorsAndSendEmails() { ...@@ -45,11 +57,11 @@ function checkValidatorsAndSendEmails() {
// logger.log(JSON.stringify(promises[0], 2)); // logger.log(JSON.stringify(promises[0], 2));
// logger.log('Error Email Responses:'); // logger.log('Error Email Responses:');
// logger.log(JSON.stringify(promises[1], 2)); // logger.log(JSON.stringify(promises[1], 2));
logger.log('✅ ✅ ✅ Run was successful ✅ ✅ ✅'); logger.log('✅ ✅ ✅ Run was successful ✅ ✅ ✅')
}) })
.catch(err => { .catch((err) => {
logger.error("SOMETHING WENT WRONG") logger.error('SOMETHING WENT WRONG')
logger.error(err.stack) logger.error(err.stack)
sendErrorEmails(ERROR_CONTACTS, err); sendErrorEmails(ERROR_CONTACTS, err)
}) })
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment