{"id":544,"date":"2017-04-16T01:35:30","date_gmt":"2017-04-15T23:35:30","guid":{"rendered":"https:\/\/blog.unetresgrossebite.com\/?p=544"},"modified":"2017-05-07T02:48:58","modified_gmt":"2017-05-07T00:48:58","slug":"pm2","status":"publish","type":"post","link":"https:\/\/blog.unetresgrossebite.com\/?p=544","title":{"rendered":"PM2"},"content":{"rendered":"<div id=\"attachment_545\" style=\"width: 410px\" class=\"wp-caption alignright\"><a href=\"https:\/\/blog.unetresgrossebite.com\/wp-content\/uploads\/2017\/04\/pm2-systemctl.png\"><img aria-describedby=\"caption-attachment-545\" decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blog.unetresgrossebite.com\/wp-content\/uploads\/2017\/04\/pm2-systemctl-300x124.png\" alt=\"systemctl integration\" width=\"400\" height=\"165\" class=\"size-medium wp-image-545\" srcset=\"https:\/\/blog.unetresgrossebite.com\/wp-content\/uploads\/2017\/04\/pm2-systemctl-300x124.png 300w, https:\/\/blog.unetresgrossebite.com\/wp-content\/uploads\/2017\/04\/pm2-systemctl.png 553w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><p id=\"caption-attachment-545\" class=\"wp-caption-text\">systemctl integration<\/p><\/div>\n<p>PM2 is a NodeJS processes manager. Started on 2013 on GitHub, PM2 is currently the <a href=\"https:\/\/stats.js.org\/\" target=\"_blank\">82nd most popular JavaScript project on GitHub<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>PM2 is portable, and would work on Mac or Windows, as well as Unices. Integrated with Systemd, UpStart, Supervisord, &#8230; Once installed, PM2 would work as any other service.<\/p>\n<p>&nbsp;<\/p>\n<p>The first advantage of introducing PM2, whenever dealing with NodeJS services, is that you would register your code as a child service of your main PM2 process. Eventually, you would be able to reboot your system, and recover your NodeJS services without adding any scripts of your own.<\/p>\n<div id=\"attachment_546\" style=\"width: 410px\" class=\"wp-caption alignright\"><a href=\"https:\/\/blog.unetresgrossebite.com\/wp-content\/uploads\/2017\/04\/pm2-list.png\"><img aria-describedby=\"caption-attachment-546\" decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blog.unetresgrossebite.com\/wp-content\/uploads\/2017\/04\/pm2-list-300x112.png\" alt=\"pm2 processes list\" width=\"400\" height=\"149\" class=\"size-medium wp-image-546\" srcset=\"https:\/\/blog.unetresgrossebite.com\/wp-content\/uploads\/2017\/04\/pm2-list-300x112.png 300w, https:\/\/blog.unetresgrossebite.com\/wp-content\/uploads\/2017\/04\/pm2-list.png 584w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><p id=\"caption-attachment-546\" class=\"wp-caption-text\">pm2 processes list<\/p><\/div>\n<p>Another considerable advantage of PM2 is that it introduces <a href=\"https:\/\/keymetrics.io\/2015\/03\/26\/pm2-clustering-made-easy\/\" target=\"_blank\">clustering capabilities<\/a>, most likely without requiring any change to your code. Depending on how your application is build, you would probably want to have at least a couple processes for each service that serves users requests, while you could see I&#8217;m only running one copy of my <em>background<\/em> or <em>schedule<\/em> processes:<\/p>\n<p>&nbsp;<\/p>\n<div id=\"attachment_547\" style=\"width: 510px\" class=\"wp-caption alignright\"><a href=\"https:\/\/blog.unetresgrossebite.com\/wp-content\/uploads\/2017\/04\/pm2-listen.png\"><img aria-describedby=\"caption-attachment-547\" decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blog.unetresgrossebite.com\/wp-content\/uploads\/2017\/04\/pm2-listen-300x53.png\" alt=\"nodejs sockets all belong to a single process\" width=\"500\" height=\"88\" class=\"size-medium wp-image-547\" srcset=\"https:\/\/blog.unetresgrossebite.com\/wp-content\/uploads\/2017\/04\/pm2-listen-300x53.png 300w, https:\/\/blog.unetresgrossebite.com\/wp-content\/uploads\/2017\/04\/pm2-listen.png 540w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><p id=\"caption-attachment-547\" class=\"wp-caption-text\">nodejs sockets all belong to a single process<\/p><\/div>\n<p>Having enabled clustering while starting my <em>foreground<\/em>, <em>inferno<\/em> or <em>filemgr<\/em> processes, PM2 would start two instances of my code. Now one could suspect that, when configuring express to listen on a static port, starting two processes of that code would fail with some <em>EADDRINUSE<\/em> code. And that&#8217;s most likely what would happen, when instantiating your code twice. Yet when starting such process with PM2, the latter would take over socket operations:<\/p>\n<p>And since PM2 is processing network requests, during runtime: it is able to balance traffic to your workers. While when deploying new revisions of your code, you may gracefully reload your processes, one by one, without disrupting service at any point.<\/p>\n<div id=\"attachment_548\" style=\"width: 310px\" class=\"wp-caption alignleft\"><a href=\"https:\/\/blog.unetresgrossebite.com\/wp-content\/uploads\/2017\/04\/pm2-show.png\"><img aria-describedby=\"caption-attachment-548\" decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blog.unetresgrossebite.com\/wp-content\/uploads\/2017\/04\/pm2-show-300x289.png\" alt=\"pm2 process description\" width=\"300\" height=\"289\" class=\"size-medium wp-image-548\" srcset=\"https:\/\/blog.unetresgrossebite.com\/wp-content\/uploads\/2017\/04\/pm2-show-300x289.png 300w, https:\/\/blog.unetresgrossebite.com\/wp-content\/uploads\/2017\/04\/pm2-show.png 362w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-548\" class=\"wp-caption-text\">pm2 process description<\/p><\/div>\n<p>PM2 also allows to track per-process resources usage:<\/p>\n<p>Last but not least, PM2 provides with a centralized dashboard. Full disclosure: I only enabled this feature on one worker, once. And wasn&#8217;t much convinced, as I&#8217;m running my own supervision services, &#8230; Then again, you could be interested, if running a small fleet, or if ready to invest on <a href=\"https:\/\/keymetrics.io\/pricing\/\" target=\"_blank\">Keymetrics premium features<\/a>. Then again, it&#8217;s worth mentioning that your PM2 processes may be configured forwarding metrics to Keymetrics services, so you may keep an eye on memory or CPU usages, application versions, or even configure reporting, notifications, &#8230; In all fairness, I remember their dashboard looked pretty nice.<\/p>\n<div id=\"attachment_549\" style=\"width: 410px\" class=\"wp-caption alignright\"><a href=\"https:\/\/blog.unetresgrossebite.com\/wp-content\/uploads\/2017\/04\/pm2-keymetrics2.png\"><img aria-describedby=\"caption-attachment-549\" decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blog.unetresgrossebite.com\/wp-content\/uploads\/2017\/04\/pm2-keymetrics2-300x178.png\" alt=\"pm2 keymetrics dashboard, as advertised on their site\" width=\"400\" height=\"237\" class=\"size-medium wp-image-549\" srcset=\"https:\/\/blog.unetresgrossebite.com\/wp-content\/uploads\/2017\/04\/pm2-keymetrics2-300x178.png 300w, https:\/\/blog.unetresgrossebite.com\/wp-content\/uploads\/2017\/04\/pm2-keymetrics2-768x456.png 768w, https:\/\/blog.unetresgrossebite.com\/wp-content\/uploads\/2017\/04\/pm2-keymetrics2-1024x608.png 1024w, https:\/\/blog.unetresgrossebite.com\/wp-content\/uploads\/2017\/04\/pm2-keymetrics2.png 1800w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><p id=\"caption-attachment-549\" class=\"wp-caption-text\">pm2 keymetrics dashboard, as advertised on their site<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>PM2 is a NodeJS processes manager. Started on 2013 on GitHub, PM2 is currently the 82nd most popular JavaScript project on GitHub. &nbsp; PM2 is portable, and would work on Mac or Windows, as well as Unices. Integrated with Systemd, UpStart, Supervisord, &#8230; Once installed, PM2 would work as any other service. &nbsp; The first [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,10,6,2],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.unetresgrossebite.com\/index.php?rest_route=\/wp\/v2\/posts\/544"}],"collection":[{"href":"https:\/\/blog.unetresgrossebite.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.unetresgrossebite.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.unetresgrossebite.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.unetresgrossebite.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=544"}],"version-history":[{"count":22,"href":"https:\/\/blog.unetresgrossebite.com\/index.php?rest_route=\/wp\/v2\/posts\/544\/revisions"}],"predecessor-version":[{"id":571,"href":"https:\/\/blog.unetresgrossebite.com\/index.php?rest_route=\/wp\/v2\/posts\/544\/revisions\/571"}],"wp:attachment":[{"href":"https:\/\/blog.unetresgrossebite.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.unetresgrossebite.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.unetresgrossebite.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}