My node-app is running in PM2. When I pull the latest version of my app off github and rebuild it, the site during the building process defaults to a much older version (probably the first version when I initially launched the daemon). How can I make it show the latest version before the fetch, while I rebuild to the really latest version?
To serve the latest code in the directory, run:
$ pm2 reload APP_NAME
You can find out APP_NAME by
$ pm2 list
When you fetch the latest code and want to run it, restart the server with
$ pm2 reload APP_NAME
To Start a server for the first time with a specific name
$ pm2 start path/to/index.js --name "api"
Note: On linux you might have to run pm2 with sudo
sudo pm2 reload APP_NAME
–
Sherbrooke I had the same problem, i restarted the app and had to clear the cloudfront cache by creating invalidation.
I am afraid this might be a little late but I found a fix for this if any of the above answers did not work for you. For context, I have a MERN web app with a client side folder (react-app).
Structure of my file is similar to this:
app
|
|-- client (react-app)
|-- build
|-- node_modules
|-- public
|-- src
|-- package-lock.json
|-- package.json
|-- controllers
|-- models
|-- routers
|-- node_modules
|-- .env
|-- package-lock.json
|-- package.json
|-- server.js
As you can see when I run pm2 I will simply run
pm2 start server.js
I had the same issue, my project shows the same front-end version that was much earlier than the one I am running.
Some steps I did to debug and detect the cause of my issue:
- Make sure pm2 is running your program on the most recent node version.
- Check if the server (back-end) is running on the newest updated version of your repo.
- If it is, then it is an issue isolated with your front-end. Try running
npm run build
in your client folder to generate the most updated front-end build. - If it is not, there may be an issue in how your git repo is recognized (unlikely, but worth a shot to try move your project to a fresh new repo).
My issue was that I didn't npm run build
the newest version of my front-end, hence it was still showing a version ages ago.
Note: npm run build
is a valid script for me because I downloaded the package react-scripts
in my client side. Just chuck in "build": "react-scripts build",
in your scripts in the client side package.json file.
Note 2: I am deploying this on a Droplet in Digital Ocean, and I have found a thread that may be relevant to your issue: How do I force PM2 to use the latest version of my app?
I had the same issue. To my mind the topic is that when you run those commands within the PM2 daemon PM2 restart all
or PM2 reload APP_NAME
that the old commit is taken.
When I used the command PM2 restart all
outside the daemon then PM2 restarted with the new commit.
My approach now is that I have a script that runs in another daemon with nohup and checks if the command PM2 restart all
should be sent (,but then it comes from outside the PM2 daemon).
Maybe the script below helps somebody, I spent some time with it with testing:
The script checks every 3rd second a file restart.txt
in the same folder like restart.sh
if there is the word true
in the first-line. If so, it sets the first-line to false
and calls PM restart all
. A log line is always placed to restart.log
in the same folder. The restart.sh
is executed in a nohup-daemon by pkill -9 -f restart.sh; nohup YOURPATH/restart.sh & pid=999999
, which is killing all running restart.sh-processes and starts a new one. In my application I set true
to restart.txt
where I originally called PM2 restart all
.
I find this solution a little bit of an overkill and definitely a workaround, if anybody has another, better idea, I look forward hearing it.
#restart.sh
#!/bin/bash
while true
do
FIRSTLINE=`head -n 1 ${0%/*}/restart.txt`
DATE=`date "+%Y%m%d_%H%M%S"`
if [ $FIRSTLINE = true ]
then
echo "${DATE} - restarting" > ${0%/*}/restart.log
echo false > ${0%/*}/restart.txt
pm2 restart all
else
echo "${DATE} - Not restarting" > ${0%/*}/restart.log
fi
sleep 3
done
© 2022 - 2024 — McMap. All rights reserved.