Heroku Crash on success - node.js

I have a node app that runs once a day. It runs successfully with scheduler and is set up as a worker. The issue is Heroku keeps restarting the worker on successful exit which is the build process. Is there a way to turn off these crash messages and restarts for successful exits? They quickly fill my log files.

I fixed this issue by not having a worker at all. I just run scheduler with an npm run build && npm run prod script. Also keeps dyno costs really low.

Related

How to keep alive node server permanently?

I have tried so many ways to keeping node server alive on Linux environment but nothing has worked. Sometime the server runs only 4-5 hours and sometime it runs 10-12 hours and after that server goes shut down automatically.
I have tried forever start, pm2, nodemon but nothing has worked.
I have also tried shell script with forever start for running it but that also not worked.
Applications that are running under PM2 will be restarted automatically if the application crashes or is killed, but an additional step needs to be taken to get the application to launch on system startup (boot or reboot). Luckily, PM2 provides an easy way to do this, the startup subcommand.
The startup subcommand generates and configures a startup script to launch PM2 and its managed processes on server boots:
$ pm2 startup systemd
Run the command that was generated (similar to the highlighted output above, but with your username instead of sammy) to set PM2 up to start on boot (use the command from your own output):
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u sammy --hp /home/sammy
check here for details https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04
You can increase the size of memory restar- check this: pm2 process crashed on server. it gives an error
Try using process manager for making the application run all the time. Here is the link for Pm2 . It will restart you application once it crashes also automatically
Use a NPM package called nodemon
npm install -g nodemon
nodemon index.js
If the application fails or crashes for any reason it will restart
Read more at
https://www.npmjs.com/package/nodemon

PM2 & continuous delivery: How to tell if PM2 process didn't launch?

CI/CD services will often use SUCCEDED or FAILED to display if a deployment succeded and if an error occurred. However, with PM2 this is not so easy.
If you do:
pm2 start ecosystem.config.js in one of your deployment steps, PM2 will start the process but won't report an error if one of the processes fails. To check it, you would need to execute pm2 status, and then judging by the number of restarts and by executing pm2 logs --err you would be able to see what happens. I would like PM2 to return a non-zero code or be verbose about a process failing on first launch, rather than what it does now (failing silently).
Is there a native PM2 command that makes it integrate with CI/CD release status reporting easily?
What I want to achieve:
pm2 start ecosystem.config.js
# one of the processes in ecosystem.config.js fails, because eg. the database engine is not running and the driver can't connect to the db
# pm2 prints to stderr and a failure shows up on my CI/CD service. pm2 closes and returns non-zero if it failed to launch one of the processes
What's happening currently:
pm2 start ecosystem.config.js
# one of the processes fails
# no error is printed, everything is ok according to pm2. pm2 keeps running and the problematic process fails silently

Heroku: Disable default behaviour of - Npm run start

Every time I deploy code to Heroku, it automatically looks to run the start script in package.json (NodeJS)
My app is set to run on a timer / scheduler and as such does not need to be run by Heroku when deployed.
How do I disable this behaviour?
you should create a One-Off Dynos whith a Heroku Scheduler add-on.

Amazon EC2 NodeJS server stops itself after 2 days even after using sudo nohup

I have my app running on http://talkwithstranger.com/ and I have deployed it on AWS EC2. I use this command
sudo nohup node index.js &
To continue running my Node JS server even if I close my terminal and exit my SSH.
However, after 2 days everytime I wake up and I find out that the node server itself stops automatically. I checked the running processes by using
ps -ef
and my node script is not there.
Google Chrome say site DNS not found, because NodeJS Express is not running of course to serve my html file, but why it stops itself?
What is causing this unexpected shutdown of my server after every 2 days? I have to manually run nohup again to run it again.
Does nohup has a time to expire or something ?
You should run node.js using a service / process manager. You can use something basic such as forever or supervisord but I would actually advise you to take a look at PM2.
It can do a lot of things - one of them being that it manages your process, makes sure it keeps running, restarts it when it fails, manages the logs, etc. You can also have it autostart when you restart the server.
It becomes really powerful in combination with https://pm2.io, because this enables you to monitor your server's metrics such as CPU and memory remotely and see whether exceptions happened, and much more (such as even remotely updating the software by pulling from git). However, they no longer offer a free plan unfortunately - their plans now start at $79/month, which is a pity. But don't worry, the PM2 application itself is still free and open source, only the monitoring costs money.
Basic usage of PM2:
npm install -g pm2
...to install PM2.
pm2 start my_script.js
Starts a script and lets it run in background.
pm2 status
Shows the status of any running scripts.
pm2 restart all
Restarts all running scripts.
pm2 kill
Stops all scripts and completely shuts down the PM2 daemon.
pm2 monit
Monitors CPU/RAM usage and shows it.
pm2 logs
Shows the last 20 output and error log lines and starts streaming live logs to the console. The logs are stored in the folder ~/.pm2/logs.
Using PM2, your script will not stop - at most, it will restart. And if it does you will be able to more easily understand why because you can easily access logs and watch what happenes with memory usage, etc.
Extra tips:
To avoid filling up the harddisk with logfiles, I recommend installing the module pm2-logrotate:
pm2 install pm2-logrotate
To automatically launch PM2 with the same script on startup when the server starts, you can first save the current configuration:
pm2 save
...and then use the following command to install a startup script - follow the instructions displayed, which will be different based on the exact OS you are using:
pm2 startup
To use PM2 in a more advanced way with multiple processes, custom environment variables, etc., take a look at ecosystem files.
You can try forever.Install using the following command.
npm install -g forever
Then just start forever:
forever start index.js
Another better option for production use is pm2.You can install pm2 with below command
npm install -g pm2
# or
yarn global add pm2
start server
pm2 start index.js
The best thing is you can achieve load balancing with pm2(utilize all available CPU)
pm2 start index.js -i max
For more info, you can visit pm2 documentation page.

Forever process for Node.js server is not running all time

I am running a forever process for Node.js server but after one day the server stops the process.My server is running on Ubuntu platform. I have done the following process:
First I installed npm install forever and ran the command forever start server.js. I need the server to run for all the time but after one day I am seeing the server stops working.
Please help me to resolve this issue.
I would like to suggest that you try PM2 instead. Here's the short tutorial I wrote about it: http://www.nikola-breznjak.com/blog/nodejs/using-pm2-to-run-your-node-js-apps-like-a-pro/.
edit:
As per StackOverflow's policy I'm including the content from the post here also:
Running your Node.js application by hand is, well, not the way we roll. Imagine restarting the app every time something happens, or god forbid application crashes in the middle of the night and you find about it only in the morning – ah the horror. PM2 solves this by:
allowing you to keep applications alive forever
reloading applications without downtime
facilitating common system admin tasks
To install PM2, run the following command:
sudo npm install pm2 -g
To start your process with PM2, run the following command (once in the root of your application):
pm2 start server.js
As you can see from the output shown on the image below, PM2 automatically assigns an App name (based on the filename, without the .js extension) and a PM2 id. PM2 also maintains other information, such as the PID of the process, its current status, and memory usage.
As I mentioned before, the application running under PM2 will be restarted automatically if the application crashes or is killed, but an additional step needs to be taken to get the application to launch on system startup (boot or reboot). The command to do that is the following:
pm2 startup ubuntu
The output of this command will instruct you to execute an additional command which will enable the actual startup on boot or reboot. In my case the note for the additional command was:
sudo env PATH=$PATH:/usr/local/bin pm2 startup ubuntu -u nikola

Resources