const cron = require('node-cron'), debug = require('debug')('maintainer:main'), isProd = (process.env.NODE_ENV && process.env.NODE_ENV.toLowerCase() == "production") || false, // dbcon = require('./db/connect'), dbcon = require('../server/helpers/db/connect'), models = require('../server/model'), mongoose = require('mongoose'), // Location = require('../shared/model/location')(mongoose), utils = require(isProd ? '../agmission/helpers/utils' : '../server/helpers/utils'), dbUtil = require('./db-utils')(mongoose), moment = require('moment'); debug("Is Prod", isProd); const runUTC = moment.utc(); process .on('uncaughtException', function (err) { debug(err); debug("Node NOT Exiting..."); process.exit(1); }) .on('unhandledRejection', (reason, p) => { debug(reason, 'Unhandled Rejection at Promise', p); }); /* * * * * * * | | | | | | | | | | | day of week | | | | month | | | day of month | | hour | minute second ( optional ) field value second 0-59 minute 0-59 hour 0-23 day of month 1-31 month 1-12 (or names) day of week 0-7 (or names, 0 or 7 are Sunday) */ const cleanMarkedDeleteData = isProd ? '0 1 * * 7' : `${runUTC.minute() + 2} ${runUTC.hour()} ${runUTC.date()} * *`; //if isProd run “At 01:00 on every 7th day-of-week.” cron.schedule(cleanMarkedDeleteData, async () => { debug("Start cleanMarkedDeleteData Task at %s ...", moment.utc().toISOString()); const delCustomers = await models.Customer.find({ markedDelete: true }); debug(`There are ${delCustomers.length} to delete !`); debug("cleanMarkedDeleteData is Done."); }, { scheduled: true, timezone: "Etc/UTC" }); // const cleanTempFilesTiming = isProd ? '5 1 * * 7' : `*/30 * * * * *`; // // In Production mode, run “At 01:05 on every 7th day-of-week.” // cron.schedule(cleanTempFilesTiming, () => { // debug("Start cleaning temp. files ...", moment.utc().toISOString()); // const tempPaths = isProd ? // ['/media/ssd1/agmission/.tmp/', '/media/ssd1/agmission/reports/dat/'] : // ['/media/data/trung/work/AgMission/trunk/Development/server/.tmp/', '/media/data/trung/work/AgMission/trunk/Development/server/reports/dat/']; // const numOfDays = isProd ? 7 : 1; // let delCmd = tempPaths.map(p => `find ${p} -mindepth 1 -mtime +${numOfDays} -daystart -prune -exec rm -f -R {} \\;`); // delCmd = delCmd.join(' ; '); // utils.execAsync(delCmd) // .then(() => { // debug("Cleaning temp files is Done."); // }) // .catch(err => { // debug(err.stack); // }); // }, // { // scheduled: true, // timezone: "Etc/UTC" // }); // const updateObstaclesTiming = isProd ? '30 1 1 */1 *' : `${runUTC.minute() + 1} ${runUTC.hour()} ${runUTC.date()} * *`; // // In Production mode, run “At 01:30 on day-of-month 1 in every month.” // cron.schedule(updateObstaclesTiming, () => { // const task = 'Updating obstacles'; // debug(`Start ${task} ...`, moment.utc().toISOString()); // dbUtil.updateFAAObstaclesASync() // .then(() => { // }) // .catch(err => { // debug(isProd ? err.message : err.stack); // }) // .finally(() => { // debug(`${task} is Done.`); // }); // }, // { // scheduled: true, // timezone: "Etc/UTC" // });