tjmehta / coworkers

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

Running with (in) express app #56

Closed Sebosek closed 7 years ago

Sebosek commented 7 years ago

Hi there,

here is a sample application, where I'm trying to run express and coworkers as one app, but it ends up with an error EADDRINUSE. What is the best strategy for this type of use case?

const RabbitSchema = require('rabbitmq-schema')
const coworkers = require('coworkers');
const express = require('express')

const qname = `coworkers.user.created.${process.env.QUEUE_ID}`;
const topic = new RabbitSchema({
    exchange: 'Cherry',
    type: 'topic',
    bindings: [{
        routingPattern: 'UserManagement.UserCreated',
        destination: {
            queue: qname,
            messageSchema: {
                type: 'object'
            }
        }
    }]
});

const rabbit = coworkers({schema: topic});
rabbit.queue(qname, function * () {
    console.log(this);
    this.ack = true;
});
rabbit.on('error', (err) => console.error(err.stack));
rabbit.connect('amqp://app:cherry@ubuntu');

const app = express();
app.get('/', (req, res) => res.send('Hello World!'));
app.listen(3000, () => console.log('Example app listening on port 3000!'));
Sebosek commented 7 years ago

I'm closing this issue because I decided to use Rabbot in favor of Coworkers.

tjmehta commented 7 years ago

Sorry for the late reply, I am on vacation with limited internet.

Coworkers is spinning up workers using cluster. You are getting EADDRINUSE bc it would cause express to be started in every worker.

You could move express listen into an if block: if cluster.isMaster

tjmehta commented 7 years ago

Lmk if you have any other questions