worker.js 894 B

12345678910111213141516171819202122232425262728293031323334
  1. #!/usr/bin/env node
  2. // Process tasks from the work queue
  3. const amqp = require('amqplib');
  4. const queue = 'task_queue';
  5. (async () => {
  6. try {
  7. const connection = await amqp.connect('amqp://localhost');
  8. process.once('SIGINT', async () => {
  9. await connection.close();
  10. });
  11. const channel = await connection.createChannel();
  12. await channel.assertQueue(queue, { durable: true });
  13. channel.prefetch(1);
  14. await channel.consume(queue, (message) => {
  15. const text = message.content.toString();
  16. console.log(" [x] Received '%s'", text);
  17. const seconds = text.split('.').length - 1;
  18. setTimeout(() => {
  19. console.log(" [x] Done");
  20. channel.ack(message);
  21. }, seconds * 1000);
  22. }, { noAck: false });
  23. console.log(" [*] Waiting for messages. To exit press CTRL+C");
  24. }
  25. catch (err) {
  26. console.warn(err);
  27. }
  28. })();