-
-
Save it-one-mm/21e38b3b13d6c5d31bff9148c5da8f40 to your computer and use it in GitHub Desktop.
Heroku worker process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const amqp = require('amqp-connection-manager') | |
const AMQP_URL = process.env.CLOUDAMQP_URL || 'amqp://localhost'; | |
if (!AMQP_URL) process.exit(1) | |
const WORKER_QUEUE = 'worker-queue' | |
// Create a new connection manager from AMQP | |
var connection = amqp.connect([AMQP_URL]) | |
console.log('[AMQP] - Connecting....') | |
connection.on('connect', function() { | |
process.once('SIGINT', function() { // Close conn on exit | |
connection.close() | |
}) | |
return console.log('[AMQP] - Connected!') | |
}) | |
connection.on('disconnect', function(params) { | |
return console.error('[AMQP] - Disconnected.', params.err.stack) | |
}) | |
// ---------- To receive the execution task messages | |
let channelWrapper = connection.createChannel({ | |
json: true, | |
setup: function(channel) { | |
return Promise.all([ | |
channel.assertQueue(WORKER_QUEUE, { autoDelete: false, durable: true }), | |
channel.prefetch(1), | |
channel.consume(WORKER_QUEUE, onMessage) | |
]) | |
} | |
}) | |
channelWrapper.waitForConnect() | |
.then(function() { | |
console.log('[AMQP] - Listening for messages on queue => '+WORKER_QUEUE) | |
}) | |
.catch(function(err) { | |
console.error('[AMQP] - Error! ', err) | |
}) | |
// Process message from AMQP | |
function onMessage(data) { | |
let message | |
try { | |
message = JSON.parse(data.content.toString()) | |
} catch(e) { | |
console.error('[AMQP] - Error parsing message... ', data) | |
} | |
console.log('[AMQP] - Message incoming... ', message) | |
channelWrapper.ack(data) | |
if (!message) { | |
return | |
} | |
switch (message.taskName) { | |
case 'resetInboxCount': | |
// do something.... | |
break | |
case 'otheTask': | |
// do another thing.... | |
break | |
default: | |
console.error('No task was found with name => '+message.taskName) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment