Weird behavior when deploying a Node.js app on Heroku - node.js

I have a Node.js app on Heroku which uses their standard dynos. When I deploy new code my site remains up, but for a short period of time some code runs twice. I'm assuming it's because what is happening with the dynos to keep the site from having any interruptions. I'm not sure how to prevent this from happening.

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.

Angular 8 Universal (Server Side Rendering) app is crashing

I have deployed an Angular 8 universal app in a server droplet of Digital Ocean. It is running in production. The server config is 4GB memory and 2 vCPU. The app is run via nodejs in the server. It's been running fine for over a month until recently it suddenly started crashing in no definite time. Since it's running in my client's computer browser, I don't know any way to monitor the error in their console. I also run this app with NOHUP command which dumps all the server side node js logs in a nohup.out file. But nohup.out file shows nothing about the crash.
Things that happened around the crash event started to happen.
1. I have pushed an update which is totally unrelated to the bootstrapping of the app. As per my understanding the Angular universal only renders the initial view and sends it to client browser. After that it's the browser that takes care of rest of the jobs.
The app uses websocket to maintain a bidirectional connectivity with the backend app written in Java. Also I don't think WS is the reason, because after implementing the WS feature, it ran just fine for over a week.
Thirdly i noticed that there are some image links(of the website) that are loaded from a CDN/Edge server and for last couple of days some static files located in the CDN server are taking too long to load/download. The response time to load those files spike up to 25-26 seconds sometimes. Since these static files are loaded during startup, can the delay of loading the static files cause any type of timeout event when Node JS renders the page in the startup?
As you can understand I have no clue how to debug this thing. Is there anyway who faced similar crashing issue with Angular Universal and help me point to the right direction? Thanks

How to reduce TTFB for webpage using heroku free service

I recently uploaded a website to heroku (using their free service). The website is built using node.js and express. According to google I had a page speed score of 99 and 100 for mobile and desktop respectively. The following day I made some basic changes to the html and css, and after pushing to heroku again the TTFB is now extremely slow. The app was out of hibernation mode when I did the speed tests. Is this issue with Heroku's servers or should I look for another way to solve it? I don't know how changing the html and css would cause TTFB issues. Is this a common issue with Heroku?
Update: After pushing changes to heroku a second time I had no problems. Seems to have been a glitch with heroku's server.
TTFB is the time that the server takes to show the first byte of content. This audit fails when the browser waits more than 600ms for the server to respond to the main document request.
So, I don't know much about Heroku but if it is a free service, the response time must be much slower than the payment plans.

Heroku Hobby server still sleeps or issue with NodeJS?

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.

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