tjmehta / coworkers

A RabbitMQ Microservice Framework in Node.js
MIT License
609 stars 36 forks source link

Disable forking/cluster completely #44

Closed mkozjak closed 8 years ago

mkozjak commented 8 years ago

Hello!

How to completely disable forking the application?

Should this be enough?

const coworkers = require('coworkers')
const app = coworkers()

process.env.COWORKERS_CLUSTER = false
process.env.COWORKERS_QUEUE = 'foo-queue'
process.env.COWORKERS_QUEUE_WORKER_NUM = 1

// shared middlewares
app.use(function * (next) {
  // all consumers will run this logic...
  yield next
})

app.queue('foo-queue', function * () {
  // consumer specific logic
  this.ack = true // acknowledge message later, see `Context` documentation below
})

app.on('error', function (err) {
  console.error(err.stack)
})

app.connect('amqp://127.0.0.1:5672', function(error) { 
    if (error)
        console.log(error)
})

console.log('hi')

console.log gets triggered five times. Am I doing something wrong?

tjmehta commented 8 years ago

If you're setting environment w/in the file it needs to be before you invoke coworkers(). The environment variables are used as default options in the coworkers(options) factory method.

mkozjak commented 8 years ago

Thanks!

tjmehta commented 8 years ago

I just added a task to document these options, but you can also just use options instead of environment variables to disable clustering.

const app = coworkers({
  cluster: false,
  queueName: 'foo-queue',
  // queueWorkerNum is not required.It is useful if you're using centralized logging and have multiple processes for a single consumer.
})