Heroku Hobby server still sleeps or issue with NodeJS? - node.js

I've noticed that my NodeJS application which resides on a Heroku Hobby server after no activity does a "soft restart". By that I mean it doesn't do big actions like reinitialize the ORM system or recreates the HTTP server, however it does seem to forget callback functions and global variables or any variables that were dynamically created and held in memory.
Does Heroku still "sleep" even with the Hobby plan or is it something related to NodeJS?

As indicated by Heroku's documentation, Hobby dynos do not sleep.
However, dynos of all types are restarted frequently:
Automatic dyno restarts
The dyno manager restarts all your app’s dynos whenever you:
create a new release by deploying new code
change your config vars
change your add-ons
run heroku restart
Dynos are also restarted (cycled) at least once per day to help maintain the health of applications running on Heroku. Any changes to the local filesystem will be deleted.
I'm not entirely clear what you mean by
it does seem to forget callback functions and global variables or any variables that were dynamically created and held in memory
but at least some of these things could happen due to automatic dyno restarts. Certainly anything that only exists in memory will be lost.
You could manually restart your dynos using heroku ps:restart and see if that replicates the behaviour you are seeing. You may need to adjust your code to survive being restarted.

Related

Node app running on cPanel hosting shuts down after 30 minutes idle

I have a REST api node app.
Once its running on localhost, it runs until I stop the dev debugging, no errors.
I moved it over to my cPanel hosting, installed a node app.
It starts up the same as localhost.
But after 30 minutes being idle, it shuts down.
The next request after this, restarts the app.
There are no crash or errors in the log, just the restarting messages.
I know this is default behaviour for free hosting, like Heroku but I'm paying for this hosting package.
Does anyone know...
Is this default behaviour for cPanel hosted node apps, or is my app causing this (using too much memory or cpu for example?
Is there any settings that can be edited to change this?
According to the docs, cPanel uses something called Phusion Passenger to run Node.js. In turn, Passenger docs show a default "idle time" of 5 minutes and a default of passenger_min_instances = 1. No idea if cPanel changes the defaults, or if the hosting provider did. I would recommend contacting the hosting provider about the issue in any case, and asking about these options specifically - they may be able to help or tune the service for you.
The startup time for a node app depends on what it's doing. A rest-api could be in the milliseconds, whereas a small Ai app loading a corpus or training a dataset (which mine was) could end up being 30 seconds plus. However the quantity of users did not warrant a dedicated server, so the work-around was to call the endpoint using a CRON, keeping the app alive.
Not perfect, but this type of thing may be useful if you are using aws lambda, which calls a 3rd party service, and which charges based on time taken. Every millisecond counts.

How to keep my heroku app working during new version deployment?

I've noticed that my app stop working briefly (all processes are killed) during the deployment of a new build. This cause the failure of many critical requests that are crucial for the life of the whole application.
Any suggestion to keep my apps working no matter what and especially in case of new version deployment ?
Don't use the free tier dynos for production apps. Free tier only allows one dyno, so you can't have one spinning up while the other is acting as your production server. Hence, they shut down during new deploys. Paid tier stays up until the new dyno is ready then a swap is done.
If you aren't using free tier dynos, this is rather interesting. Unless you did a "restart all dynos", that shouldn't be the case. You could try using multiple dynos then. Typically heroku does a sequential shutdown for multiple dynos, so one remains running.

Is it possible to have workers running "virtually" 24h/7 in Heroku?

I have real time data incoming and I need to process it 24h/7, but:
a) Heroku will restart Dynos once a day.
b) Heroku will restart Dynos when code is updated.
Point a, can be more or less handled by having multiple dynos, if one restarts, the other is still there.
But for point b, I don't see how I can handle it. If all dynos restart for an update, I'll lose data until they are up again.
Is there any solution?
Yes, you can enable preboot on your dynos.
Preboot changes the standard dyno start behavior for web dynos. Instead of stopping the existing set of web dynos before starting the new ones, preboot ensures that the new web dynos are started (and receive traffic) before the existing ones are terminated. This can contribute to zero downtime deployments.
heroku features:enable preboot -a <myapp>
Do read the entire docs - there are some important considerations / caveats to be mindful of, like the fact that deployments will now take several minutes to switch to the newer set of dynos.

node cron jobs on heroku, is using the heroku scheduler necessary?

I'm building my app backend as a node/express API to be deployed on Heroku.
I am new to implementing "cron jobs", and I found the npm library named node-cron which seems very straightforward.
Is it as simple as just setting up the cron job in my app runtime code? I know it will not run when the heroku free dyno goes into "sleep mode" (based on other StackOverflow answers), but I plan to use paid dynos in production so that's not an issue.
My main concern is when I "scale up" on heroku and run multiple dynos, will this cause weird interactions? Will every "instance" of my app on a separate dyno try to run its own crons independantly, causing duplication of work?
I know heroku provides a free "scheduler" addon for this to spin up dynos to do this, but if the above won't happen, the scheduler heroku addon seems to be unneeded overhead in my case.
Notes: my cron will be very simple, just doing some database clean up on old records, I didn't want to do it in the database layer to keep things simpler as it seems not super easy to schedule jobs in postgres.
Any insights will be much appreciated. Thank you.
As you mentioned above all things are correct as per my past experience with the same situation.
npm package node-cron only works fine if you have only one dyno
otherwise it will trigger based on a number of dynos.
If you want to execute the cron perfectly without taking any risk (doesn't matter how much dyno's you have) I suggest you to use heroku add-on.

How to warm up a Heroku Node.js server?

Heroku reboots servers everyday. After reboot, my node server takes around 20 seconds to load a page for the first time. Is there a way to prevent this?
EDIT: You guys seem to be misunderstanding the situation. In Heroku, even production servers must be restarted daily. This is not the same as a free server sleeping. This question is aimed more at preventing lazy-loading and pre-establishing connection pools to databases.
Old question, but in case others stumble upon it like I did
Use can use Heroku's Preboot feature:
Preboot changes the standard dyno start behavior for web dynos. Instead of stopping the existing set of web dynos before starting the new ones, preboot ensures that the new web dynos are started (and receive traffic) before the existing ones are terminated. This can contribute to zero downtime deployments
You could also combine it with a warmup script like the one described in this Heroku post

Resources