Nodejs once every eight hours - node.js

I am trying to impliment into my nodejs script a function to allow once per 8 hours a select command.
example:
!hug <--- would let bot respond with a hug but only once every 8 hours
I've been scouring online but cannot find what I need... I am trying to get it as simplified as possible.. (i.e without mongo... etc)

You can use node-schedule for this and for more versatility where you can configure days, hours and minutes and cancel on particular conditions being met this also gives you to use cron expressions as well.
var schedule = require("node-schedule");
var rule = new schedule.RecurrenceRule();
//Will run at 1am 9am and 5pm
rule.hour = [1, 9, 17];
var task = schedule.scheduleJob(rule, function(){
//Do Stuff
console.log("Scheduled Task Running...")
/*
if(condition met)
task.cancel();
*/
});

You can use node-cron
var CronJob = require('cron').CronJob;
var job = new CronJob('00 30 11 * * 1-5', function() {
/*
* Runs every weekday (Monday through Friday)
* at 11:30:00 AM. It does not run on Saturday
* or Sunday.
*/
}, function () {
/* This function is executed when the job stops */
},
true, /* Start the job right now */
timeZone /* Time zone of this job. */
);

Related

How to schedule cron job in nodejs for a time range

I am trying to schedule a Cron Job between 9AM to 9PM for every 15 minutes. I am able to schedule it for every 15 minutes, but not for the time duration. Below is the code snippet
const CronJob = require('cron').CronJob
const splunkNode = require('./splunk_node')
let job = new CronJob("*/15 08-21 * * * 1-5", function(){
console.log('ran at ', new Date())
// CRON JOB()
},function(){
console.log('Job stopped')
},true,'America/Los_Angeles')
job.start()
Does the syntax for specifying range has to be enclosed between brackets[]?
A lil workaround is to schedule a function that calls itself after a certain amount of time
function callCronJob (){
const job = new CronJob("*/15 08-21 * * * 1-5", function(){
// if time range we are in doesn't fall between 9AM and 9PM cancel this job and call // this function again with it's 9AM
// next part is pseduocode cause I am lazy to write actual code
if ( 9am >timeRange > 9pm ){
job.cancel()
new cronJob(timeRange+ 12 hours , ()=>{
callCronJob();
});
return;
}
// CRON JOB()
},function(){
console.log('Job stopped')
},true,'America/Los_Angeles')
job.start()
}

How to run cron job 4 times in a day using nodejs?

I have written on cron job to update users table using Nodejs. I have used cron node package. I need to run cron 4 times a day. I have written 2 crons. The first cron will run 4 times a day with particular time duration and will start the second cron. The second cron will run in every 5 minutes till all users updated. after updating of users the second cron will stop.
sample code :
//This cron will run at every 5 minutes
var job = new CronJob({
cronTime: '*/5 * * * *',
onTick: function() {
//cron runs in every 5 min;
/* API logic */
// when all user updated this cron will stopped
job.stop();
},start: false
});
//runs on 7est,8est,9est,10est,11est
var mainjob = new CronJob('0 0 7-11 * * *', function() {
job.start();
}, function () {},true
);
First time It's running well but the second time not started. Can you please advice on this?

Run Node task/function at specific time daily while accounting for Daylight Savings

I'm trying to run a Node task/function at a specific time each day (7am Eastern Time) regardless of Daylight Savings. I've tried cron-based packages, but cron doesn't seem to account for it. The server the app is running on is on GMT/UTC, so that that needs to be taken into consideration as well. Here is my current code:
const schedule = require('node-schedule');
...
const j = schedule.scheduleJob('0 12 * * *', function(){
bot.channels.get(getDefaultChannel().id).send("Hello! Your daily fact for today is", { embed: generateEmbed(getRandomFact()) });
});
This works fine, but since we just moved ahead an hour, the message appears at 8am instead of 7am.
This is a tricky one, I've played about with a couple of ways of doing this, I think the Cron library works the best (https://www.npmjs.com/package/cron). You can schedule a job to run at 7 am in the US/Eastern timezone.
"use strict";
var cron = require('cron');
var job1 = new cron.CronJob({
cronTime: '0 7 * * *',
onTick: function() {
bot.channels.get(getDefaultChannel().id).send("Hello! Your daily fact for today is", { embed: generateEmbed(getRandomFact()) });
},
start: true,
timeZone: 'US/Eastern'
});

How to setup node-schedule for every day at 12am

I am using node-schedule to schedule my tasks. Now I need to schedule a job everyday at 12am.
Below given is the code I am using,
var rule3 = schedule.scheduleJob('00 00 00 * * *', function(){
console.log('my test job!');
});
This is not working for me.
Any help appreciated. Thanks in advance.
You can use node-cron module simply.
var CronJob = require('cron').CronJob;
var job = new CronJob('00 00 12 * * 0-6', function() {
/*
* Runs every day
* at 12:00:00 AM.
*/
}, function () {
/* This function is executed when the job stops */
},
true, /* Start the job right now */
timeZone /* Time zone of this job. */
);
Read docs for more pattern.
For anyone who is stuck on this also check what time your app is operating in. Your app by default might be in Greenwich Mean Time which would definitely make you think your schedule is not working. Toss a console.log(Date.now()) in a convenient location and check. You might just have to adjust the time on your schedule by a couple hours.

node JS: How to run a task for every 30 minutes , including now

I'm running CronJob Every 30 mins.
But it is not running at the time of start.
How can i make it run at the time of start as well as every 30 min?
var CronJob = require('cron').CronJob;
console.log('started ' + new Date());
var job = new CronJob('0 */30 * * * *', function() {
myJob()
}, function () {
},
true,
'Indian/Mauritius'
);
job.start();
function myJob()
{
console.log('in Job');
console.log(new Date());
}
Output
started Mon Sep 21 2015 18:44:29 GMT+0530 (IST)
You could run myJob() and then start your cronjob.
or ...
you could change the way you use cron so that it runs at particular times, say now and every half hour like this:
var job = new CronJob('* 16,46 * * * *', // etc
Say the time is now 15:45, the above command will run in a minute (i.e. on the 16th and 46th minute of every hour). With a bit more code, you could generate the string "16,46" to be one minute from now and 30 minutes after that.

Resources