简体   繁体   中英

How to reload/restart pm2 with flighplan and be aware of a symlink directory?

I am using flighplan to deploy my web service that built by Node.js

My deployment script uploads the new release to a new directory which has a timestamp or some random characters in its name. I am keeping all my releases in my server so I can rollback easily by just changing the link to any specific release and have zero-downtime deployment.

The main directory, named by the service name and it is just a symbolic link that get changed to the new release's directory after uploading it.

ln -snf ~/tmpDir ~/appName

在此处输入图片说明

My problem is when pm2 restars my server it uses the original path of the previous release, it doesn't bind with the symbolic link and follow the link to the new directory that the link is pointing to.

Is there any way to restart or reload pm2 and let it be aware of that symbolic link ?

Short answer - You should not run pm2 inside a symlink which changes. pm2 would always pick the old path the symlink is pointing to unless you use pm2 kill command.

Solution - create a new directory and make it parent directory of smylink and your code directories (mohmal-144 etc.). For the sake of understanding lets call this deploy.

Now you should have below structure /home/deploy /home/deploy/mohmal -> home/deploy/mohmal-144.

If you are using pm2, yous should use ecosystem.json (pm2 config for starting apps) file. Though you can name this file to anything you want. For the sake of understanding, lets call this file ecosystem.json file.

Inside this ecossystem.json file, in the apps section, add the cwd directory and cwd should point to the path of the symlink(not the path to which symlink it pointing to). See below example.

{
  "apps": [
    {
      "name": "mohmal",
      "script": "src/bin/server.js",
      "exec_mode": "cluster",
      "instances" : "0",
      "cwd": "/home/deploy/mohmal",
      "error_file": "/var/log/mohmal/error.log",
      "out_file" : "/var/log/mohmal/out.log",
      "merge_logs": true
    }
  ]
}

Place this file in the parent directory which is named deploy in this example.

Now run/use pm2 start, pm2 restart commands from this directory only. If you are already running pm2 in the system, just once run pm2 kill to clear the old processes.

And then use the suggested changes, you would never have to pm2 kill the processes. Also the changes to symlink would reflect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM