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.
Related
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.
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 question already has answers here:
Heroku Node.js Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
(6 answers)
Closed 3 years ago.
I'm trying to make a discord bot that runs 24/7 using Heroku. Everything is good, except for the fact that the bot crashes after 60 seconds.
The error output tells me this:
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
I looked for solutions on the internet, and I found quite a few. However, none of them worked.
Here's my main file's code:
const Discord = require('discord.js')
const {prefix, token} = require('./config.json')
const client = new Discord.Client()
// Login
client.login(token)
client.once('ready', () => {
console.log('Back online')
client.user.setActivity(' channel', {type: 'LISTENING'})
})
client.on('message', message => {
if (message.author.bot) {
return;
}
if (message.content.toLowerCase() === 'hello') {
message.channel.send('Hey!')
}
})
You've most likely assigned your bot to run on a web service in your Procfile. The Heroku Procfile is a file that stores information about what processes Heroku should run. If you set your Procfile to run a web service, Heroku expects you to bind to the required port (using process.env.PORT) once it launched. If it didn't then Heroku will assume that your program failed to start and will restart it.
As of now, your Procfile most likely looks like this:
web: node index.js
This tells Heroku to run your program in a web dyno. However, if you don't bind to an HTTP port with a Node.js service like Express, Heroku will crash your program. To fix this, change web to worker.
worker: node index.js
Note that by changing your Procfile to use worker, your free dyno hours (if you are running on a free dyno) will continue decreasing 24/7, and you'll be using up around 700 hours per month. If you registered your credit card, the limit is set to 1000 hours per month and you don't have to worry. Otherwise, you have to upgrade your dyno to a Hobby dyno to keep your bot running the entire month.
EDIT: Though this wasn't the accepted answer, I still have to clarify that sometimes Heroku doesn't read the Procfile settings. In that case, you should run these commands in the folder where your project is:
heroku ps:scale web=0
heroku ps:scale worker=1
This will force Heroku to use the worker dyno defined in your Procfile. Hope this helps.
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 created a nodejs file using express enviornment and running the file on server using nodemon. Currently I have to give commands to the interface to run the particular file on nodemon but what I currently need is to schedule the task to run that file on server automatically at multiple occasion in a single day.
my file excute like this on terminal::
nodemon example_api.js
output terminal:
root#*********:/var/www/example project# nodemon example_api.js
[nodemon] ##.##.#####
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node api.js`
Listening on port 8080
Note: I am currently running node js on Mobaxterm terminal currently using windows but my file will be run on a server with linux interface
1. If you want to run your node process continuously and want to run only particular task:
Use node-schedule or node-cron packages to run your code block at desired time or interval.
i.node-schedule
var schedule = require('node-schedule');
var j = schedule.scheduleJob('*/30 * * * * ', function(){
console.log('The answer to life, the universe, and everything!');
});
ii.node-cron
var cron = require('node-cron');
cron.schedule('*/30 * * * *', function(){
console.log('The answer to life, the universe, and everything!');
});
2. If you want to run only single node script:
You can use Linux crontab to execute your script at desired time
crontab -e
and add following entry
*/30 * * * * /usr/local/bin/node /home/ridham/example/script.js
This will execute /home/ridham/example/script.js every 30 minutes. and always give full qualified path here.
You have to give crontime in any of the following. you can learn about crontime here
For a simple implementation - you can just use setInterval like so:
setInterval(() => {
// do something every 5 seconds
}, 5000);
But it you want something like a cron, then, you might wanna go with node-cron or node-schedule.
You can also use providers like AWS CloudWatch. AWS CloudWatch allows you to run AWS Lambda functions on a cron.