Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'use strict';
var CronJob = require('cron').CronJob;
var counter = 0;
new CronJob('* * * * * *', function () { // eslint-disable-line no-new
counter += 1;
}, null, true, 'America/Los_Angeles');
setTimeout(function () {
if (counter === 4) console.log('ok');
process.exit();
}, 4500);
.catch(error => {
console.error(error.message)
Healthcheck.isRunning = false
Healthcheck.error = error
// When not running job continuously stop the server
if (!options.cron) {
server.close()
}
})
}
// Setup CRON job if required
let cronJob
if (options.cron) {
console.log('Scheduling job with cron pattern ' + options.cron)
cronJob = new CronJob(options.cron, () => {
// If last job has not yet finished skip this call as we are late
if (!Healthcheck.isRunning) {
Healthcheck.nbSkippedJobs = 0
runJobWithOptions()
} else {
console.log('Skipping scheduled job as previous one is not yet finished')
Healthcheck.nbSkippedJobs++
}
})
// In case the server is forced to exit stop the job as well
server.on('close', () => {
cronJob.stop()
console.log('Stopped scheduled job with cron pattern')
})
}
// Run job
// ๆฏๆไธ็น
new CronJob('01 01 03 * * *', function() {
// ๆดๆฐๅฎ้ชๅฎค้็Githubๆฐๆฎ
updateLabsDataFromGithub.all()
// ๆดๆฐไธชไบบGithubไฟกๆฏ
myGithubData.update()
// ๆดๆฐๅงไธญไบบ็ๆๅๅๆฐๆฎ
updateMoment.sync()
// ๆดๆฐ720ไบๆฐๆฎ
my720Data.update()
// ๆดๆฐๅพ่ซๆฐๆฎ
myTuchongData.update()
}, null, true, 'Asia/Hong_Kong')
// ๆฏๆไธ็น้ถๅๅ
new CronJob('01 10 03 * * *', function() {
// ๆธ
้คๅ
จ้จ็ผๅญ
app.cache.clear()
}, null, true, 'Asia/Hong_Kong')
}
OTP.timer = function() {
OTP.state.cron = new cron('0 * * * * *', function() {
OTP.printPin();
}, null, true, "America/New_York");
}
file,
size: parseInt(stat.size / 1000.0),
date: stat.mtime,
retention: parseInt(retention)
};
};
const getRetentionPeriod = stat => {
return (
MIN_AGE +
(-MAX_AGE + MIN_AGE) *
Math.pow(parseInt(stat.size / 1000.0) / MAX_SIZE - 1, 3)
);
};
const job = new CronJob(
cron.schedule,
async () => {
logger.info('Running Cron job for deleting files');
const files = await readDirAsync(uploadsPath());
for (const file of files) {
const fileStats = getFileStats(file);
const diff = differenceInDays(new Date(), fileStats.date);
if (diff > fileStats.retention) {
await deleteAsync(uploadsPath(fileStats.file));
await db.sadd(`deleted`, fileStats.file);
const shortName = fileStats.file.split('.')[0];
await db.del(`short:${shortName}`);
logger.info('Successsfully deleted the file ' + fileStats.file);
}
}
},
function addCron(jobId, job, cronExpression, configData) {
const jobConnector = require(`./${configData.job_platform.toLowerCase()}/jobConnector`);
let scheduledJob = new CronJob(cronExpression, async function () {
try {
if (job.enabled === false) {
logger.info(`Skipping job with id: ${jobId} as it's currently disabled`);
} else {
let latestDockerImage = await dockerHubConnector.getMostRecentRunnerTag();
let runId = Date.now();
let jobSpecificPlatformConfig = createJobRequest(jobId, runId, job, latestDockerImage, configData);
await jobConnector.runJob(jobSpecificPlatformConfig);
}
} catch (error) {
logger.error({ id: jobId, error: error }, 'Unable to run scheduled job.');
}
}, function () {
logger.info('Job: ' + jobId + ' completed.');
}, true);
cronJobs[jobId] = scheduledJob;
await Promise.all(
projects.map(async project => {
project.members.map(member => {
sendPublishProjectNotificationEmail(member.user.email, project);
});
await entityManager.update(ProjectModel, project.id,
{ publishNotificationsSent: project.publishNotificationsSent + 1 });
})
);
}
catch (error) {
logger.error(error);
}
};
new CronJob('00 00 14 * * 1-5', emailNotificationsHandler, null, true);
logger.info('Cron job started');
};
case 'seconds':
cron = "*/" + config.timeout + " * * * * *";
break;
case 'minutes':
cron = "0 */" + config.timeout + " * * * *";
break;
case 'hours':
cron = "0 0 */" + config.timeout + " * * *";
break;
case 'days':
cron = "0 0 0 */" + config.timeout + " * *";
break;
default:
break;
}
node.job = new cron_1.CronJob(cron, cronCheckJob.bind(null, node, config));
node.job.start();
node.on('close', function () {
node.job.stop();
});
}
cronCheckJob(this, config);
}
catch (err) {
node.error('Error: ' + err.message);
node.status({ fill: "red", shape: "ring", text: err.message });
}
}
function cronCheckJob(node, config) {
case 'seconds':
cron = `*/${config.timeout} * * * * *`;
break;
case 'minutes':
cron = `0 */${config.timeout} * * * *`;
break;
case 'hours':
cron = `0 0 */${config.timeout} * * *`;
break;
case 'days':
cron = `0 0 0 */${config.timeout} * *`;
break;
default:
break;
}
node.job = new CronJob(cron, cronCheckJob.bind(null, node, config));
node.job.start();
node.on('close', () => {
node.job.stop();
});
}
cronCheckJob(this, config);
}
catch (err) {
node.error('Error: ' + err.message);
node.status({ fill: "red", shape: "ring", text: err.message })
}
}