chopper draw lion4
chopper draw lion4

Reputation: 13507

How do I setup a background process on a express.js app hosting on Azure websites?

I have an Express app that is currently running on Azure. It works great, but my Express app has a service which connects to a streaming API and populates the database. This service requires it's own process. Normally when I host this locally I just type

node listen

to start streaming, and then open another window and type

node app.js

to start the server.

But how do I do this when launching my node app on Azure?

Upvotes: 0

Views: 3965

Answers (1)

Peter Lyons
Peter Lyons

Reputation: 146154

I recommend converting your import process to just an in-process background job, which is trivial in a non-clustered node app. You can do the scheduling via the cron npm module or one of the many similar modules available. Just code your import routine as a function.

So instead of in listen.js having your code just execute top-level, put it into a named function and export it:

function populateDatabase() {
  //connect to streaming API
  //populate database
}

module.exports = populateDatabase;

Then in app.js you can wire that up with setInterval or the node scheduling/cron module of your choice:

//app.js
var populateDatabase = require("./listen");
setInterval(60 * 1000, populateDatabase);
// rest of app setup/start code

Upvotes: 2

Related Questions