Pm2 throwing service unavailable error after deployment - node.js

I have been using pm2 to manage my node apps for a while. In deployment steps, pm2 restart is added after deployment finished. But it throws unhandledrejection event in promise js and service becomes unavailable.
when i manually restart pm2 manually issue vanishes. we are using same deployment steps on qa and production. But this issue of taking manual restart of pm2 is only happening at qa environment.
we have tried restart pm2 script in cron job as well but that also doesn't solved our issue.
I suspect may be due to diff in npm, node versions at prod and qa env, the behaviour is different.

Related

Error running nextjs with PM2 on Azure App Service on Linux

The pipelines and releases seem to work finely as they get the artifact deployed on the app service or so it seems as the output error happens on start of the PM2 process.
I haven't found anything on this error on google, and don't know if there is a way to update PM2 on the app service machine as PM2 itself suggests.
These are the logs.
Error message
summary image
I was expecting the app to work and no longer show an application error as I fixed both build and release pipelines.
This is a picture of the important bit of info of the error as its really long
Check the below steps to run nextjs with PM2.
First create a nextJS app.
Run npm run dev command to run the app in development.
Deploy the app to Azure Linux App Service.
Open the SSH of the Azure App Service to run the PM2 commands.
We can open directly with the below URL.
Path to SSH - https://YourDeployedAppName.scm.azurewebsites.net/
OR
Navigate to the deployed Azure Linux App => Advanced Tools => Go => SSH.
Run the below command to install PM2.
npm install pm2 -g
Thanks #Azure OSS Developer Support for the commands.
In Configuration section => General Settings add the Startup command.
pm2 start site/root/index.js --name mynpmnextapp
Your path may differ for the index.js file.
My app entry point is index.js.For some apps it can be server.js.Change it according to your app.
cannot find module `../build/output/log'
Make sure you are not running the PM2 with the output folder. As mentioned above it has to be the entry point either server.js or index.js based on your code files.
don't know if there is a way to update PM2 on the app service
In the KUDU Console use npm install pm2#latest -g to update the PM2.
References taken from MSDoc and npmjs.

Safely Undeploying / Redeploying on MERN Stack using PM2 and yarn?

I am running a web service on a reasonably standard MERN stack on a Ubuntu server. I am not overly familiar with web deployment or web programming but I have found myself with this project.
The web service has a pretty extensive list of instructions on how to deploy the service, but it has no instructions on how to undeploy / redeploy it. In addition, it was the sellers of the service's code that deployed it on our server.
I have made some minor changes to the code (fixing spelling, removing / adding functionality) and I cannot seem to work out how to safely undeploy or redeploy the service.
The list of instructions for deployment is, essentially:
yarn
yarn build
yarn start:prod
pm2 start dist/main.js
Doing this as-is while the old service is running obviously doesn't work as the port that I am trying to relaunch on is being watched. Stopping the pm2 process allowed me to redeploy using the above method but has now caused a 502 Gateway error when I relaunch the process.
What is the safe method to undeploy and / or redeploy a service using this tech stack?
You first may want to have a look around on your ubuntu server.
PM2 is like a service manager. To get a list of running services, run this command.
pm2 ls
A typical output:
Typically when you install an update, you just need to restart them.
pm2 restart <service-name>
e.g. pm2 restart cache in my example

How to fix an issue that code changes are not reflecting in PM2?

In my PM2 instance running on Windows server having 4 cores CPU and that many clusters/instances, file changes are caught and servers get restarted but the changes do not get reflected. Instead it continues to show the old updates/features.
But when I tried doing the following it starts reflecting the new updates.
$ pm2 stop all
$ pm2 kill
$ pm2 start... (one by one)
This does not happen always but happens sometime only. How to fix this?
I've faced this problem in AWS deployment. Then I come up with a solution after long research and this worked for me as a charm.
After git pull just stop your server and then start it. After that
pm2 start index.js
pm2 reload index
Now your changes are reflected.

Swagger managed nodejs server crashing when using PM2

I created my node js server using swagger via:
swagger project create <project-name>
But when I'm trying to launch the server using pm2 as follows:
pm2 start app.js
it's crashing because of too many unstable restarts. Below is the error message:
Script /path/to/app.js had too many unstable restarts (16). Stopped.
"errored"
Its a known issue and it can be fixed if we change the FD0 pipe in ForkMode from "ipc" to "ignore".
For more details please refer to this issue thread on github: PM2 crashes due to many unstable restarts

NodeJS - Keep my App running with Forever

Currently, I have a NodeJS service running on my server. It provides a RestAPI thanks to HAPI.JS
This service run permanently with forever executed in upstart script but I got some trouble.
Sometimes, the service have an error like this :
Debug: internal, implementation, error
TypeError: Uncaught error: Cannot call method 'replace' of undefined....
At this moment, server is completly down and never restart :(
I need a 100% stable service that why I have to restart it when an error appears.
My Question :
How can I restart NodeJS Service with forever when errors occured ?
Run you app with pm2 instead of forever. Pm2 will restart the node server even after uncaught exceptions.
If the process fails on startup then it won't restart automatically. You can configure the minimum uptime --minUptime flag.
You can then use the --watch flag to watch for changes of the fix.
However, the specific error you're receiving wouldn't normally stop the Hapi process which would suggest this isn't a forever issue.

Resources