Round-robin: distribute time-consuming tasks among multiple workers

callback 옵션

noAck: false

Soon after the worker terminates, all unacknowledged messages are redelivered.

캐싱 서버에 적용할 수 있는 옵션. receiver(메인 서버 역할)가 처리하지 못한 요청을 저장해두었다가 다시 전송

Queue & Message 보존 옵션

we need to mark both the queue and messages as durable.

  1. channel.assertQueue('hello', {durable: true});
  2. channel.sendToQueue(queue, Buffer.from(msg), {persistent: true});

캐싱 서버에 적용할 수 있는 옵션. sender(캐싱 서버의 메세지 큐 - RabbitMQ 역할)가 비정상적으로 종료되었을 때 Queue와 Message를 잃지 않도록.

prefetch 옵션

This tells RabbitMQ not to give more than one message to a worker at a time.

channel.prefetch(1);

Untitled

#!/usr/bin/env node

var amqp = require("amqplib/callback_api");

amqp.connect(
  "amqp://myuser:secret@localhost:5672",
  function (error0, connection) {
    if (error0) {
      throw error0;
    }
    connection.createChannel(function (error1, channel) {
      if (error1) {
        throw error1;
      }
      var queue = "task_queue";
      var msg = process.argv.slice(2).join(" ") || "Hello World!";

      channel.assertQueue(queue, {
        durable: true,
      });
      channel.sendToQueue(queue, Buffer.from(msg), {
        persistent: true,
      });
      console.log(" [x] Sent '%s'", msg);
    });
    setTimeout(function () {
      connection.close();
      process.exit(0);
    }, 500);
  }
);
#!/usr/bin/env node

var amqp = require("amqplib/callback_api");

amqp.connect(
  "amqp://myuser:secret@localhost:5672",
  function (error, connection) {
    connection.createChannel(function (error, channel) {
      var queue = "task_queue";

      channel.assertQueue(queue, {
        durable: true,
      });
      channel.prefetch(1);
      console.log(
        " [*] Waiting for messages in %s. To exit press CTRL+C",
        queue
      );
      channel.consume(
        queue,
        function (msg) {
          var secs = msg.content.toString().split(".").length - 1;

          console.log(" [x] Received %s", msg.content.toString());
          setTimeout(function () {
            console.log(" [x] Done");
            channel.ack(msg);
          }, secs * 1000);
        },
        {
          noAck: false,
        }
      );
    });
  }
);