new_task.js 636 B

12345678910111213141516171819202122232425
  1. #!/usr/bin/env node
  2. // Post a new task to the work queue
  3. const amqp = require('amqplib');
  4. const queue = 'task_queue';
  5. const text = process.argv.slice(2).join(' ') || "Hello World!";
  6. (async () => {
  7. let connection;
  8. try {
  9. connection = await amqp.connect('amqp://localhost');
  10. const channel = await connection.createChannel();
  11. await channel.assertQueue(queue, { durable: true });
  12. channel.sendToQueue(queue, Buffer.from(text), { persistent: true });
  13. console.log(" [x] Sent '%s'", text);
  14. await channel.close();
  15. }
  16. catch (err) {
  17. console.warn(err);
  18. }
  19. finally {
  20. await connection.close();
  21. };
  22. })();