index.js

index.js is a single-file app that either sends a timestamp every 1.5 seconds to the queue or reads from the queue depending on the invocation argument. The queue location is defined by the QUEUE_HOST environment variable:

'use strict'

const Queue = require('bull');

const veryImportantThingsQueue = new Queue('very_important_things',
{ redis: { port: 6379,
host: process.env.QUEUE_HOST }});

// Prints any message data received
class Receiver {
constructor () {
console.info('Registering listener...');
veryImportantThingsQueue.process(job => {
console.info('Got a message from the queue with data:', job.data);
return Promise.resolve({});
});
}
}

// Sends the date every 1.5 seconds
class Sender {
constructor () {
function sendMessage() {
const messageValue = new Date();
console.info('Sending a message...', messageValue);
veryImportantThingsQueue.add({ 'key': messageValue });
}

setInterval(sendMessage, 1500);
}
}

// Sanity check
if (process.argv.length < 2) {
throw new Error(`Usage: ${process.argv.slice(2).join(' ')} <sender | receiver>`);
}


// Start either receiver or sender depending of CLI arg
console.info('Starting...');
if (process.argv[2] === 'sender') {
new Sender();
} else if (process.argv[2] === 'receiver') {
new Receiver();
} else {
throw new Error(`Usage: ${process.argv.slice(0, 2).join(' ')} <sender | receiver>`);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.5.151