PM2 shows old version when re-building app
Asked Answered
D

4

10

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?

Disunity answered 24/5, 2018 at 23:9 Comment(3)
having exactly the same problem! Any luck with it?Eureka
Yeah actually hadn't solved it back when I posted this. But, I don't need it for the time being. So hope you can find the answer.Disunity
found! The process itself was ok, was exactly the latest version. The problem I had was that the system node version was 8.something, while the latest version of my process needed 10.10.0. My CLI used the 10, while the server the 8. Setting the system node as 10 solved my issue.Eureka
S
5

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

Sherbrooke answered 24/5, 2018 at 23:33 Comment(4)
this is not working, when I reload it and rebuild it, it defaults to a version of about a month agoDisunity
have you tried running with sudo? sudo pm2 reload APP_NAMESherbrooke
This isn't working for me either. Error logs still displaying file names long since deleted from project.Carlow
Note: If it's typescript code, you have to "build" it before pm2 start. Depending on how your package.json is setup, pm2 most probably serves the already built codeConsanguinity
K
1

I had the same problem, i restarted the app and had to clear the cloudfront cache by creating invalidation.

Kershaw answered 3/2, 2020 at 6:50 Comment(1)
While this code may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestionPlayful
C
1

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:

  1. Make sure pm2 is running your program on the most recent node version.
  2. Check if the server (back-end) is running on the newest updated version of your repo.
  3. 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.
  4. 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?

Calculus answered 15/10, 2022 at 2:6 Comment(0)
F
1

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
Fivepenny answered 26/10, 2022 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.