Skip to content

Instantly share code, notes, and snippets.

@ubaid-me
Last active May 21, 2017 13:50
Show Gist options
  • Save ubaid-me/1730de9ebd8f38a4dfa8e62ef082ee11 to your computer and use it in GitHub Desktop.
Save ubaid-me/1730de9ebd8f38a4dfa8e62ef082ee11 to your computer and use it in GitHub Desktop.
NodeJS - perform an activity between a specific start and end time
# set your crontab cron like this, to check after every 5 minutes let's say
*/5 * * * * /home/ub/local/bin/node /mnt/d/tmp/cron001/cron.js >> /mnt/d/tmp/cron001/activtyOn21-5.log
# for every minute
#* * * * * /home/ub/local/bin/node /mnt/d/tmp/cron001/cron.js >> /mnt/d/tmp/cron001/activtyOn21-5.log
var fs = require('fs');
var dateTimeToStartActivityString = 'Sat May 20 2017 17:38:00.00 GMT+0500';
var dateTimeToStopActivityString = 'Sat May 20 2017 17:39:00.00 GMT+0500';
var dateTimeToStartActivity = new Date(dateTimeToStartActivityString);
var dateTimeToStopActivity = new Date(dateTimeToStopActivityString);
var start_activity_lock_file = 'start_activity.lock';
var currentDateTime = new Date();
function startActivity() {
console.log('difference > 0 time to stop');
console.log('stopping site');
}
function startActivityInternal() {
if (!fs.existsSync(start_activity_lock_file)) {
startActivity();
fs.closeSync(fs.openSync(start_activity_lock_file, 'w'));
console.log('lock file created');
} else {
console.log('site was down when required');
}
}
function stopActivity() {
console.log('make the site up again');
}
function main() {
var difference = currentDateTime - dateTimeToStartActivity; // difference in milliseconds
console.log('dateTimeToStartActivity', dateTimeToStartActivity);
console.log('currentDateTime', currentDateTime);
console.log('difference = ', difference);
if (difference > 0) {
startActivityInternal();
} else {
console.log('difference < 0 ' + (difference / 1000) + ' seconds remaining before starting task');
}
var newDiff = currentDateTime - dateTimeToStopActivity;
if (currentDateTime > dateTimeToStopActivity) {
stopActivity();
} else {
console.log('newDiff ' + (newDiff / 1000) + ' seconds remaining before we make the site up again');
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment