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 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
- Update nuget packages
- Build ASP.NET Core service
- Start ASP.NET Core service
- Install bower packages for web with gulp
- Publish web package with gulp
- Start express server for web
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 ..
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
In result all required steps are being executed: ASP.NET Core 2 service starts, all artifacts are being published and website is started.
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.