Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: auto reconnect in case broker fail #29

Closed
H01001000 opened this issue Jun 25, 2022 · 2 comments
Closed

feat: auto reconnect in case broker fail #29

H01001000 opened this issue Jun 25, 2022 · 2 comments

Comments

@H01001000
Copy link

I hope to you can implement this feature because when the broker dies, we need to restart the deno to get it to reconnect, which is unacceptable for event handling.

@lenkan
Copy link
Owner

lenkan commented Jun 26, 2022

Thanks for reporting. You should not have to restart deno to reconnect, but rather create a new connection and run through your setup flow.

As mentioned in a similar feature request here: amqp-node/amqplib#25, reconnecting a client to the broker involves more things than just setting up the AMQP Connection.

Here is the consume_message.ts example rewritten to handle connection errors:

import { connect } from "../mod.ts";

const queueName = Deno.args[0];
if (!queueName) {
  console.log(`No queue name specified`);
  Deno.exit(1);
}

while (true) {
  const connection = await connect({ hostname: "127.0.0.1" });

  try {
    const channel = await connection.openChannel();

    await channel.declareQueue({ queue: queueName });
    await channel.consume(
      { queue: queueName },
      async (args, props, data) => {
        console.log(JSON.stringify(args));
        console.log(JSON.stringify(props));
        console.log(new TextDecoder().decode(data));
        await channel.ack({ deliveryTag: args.deliveryTag });
      },
    );

    await connection.closed();
  } catch (error) {
    console.error(error);
    await new Promise((resolve) => setTimeout(resolve, 1000));
  } finally {
    await connection.close();
  }
}

@lenkan lenkan closed this as completed Dec 7, 2022
@lenkan lenkan closed this as not planned Won't fix, can't repro, duplicate, stale Dec 7, 2022
@bramtechs
Copy link

bramtechs commented Nov 21, 2024

Thanks for reporting. You should not have to restart deno to reconnect, but rather create a new connection and run through your setup flow.

As mentioned in a similar feature request here: amqp-node/amqplib#25, reconnecting a client to the broker involves more things than just setting up the AMQP Connection.

Here is the consume_message.ts example rewritten to handle connection errors:

import { connect } from "../mod.ts";

const queueName = Deno.args[0];
if (!queueName) {
  console.log(`No queue name specified`);
  Deno.exit(1);
}

while (true) {
  const connection = await connect({ hostname: "127.0.0.1" });

  try {
    const channel = await connection.openChannel();

    await channel.declareQueue({ queue: queueName });
    await channel.consume(
      { queue: queueName },
      async (args, props, data) => {
        console.log(JSON.stringify(args));
        console.log(JSON.stringify(props));
        console.log(new TextDecoder().decode(data));
        await channel.ack({ deliveryTag: args.deliveryTag });
      },
    );

    await connection.closed();
  } catch (error) {
    console.error(error);
    await new Promise((resolve) => setTimeout(resolve, 1000));
  } finally {
    await connection.close();
  }
}

This code does not seem to handle the error-case where the queue does not exist.

with:

const queueName = "nonexistingqueue";
PS C:\dev\amqp-test> deno run --allow-net test.ts
error: Uncaught (in promise) Error: EOF
      throw new FrameError("EOF");     
            ^
    at AmqpFrameReader.#readBytes (https://deno.land/x/amqp@v0.24.0/src/amqp_frame_reader.ts:18:13)        
    at eventLoopTick (ext:core/01_core.js:175:7)
    at async AmqpFrameReader.#readFrame (https://deno.land/x/amqp@v0.24.0/src/amqp_frame_reader.ts:25:20)  
    at async AmqpSocket.read (https://deno.land/x/amqp@v0.24.0/src/amqp_socket.ts:159:14)
    at async listen (https://deno.land/x/amqp@v0.24.0/src/amqp_multiplexer.ts:108:23)

The line const connection = await connect({ hostname: "127.0.0.1" }); needs to be inside the try-catch block too in order to catch this error.

I've been stumped for a while figuring out why this library seems to crash my server in one way or another. 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants