moleculerjs / moleculer-channels

Reliable messages for Moleculer services via external queue/channel/topic.
MIT License
72 stars 15 forks source link

Bug or Feature? amqp: SendToChannel without Listener => Error 404 - no exchange #37

Closed me23 closed 11 months ago

me23 commented 2 years ago

Prerequisites

Please answer the following questions for yourself before submitting an issue.

Current Behavior

Starting with an fresh instance of rabbitmq and trigger sendToChannel from moleculer i got follow Error: [2022-05-10T07:30:40.939Z] ERROR my-client/CHANNELS: AMQP channel error Error: Channel closed by server: 404 (NOT-FOUND) with message "NOT_FOUND - no exchange 'something.done' in vhost '/'"

and the client exits with unhandled Promise-rejection.

As soon if i start a listener for this channel (somthing.done) once, even if i stop the listener again, then it works like expected.

Expected Behavior

I don't know if its an bug or a feature, but i expected the channel to be created either if the first listener register or if the first producer send some messages in.

My idea was to send some events with a ttl-value, so if nobody cares about it, it will be deleted. As soon as one consumer came up, he can use the stored events. no exchange

Failure Information

[2022-05-10T07:30:40.939Z] ERROR my-client/CHANNELS: AMQP channel error Error: Channel closed by server: 404 (NOT-FOUND) with message "NOT_FOUND - no exchange 'something.done' in vhost '/'"
at Channel.C.accept (/home/me/public_html/moleculer-channel-test/node_modules/amqplib/lib/channel.js:422:17)
    at Connection.mainAccept [as accept] (/home/me/public_html/moleculer-channel-test/node_modules/amqplib/lib/connection.js:64:33)
    at Socket.go (/home/me/public_html/moleculer-channel-test/node_modules/amqplib/lib/connection.js:478:48)
    at Socket.emit (node:events:527:28)
    at Socket.emit (node:domain:475:12)
    at emitReadable_ (node:internal/streams/readable:601:12)
    at processTicksAndRejections (node:internal/process/task_queues:82:21) {
  code: 404,
  classId: 60,
  methodId: 40
}
[2022-05-10T07:30:40.941Z] ERROR my-client/CHANNELS: AMQP channel closed.

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. start a fresh instance of rabbitmq
  2. start producer that trigger sendToChannel("event.occured")
  3. => Error

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

Failure Logs

icebob commented 2 years ago

Good question, but I don't think it's a bug. The publish doesn't create queues just puts the message to existing queues. Libraries of other adapters auto-create the queues if not exist. I will check what we can do, because in RabbitMQ there is several settings of the queues, so it means, you should define these settings in every publish (because you don't know whether the queue is exist or not). But I think it's not a good solution.

me23 commented 2 years ago

Maybe we could define a auto-create option and if it set, the service will check and create the necessary channels on startup?

ujwal-setlur commented 1 year ago

Just to add, not sure if this is a different issue, but I did the following:

  1. from service A, send a message on a channel/topic where there were subscribers. Message was received.
  2. from service A again, sent a message on a channel/topic there were no subscribers. As expected, got an error that there were no subscribers
  3. from service A again, send message from step 1 where there are subscribers. It fails!
 IllegalOperationError: Channel closed

Step 3 should work, since listener is still available!

ujwal-setlur commented 1 year ago

Just tested with master branch. It is now fixed. Guess I have to ask when it will be released? :-)

ujwal-setlur commented 1 year ago

I did one more test on the master branch where this code has been merged:

  1. Started publisher, but not subscriber. Sent message. No error from publisher side. But message clearly not delivered since there is no subscriber.
  2. Started subscriber. No message was received. Is this this expected behavior? I would have thought the message would be received as soon as subscriber comes up.
  3. Any new message from publisher after subscriber comes up is delivered.
ujwal-setlur commented 1 year ago

Actually, if a subscriber comes, then goes away (say a crash), and a message is sent in the meantime, the message is delivered when the subscriber comes back. The only issue here seems to be if the subscriber never started and a message is sent, and then the subscriber connects, the message is not delivered.

icebob commented 1 year ago

Currently the subscriber creates the queues, so if you send the message when the queue is not exists, it won't be delivered. However, it should throw error if no queue.

ujwal-setlur commented 1 year ago

It did not, or at least I don't think so, and subsequent messages to queues that did exist would also not work. However, this seems have been fixed in the master branch. But now if the queue does not exist, it won't crash, but it does not seem to throw an error either. Message is just swallowed/dropped. I am testing from the REPL console.

AndreMaz commented 1 year ago

Can you please provide a repro example? I would like to debug the issue

me23 commented 1 year ago

Clone this repo: https://github.com/me23/moleculer-channels-amqp-test

Failure

  1. Start rabbitmq with docker-compose up
  2. start client: node source/client.js => Error.

Success:

  1. Start rabbitmq with docker-compose up
  2. start service: node source/service.js
  3. start client: node source/client.js => No Error
icebob commented 11 months ago

You should define the publishAssertExchange to use this feature. More info: https://github.com/moleculerjs/moleculer-channels/pull/63#pullrequestreview-1302006083