Search code examples
javascriptmeteorcpu-usagepm2

Steps to setup pm2 to monitor a meteor app


I am trying to use pm2 to monitor my meteor app, intending to resolve the issue of cpu 100% usage.

I refereed to PM2 + Meteor Environment Setup, but the result looked like a failure.

My idea is that the meteor process was not initiated at all. Any ideas are welcome.

enter image description here

By the way, I've tried pm2-meteor, but probably because it is not maintained, it now cannot run successfully.

enter image description here

My code structure is listed as such,

+- cloud
  +- package.json
  +- client
  +- server
  +- pm2.json
  +- private
  +- public
  +- server

In the normal mode, I run the web app by get to cloud folder and the 'meteor'instruction.


Solution

  • I found that the cause of my issue is in Nodejs always installing 4.2.6

    So after update the nodejs on my machine, I need to make a decision to choose a way to run pm2

    1. pm2-meteor in https://github.com/andruschka/pm2-meteor
    2. run mongodb and it listens to 172.0.0.1:27010 
       + meteor build the app to be .tar.gz
       + pm2 run meteor app
    

    I use 2. so the steps are,

    1. install mongodb, pm2, nodejs(nodejs installation with nvm please)
    
    2. In order to create a mongodb service
       - launch mongod by 
         mongod -dbpath=$(some_accessible_path) -logpath=$(some_accessible_path) --fork --replSet meteor
    
    3. run mongo and get to mongo shell and key in commands bellow
       a. var config = {_id:"meteor",members:[{_id:0,host:"127.0.0.1:27017"}]}
       b. rs.initiate(config)
       if the return value is {"ok":1}, mongodb service is ready.
    
    4. pack meteor app
       a. mkdir ~/cloud_build
       b. get to the source code folder and use the command
          meteor build --architecture=os.linux.x86_64 ~/cloud_build
       c. cd ~/cloud_build
       d. tar xvf some.tar.gz
       e. you will get a bundle folder
       f. cd that_bundle_folder/program/server && npm install
    
    5. run pm2
       a. create a file of pm2.json in the bundle folder 
       b. pm2 start pm2.json
    

    the pm2.json looks like something bellow

    {
      "apps": [{
      "name": "appName",
      "cwd": "/yourhome/cloud_build/bundle",
      "script": "main.js",
      "env": {
      "NODE_ENV": "production",
      "WORKER_ID": "0",
      "PORT": "3000",
      "ROOT_URL": "http://yourweburl",
      "MONGO_URL": "mongodb://localhost:27017/meteor",
      "MONGO_OPLOG_URL": "mongodb://localhost:27017/local",
      "HTTP_FORWARDED_COUNT": "1",
      "METEOR_SETTINGS": {}
      }
      }]
    }
    

    Then the webapp can be accessed in http://yourweburl:3000