Cron job scheduled not running automatically in Node.js BOT - node.js

I am working on a Bot, using BotFramework-node.js hosted on Azure.
I am using four cronjobs in my bot to run different functions at different points of a day. The only issue is that the jobs are not running automatically. They will run normally if i compile/run the code, but wont if i don't compile/run it within 12 hours i.e, if i run the code manually today morning, the jobs will execute fine for today but tomorrow it won't unless i run it again.
Tried changing the modules. I have tried using node-schedule, node-cron. Now using cron
var Name = new CronJob({
cronTime:'0 15 11 * * 0-6',
onTick: function(){
//function call / another js file call
}
});
Name.start();

Related

Background process in node

I am new to the whole javascript stack .I have been trying to learn by building a small application based on React-Express-Mongo. My application basically saves some config settings to mongo . And based on these settings the app periodically tries to fetch some values by querying and elasticsearch index.
So far i have the part to save the config settings done.
What I need to do now ,is to extract these setting from my mongo DB and schedule a job which keeps runnning periodically (the period is one of the settings) to poll my elastic index.The thing that i cannot wrap my head around ,is how do I create this scheduled job. All i have been using till now is the Express router to interact with my UI and DB.
I did some research ,would spawning a child process be the ideal way to go ahead with this ?
I would suggest you to go take a look to node-corn. Cron is a popular task scheduler on UNIX and node-cron is its implementation in node.
Basic usage - taken from the doc
var CronJob = require('cron').CronJob;
new CronJob('* * * * * *', function() {
console.log('You will see this message every second');
}, null, true, 'America/Los_Angeles');

Is it possible to run a node.js app in Heroku every *4 minutes*?

I have an app I want to run in Heroku (the free version) every 4 minutes from like 10am-10pm Monday-Friday. I used to do this using a cron job in Digital Ocean but I'm trying to migrate over to Heroku since it's free. But is this even possible? From everything I can see it looks like you can only run stuff every 10 minutes on Heroku.
It is a simple app that sends email updates every 4 minutes.
Thank you.
Heroku doesn't give you a way to run things every 4 minutes. As you mentioned, the lowest the Scheduler add-on will give you is 10 minutes.
You could setup a process running all the time, and enqueuing/processing your tasks every 4 minutes though.
As mentioned in garson's comment, node-cron can handle this very easily for you.
For example, the following code:
var CronJob = require('cron').CronJob;
new CronJob('* */4 * * * *', function() {
console.log('You will see this message every 4 minute');
}, null, true, 'America/Los_Angeles');
Will show the console message every 4 minutes.
You can run it with node index.js.
So if you put the following line in your Procfile with the name cron and deploy your app, you can do:
heroku ps:scale cron=1
And the process will indefinitely, executing your code every 4 minutes.

Make scheduled tasks with node-schedule (using forever) persist after a restart

I want to develop a node.js program that will be executed at a specific time using a job scheduler (node-schedule).
This program is running in the background using forever (node.js module).
Here's the content of my app.js :
var schedule = require('node-schedule');
~
~
var id = request.body.id;
var scheduled = schedule.scheduledJobs;
if(scheduled[id]!=null){
//Tasks
}
else{
scheduled[id].cancel;
delete scheduled[id];
}
But if app.js is killed by any reason,
the schedule object is removed.
and sometimes app.js is restarted by forever.
how can I handle node-schedule objects ?
I have faced similar problem recently and there are two solutions:
1. Use actual cron
2. Use database
I solved my problem by using database. Each time you are creating some event save it to database. In your app.js file when the application is starting make function reading the database and creating scheduled events accordingly.
The first option is better if you do not have dynamic tasks, you do not create new tasks or if they are always the same.

Setting up cron job with Bolt on Plesk 11.5.30

I'm trying to setup a cron job with Bolt.cm via the Plesk 11.5.30 admin console.
Bolt task scheduler documentation: https://docs.bolt.cm/v20/tasks
Scheduled Tasks
I have setup crontab for the apache user as follows:
Min H DM M DW Command
0 */1 * * * /var/www/vhosts/mysite.com/httpdocs/app/nut cron
0 */1 * * * httpdocs/app/nut cron
The reason for two different commands here is that I've read in another thread troubleshooting Plesk that the command path should be relative from the domain. I assume if the first one fails, the second will still run. I also assume the above will run cron every hour.
Bolt Config
In my config file the cron_hour is set to 3am. However as the listener is set for an hourly event CRON_HOURLY I assume the setting is bypassed/ignored. Either way this setup has been running for over 48 hours and no effect has yet been witnessed.
Bolt Setup
As a test I have added the following into my Bolt extension so that it should simply send me an email when it is run (the following has been cut down to keep it short).
use \Bolt\CronEvents;
class Extension extends \Bolt\BaseExtension
{
function initialize() {
$this->app['dispatcher']->addListener(CronEvents::CRON_HOURLY, array($this, 'cronNotifier'));
}
function cronNotifier() {
// send email script...
}
}
I've tested my email script individual so I know it works. With the cron job setup and assumed to be running this function is never hit. What's more is that in the database the bolt_cron table remains to be empty.
If anyone could help suggest some things I could try to get this running it would be greatly appreciated.
THE FIX
In order to fix this issue I altered my schedule to execute every 5 minutes (quicker debug loop). I then made the following discoveries:
I had to set execute permissions on public for the nut file (so apache user could execute it)
for the server I'm using only the full path version works
Thanks #Gawain for prompting me in the right direction.
Scheduled Tasks
Yes, that looks correct. The handling of directory locations was enhanced recently, but you used to need to call nut from in the Bolt directory.
Bolt Config
cron_hour only applies to daily, and longer periods. There was a bug I fixed recently that was blocking out midnight to 3 am hourly tasks.
OK, there should be traces logged in the Bolt log about running cron events.
If you run this, do you see the hourly job trigger and your email sent?
./app/nut cron --run=cron.Hourly
If that works, then the problem is with the system cron/crontab, if editing directly were you using crontab -e?
Also if you're on an older checkout of master, you can try to prefix the crontab command section with
cd /var/www/vhosts/mysite.com/httpdocs &&
...before each invocation of ./app/nut

Help getting first web2py Cron Task working

I'm running web2py locally with Windows 7 and live on a Linux Ubuntu
server and I haven't been able to get my cron job to run in either.
My crontab looks like this:
*/1 * * * * root *autoemail/send_autoemails
and my function works fine when called manually. It also ends with
db.commit()
Other than that I don't know what else to do get it working although I
really didn't understand all of the web2py book section on Cron,
specifically when it came to soft/hard/external cron and all of that.
I saw a web2py thread that perhaps cron was going to be replaced?
Perhaps that has something to do with this? Is there something else I need to do to configure cron before it will work?
Any ideas about how I can troubleshoot this are greatly appreciated.
On this moment web2py is changing from Cron to Scheduler, with newer web2py versions Cron is disabled by default.
You can use your function with the Scheduler, putting it into a model file and passing it to the scheduler creator class, in order to enable a new Scheduler instance with it:
# New File applications/yourapp/models/zfunctions.py
#
def send_autoemails():
...
...#Your code here
...
...
from gluon.scheduler import Scheduler
Scheduler(db,dict(yourfunction=send_autoemails))
After that you can add a new job simply from the web2py db admin interface,
under db.task_scheduled you must click on insert new task_scheduled and set
period to run, repeats, timeouts, enable, disable, etc....
Here are some info about it: http://web2py.com/book/default/chapter/04#Scheduler-(experimental)

Resources