feathers icon indicating copy to clipboard operation
feathers copied to clipboard

Doc on Clustering not clear

Open hacking-robot opened this issue 4 years ago • 4 comments

Hi I'm following the doc to make a cluster with the app but the doc doesn't match with what's in the default app.js file Since there is a condition that either fork or create the app, how can you export the last line module.exports = app for the forks ?

Do you have any example ?

Link to the doc: https://docs.feathersjs.com/cookbook/general/scaling.html#horizontal-scaling Link to a typical cluster with express: https://gist.github.com/shivkumarganesh/6052143

And Feathers expects module.exports = app as the last line which doesn't exist during a fork if condition.

Thanks.

hacking-robot avatar Oct 22 '20 14:10 hacking-robot

I had an app generated by feathers-plus CLI and it generated two files - app.ts and index.ts, and it sounds similar to what you describe. So what I do is the following:

app.ts

const sync = require('feathers-sync')

const app = express(feathers().configure(reactive({idField: 'id'})))

// Lots of app.configure() and app.use() calls here

// This one is important - configure feathers-sync
app.configure(sync({
  uri: app.get('redis')
}))

const moduleExports = app
export default moduleExports

// rest of the file...

index.ts

import app from './app'
import * as cluster from "cluster";

let server;

const CLUSTER_COUNT = 2

if (cluster.isMaster) {
  for (let i = 0; i < CLUSTER_COUNT; i++) {
    cluster.fork()
  }
} else {
  if (protocol === 'https') {
    server = https.createServer({
      key: fs.readFileSync('/blahblahblah/privkey.pem'),
      cert: fs.readFileSync('/blahblahblah/cert.pem')
    }, app as any).listen(port);
    app.setup(server);
  } else {
    server = app.listen(port)
  }

  server.on('listening', async () => {
    logger.info(`Feathers application started on ${protocol} ://%s:%d`, app.get('host'), port)
    await app.get('sequelizeClient')
  })
}

And it works.

Artaud avatar Dec 16 '20 08:12 Artaud

I've mostly used PM2's cluster mode to deploy and cluster my feathers applications.

https://pm2.keymetrics.io/docs/usage/cluster-mode/

I don't even have to modify the default feathers generated files. PM2 takes care of that for me.

asamolion avatar Jan 05 '21 20:01 asamolion

With pm2 cluster mode, each instance has to run on a different port, is that correct @asamolion ?

Artaud avatar Jan 06 '21 15:01 Artaud

@Artaud Nope, it's not correct, you can setup pm2 to run all instances on one port.

Artaud avatar Feb 03 '21 10:02 Artaud