ASP.NET Core with PM2

asp.net core with pm2

Overview

In latest posts I have migrated my microservice on ASP.NET Core to RC2. I’m using gulp to manage my front-end build pipeline. As it’s slightly annoying to start everything, let’s see how can I launch multiple services on ASP.NET Core with PM2.

PM2

PM2 is process manager that allows you to start multiple processes, monitor them, restart automatically and a lot more. Installation process is straightforward as usual with all node packages.

Key functionalities are:

Start an app using all CPUs available + set a name :
$ pm2 start app.js -i 0 --name "api"

Restart the previous app launched, by name :
$ pm2 restart api

Stop the app :
$ pm2 stop api

Restart the app that is stopped :
$ pm2 restart api

Remove the app from the process list :
$ pm2 delete api

Kill daemon pm2 :
$ pm2 kill

ASP.NET Core with PM2

Steps to be done

  1. Update nuget packages
  2. Build ASP.NET Core service
  3. Start ASP.NET Core service
  4. Install bower packages for web with gulp
  5. Publish web package with gulp
  6. Start express server for web

Script

There are options to use PM2 within gulp, but to follow KISS principle as usual I’ll use simple bash script. You can find source code on GitHub.

clear
pm2 kill

echo "Starting up build pipeline"

cd userService
#hack to allow to do this multiple times
rm -rf obj
dotnet restore
dotnet build
pm2 start --name userService dotnet -- run 

echo "User Service started"

cd ..

cd web
bower install
gulp publish

pm2 start index.js

cd ..

This line:

rm -rf obj

will resolve following issue:

userService-0 (err): Could not read the incremental cache file: /home/mogluszka/Sources/shout/src/userService/obj/Debug/netcoreapp1.0/.IncrementalCache

Results

In result all required steps are being executed: ASP.NET Core 2 service starts, all artifacts are being published and website is started.

asp.net core with pm2

Now I have few useful commands:

$ pm2 show userService
mogluszka@mogluszkaW540:~/Sources/shout/src$ pm2 logs userService
[PM2] Tailing last 20 lines for [userService] process 

userService-0 (out): Project userService (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
userService-0 (out): Hosting environment: Production
userService-0 (out): Content root path: /home/mogluszka/Sources/shout/src/userService
userService-0 (out): Now listening on: http://localhost:5000

Second one is actually key to diagnose any issues with your processes.

 

2 thoughts on “ASP.NET Core with PM2

  • Pingback: dotnetomaniak.pl

  • August 28, 2016 at 3:55 pm
    Permalink

    The process of dotNet starts, but does not monitore the usage of CPU and Memory.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *