Reactgo: Concurrency Using Throng (Based on a Heroku suggestion)

Created on 17 Oct 2016  路  4Comments  路  Source: reactGo/reactGo

Type: Question / Enhancement

Topic: Setting up concurrency via Throng (https://www.npmjs.com/package/throng) within this architecture.

I'm preparing to deploy a project on Heroku using this architecture and seeking to prepare it to be easily scaleable. As part of this I'm attempting to prepare for the likely possibility of running concurrent tasks.

Throng (https://www.npmjs.com/package/throng) seems like the best solution for this.

My question is: What part of this architecture should I wrap in a "start()" function to run through throng. My best guess is to run the script in "server/index.js" through throng as this seems to call everything. I'd like to make sure before I implement this.

It may also be good to include throng in a later version of this architecture since Heroku recommends it (https://devcenter.heroku.com/articles/node-concurrency).

question

Most helpful comment

Replace throng(WORKERS, startApp); with:

if (ENV === 'development') {
    startApp();
} else {
    throng(WORKERS, startApp);
}

For it to work in development mode.

All 4 comments

This works when the app is built. Going to use the ENV type to determine whether to run Throng or not since it doesn't seem to work in development mode.

   import express from 'express';
   import webpack from 'webpack';
   import { ENV } from './config/appConfig';
   import { connect } from './db';
   import passportConfig from './config/passport';
   import expressConfig from './config/express';
   import routesConfig from './config/routes';
   import throng from 'throng';

  var WORKERS = process.env.WEB_CONCURRENCY || 1;

  var startApp = function() {

    const App = require('../public/assets/server');
    const app = express();

    /*
     * Database-specific setup
     * - connect to MongoDB using mongoose
     * - register mongoose Schema
     */
    connect();

    /*
     * REMOVE if you do not need passport configuration
     */
    passportConfig();

    if (ENV === 'development') {
      const webpackDevConfig = require('../webpack/webpack.config.dev-client');
      const compiler = webpack(webpackDevConfig);
      app.use(require('webpack-dev-middleware')(compiler, {
        noInfo: true,
        publicPath: webpackDevConfig.output.publicPath
      }));

      app.use(require('webpack-hot-middleware')(compiler));
    }

    /*
     * Bootstrap application settings
     */
    expressConfig(app);

    /*
     * REMOVE if you do not need any routes
     *
     * Note: Some of these routes have passport and database model dependencies
     */
    routesConfig(app);

    /*
     * This is where the magic happens. We take the locals data we have already
     * fetched and seed our stores with data.
     * App is a function that requires store data and url
     * to initialize and return the React-rendered html string
     */
    app.get('*', App.default);

    app.listen(app.get('port'));

   };


   throng(WORKERS, startApp);

Replace throng(WORKERS, startApp); with:

if (ENV === 'development') {
    startApp();
} else {
    throng(WORKERS, startApp);
}

For it to work in development mode.

Great analysis! Given that you've set this up, would you like to create a doc for this here so that others can benefit from this as well?

Sounds nice 馃槃 . While i dont think we will include this in the stack since its a pretty specific use case in my opinion.
You might deploy to any cloud service and the configuration would be different.
Regarding thong, it seems to be similar to what node allows you to do using cluster. You can also achieve this using pm2 and start multiple instances (maybe not in dev).

In my experience, it might not always be the best option to use concurrency. It highly depends on the use case. When you do use concurrency you have to react to events when a child dies, and what if the parent dies? Are they somehow related and need to share data? If not then why do you need parent-child relation.
You might as well just wrap your project with a docker and start X containers. Or just run X servers with the application of them if the budget allows it.

In any case, if you would like to open a PR and add this to the docs as suggested before it would be much appreciated.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kenjim83 picture kenjim83  路  8Comments

newasmod picture newasmod  路  4Comments

psimyn picture psimyn  路  6Comments

choonkending picture choonkending  路  9Comments

elitekode picture elitekode  路  8Comments