Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep a node application running on azure app service

I have deployed a node js web application on app service in azure. Issue is that my application occasionally getting killed for unknown reason. I have done exhaustive search through all the log fines using kudu.

If I restart app service, application starts working.

Is there any way I can restart my node application once it has crashed. Kind of run for ever no matter what. For example if any error happens in an asp.net code deployed in IIS, IIS never crashes, its keeps of serving other incoming request.

Something like using forever/pm2 in azure app service.

like image 400
Anup Das Gupta Avatar asked Oct 16 '16 18:10

Anup Das Gupta


People also ask

How do I keep node running app?

js application locally after closing the terminal or Application, to run the nodeJS application permanently. We use NPM modules such as forever or PM2 to ensure that a given script runs continuously. NPM is a Default Package manager for Node.

How do I set always on Azure App Service?

05 On the General settings panel, under Platform settings, select On next to Always on configuration setting to enable the Always On feature for the selected Azure App Services web application. Click Save to apply the changes.

How do I run a node js app as a background service?

Method 1: The easiest method to make a node. js app run as a background service is to use the forever tool. The forever is a simple Command Line Interface Tool that ensures that a given particular script runs continuously without any interaction.


1 Answers

node.js in Azure App Services is powered by IISNode, which takes care of everything you described, including monitoring your process for failures and restarting it.

Consider the following POC:

var http = require('http');    
http.createServer(function (req, res) {
    if (req.url == '/bad') {
        throw 'bad';
    }
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('bye');
}).listen(process.env.PORT || 1337);

If I host this in a Web App and issue the following sequence of requests:

  1. GET /
  2. GET /bad
  3. GET /

Then the first will yield HTTP 200, the second will throw on the server and yield HTTP 500, and the third will yield HTTP 200 without me having to do anything. IISNode will just detect the crash and restart the process.

So you shouldn't need PM2 or similar solution because this is built in with App Services. However, if you really want to, they now have App Services Preview on Linux, which is powered by PM2 and lets you configure PM2. More on this here. But again you get this out of the box already.

Another thing to consider is Always On setting which is on by default:

By default, web apps are unloaded if they are idle for some period of time. This lets the system conserve resources. In Basic or Standard mode, you can enable Always On to keep the app loaded all the time. If your app runs continuous web jobs, you should enable Always On, or the web jobs may not run reliably.

This is another possible root cause for your issue and the solution is to disable Always On for your Web App (see the link above).

like image 81
itaysk Avatar answered Nov 14 '22 02:11

itaysk