I use a web server (o2Switch) who use a Cpanel interface, my nodejs app is running with the setup node.js module.
My rest api use Nodejs with express and a database mongoDb atlas, my front is a Vuejs app, everythings works fine in production, but i have one cron with node-cron package:
// scripts/myscript.js
const cron = require('node-cron');
const script = cron.schedule('00 20 * * *', async () => {
console.log('PASSED')
},
{
scheduled: true,
timezone: "Europe/Paris"
}
);
script.start()
The script is import inside app.js:
// Scripts
require('./scripts/myscript')
My nodecron works fine in local and i don't have any trouble, but on production is not executed.
I do a lot of test:
It's 10:00(am) and i setup my cron on 05 10 * * *, this will work. But is never reexecuted the following day.
It's 10:00(am) and i setup 00 12 * * * this will never works.
I setup every minute * * * * * this works every minute.
What is seems, is my server is like standby after a couple of hours (i don't know if it's possible).
My vuejs app works fine at any time, the request works and do not have trouble.
Related
Have a heroku app running on free tier that is working fine (it's a nodejs/express rest app).
Trying to add a clock process that fetches some content via https request and saves that content locally on the server.
It runs famously when I run locally (heroku local).
When I deploy to heroku, it doesn't run at all (the web app runs fine.)
I tried reducing the app to a simple case that only does a console.log() on the timer tick, but - nothing.
I also tried specifying the clock process as a worker in the Procfile - heroku doesn't see it (but heroku local does...).
My understanding is this is supposed to work ok in the free tier, but I'm having doubts.
Here's my (simple) Procfile:
web:npm start
clock:npm run start-clock
the scripts part of my package.json:
"scripts": {
"start": "node ./bin/www",
"start-clock": "node ./bin/clockproc.mjs"
},
and the simplified clockproc.mjs file (I'm using the node-cron package:
import cron from 'node-cron';
const TESTING = true;
const INTERVAL = TESTING? "*/15 * * * * *" : "0 10 * * *";
let task = cron.schedule(INTERVAL, async () => {
console.log("==> got a a tick...")
});
task.start();
Any clue why this isn't working?
This script
0 0/3 * * * node test.js
is used to schedule the job in Ubuntu, How to set the same way in Windows using node-schedule npm package?
As a work around , have scheduled the script in Task Scheduler:
cmd /c c:\node\node.exe c:\myscript.js
I want to to know how this can be done in node-schedule npm package.
You can use 'cron' package for schedule functions on nodejs -
https://www.npmjs.com/package/cron
According it's docs
Cron is a tool that allows you to execute something on a schedule.
This is typically done using the cron syntax. We allow you to execute
a function whenever your scheduled job triggers.
Usage example
const CronJob = require('cron').CronJob;
const exampleJob = new CronJob(`*/2 * * * * *`,()=>{
console.log("You will see this message every 2 seconds",new Date().getSeconds());
});
exampleJob.start();
for addition if you not familiar with cron schedule expressions(cron syntax) that web will help you get right expression
https://crontab.guru/
From https://npmjs.org/package/node-schedule:
Execute a cron job every 5 Minutes = */5 * * * *
So (according to the NodeJS docs) you can use the child_process npm module to run the script.
Like this:
const { spawn } = require('child_process');
const schedule = require('node-schedule');
schedule.scheduleJob('*/5 * * * *', function() {
spawn('node', ['test.js']);
});
I want a Nodejs application running on Heroku to run a task every 30 seconds. I use Heroku scheduler for another task but that's because it runs once a day and Scheduler seems fine for that, but to run at this interval it is not suitable.
So I have installed node-cron and setup a job that would run every 30 seconds, daily, between 11am and 6pm (I am basically calling an api and then updating a database), locally I can run this fine with node cron.js
cron.js
const cron = require('node-cron');
const task = require('./scripts/task');
cron.schedule('*/30 * 11-18 * * *', () => {
console.log('Running Function');
task.runFunction();
});
I'm not 100% sure how to run this on Heroku, from their docs they specify this kind of setup (configured in Procfile):
web: node web.js
worker: node worker.js
clock: node clock.js
Current Procfile
web: node server.js
Is it a case of changing my cron.js to clock.js ? and then having something in a worker.js file? (though unsure as to what).
You should probably change the clock.js to cron.js, or, as you say, rename cron.js to clock.js.
web: node web.js
worker: node worker.js
clock: node cron.js
Also, I'm not sure your cron.js is set up correctly. From the documentation on node-cron, it looks like you might need something that looks like this:
const CronJob = require('../lib/cron.js').CronJob;
console.log('Before job instantiation');`
const job = new CronJob('*/30 * 11-18 * * *', () => {
console.log('Running Function');
task.runFunction();
});
console.log('After job instantiation');
job.start();
You don't need this line worker: node worker.js if in your cron.js you don't tell him to do somethings with this worker. (I don't know what runFunctions() do). And you don't even need to add the first line if your application is just cron jobs.
So you can just have this Profile :
clock: cron.js
You can change clockto cron if you want. It doesn't mean: docs.
I have Cronjob use node_module: node-cron
var hello = function( ) {
console.log('Hello world')
}
var job=new CronJob({cronTime:'00 52 13 * * *',
// that's important (100% made up example):
onTick: function() {
hello({
done: true,
lastRun: new Date()
});
},
start:false,timeZone:"America/Los_Angeles"});
job.start();
when I restart server .Cronjob not running.
node scripts that uses node-cron still have to be started for the cronjob to run. Adding things to node-cron, does not add it to cron on your machine, even though the naming can be confusing. node-cron simply allows your script to schedule jobs inside your script, while your script is running.
For this to work, either convert to scheduling the script on your machine, using the built in scheduler, or start your node script on machine startup. (This is different depending on your operating system).
I'm trying to host my Twitter bot on Heroku that tweets once every hour. But when I try to deploy, it gives me this error:
`Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch`
I did some Googling and found out this usually happens when you try to specify an explicit port instead of using process.env.$PORT, but I'm not specifying any ports in my code, which is basically just this:
var CronJob = require('cron').CronJob;
new CronJob('0 0 * * * *', function() {
//tweet once, once an hour
runTheBot();
}, null, true, 'America/New_York');
where runTheBot() makes a few API requests using Twit.
Interestingly, if I set the cron job to run more often than 60 seconds, say once every 10 seconds ('*/10 * * * * *'), it works fine. It even works if I have it run once a minute ('0 * * * * *').
Here's my Procfile:
web: node index.js -p $PORT
Is Heroku not compatible with this cron library? Should I be using Heroku Scheduler instead?
I solved this by changing the process type in the Profile from web to worker. I also had to manually scale up the dynos of the workers from 0 to 1 since Heroku set it to 0 by default for some reason.