<commit_before>'use strict';

const amqp = require('amqp');
const events = require('../events');

module.exports = function AmqpListener(config) {
	let connected;

	console.log('Initialising Rabbit MQ listener');

	function messageReceived(queue, msg) {
		console.log('Message received', { msg: msg });

		if (!msg.data) {
			console.log('Message received with no data.', { msg: msg });
			return queue.shift();
		}

		events.emit('message-received', JSON.parse(msg.data.toString('utf-8')));
		queue.shift();
	}

	function queueReady(resolve, queue) {
		console.log('Connected to Queue');
		console.log('Binding Queue to exchange.', { exchange: config.exchange, routingKey: config.routing });
		queue.bind(config.exchange, config.routing);

		queue.subscribe({ ack: true }, messageReceived.bind(undefined, queue));

		console.log('Queue connected and bound.');

		resolve();
	}

	function connectionReady(resolve, reject, connection) {
		if (connected) {
			return;
		}

		connected = true;
		console.log('Connected to Rabbit MQ');
		console.log('Connecting to Queue to Rabbit MQ', { queue: config.queue });

		connection.queue(config.queue, { autoDelete: false }, queueReady.bind(undefined, resolve));
	}

	function startUp(eventEmitter, resolve, reject) {
		const options = { host: config.host };

		if (config.port) {
			options.port = config.port;
		}

		const connection = amqp.createConnection(options);

		console.log('Connecting to Rabbit MQ', options);

		connection.on('ready', connectionReady.bind(undefined, resolve, reject, connection));

		connection.on('error', function(e) {
			console.log('Error connecting to RabbitMQ', { message: e.message });
		});

		connection.on('end', function() {
			console.log('Rabbit MQ connection lost');
		});
	}

	function start(eventEmitter) {
		return new Promise(startUp.bind(undefined, eventEmitter));
	}

	return {
		start: start
	};
};<commit_msg>Refactor of AMQP<commit_after>'use strict';

const amqp = require('amqp');
const events = require('../events');

module.exports = function AmqpListener(config) {
	let connected;

	console.log('Initialising Rabbit MQ listener');

	function messageReceived(queue, msg) {
		console.log('Message received', { msg: msg });

		if (!msg.data) {
			console.log('Message received with no data.', { msg: msg });
			return queue.shift();
		}

		events.emit('message-received', JSON.parse(msg.data.toString('utf-8')));
		queue.shift();
	}

	function connect(options) {
		return new Promise((resolve) => {
			const connection = amqp.createConnection(options);

			connection.on('ready', () => {
				if (connected) {
					return;
				}

				connected = true;

				resolve(connection);
			});

			connection.on('error', e => console.log('Error connecting to RabbitMQ', { message: e.message }));

			connection.on('end', () => console.log('Rabbit MQ connection lost'));
		});
	}

	function connectToQueue(connection) {
		return new Promise(resolve => {
			connection.queue(config.queue, { autoDelete: false }, queue => resolve(queue));
		});
	}

	function bindQueueToExchange(queue) {
		return new Promise((resolve) => {
			queue.bind(config.exchange, config.routing);
			resolve(queue);
		});
	}

	function subscribeToQueue(queue) {
		return new Promise(resolve => {
			queue.subscribe({ ack: true }, messageReceived.bind(undefined, queue));
			resolve(queue);
		});
	}

	function startUp() {
		const options = { host: config.host };

		if (config.port) {
			options.port = config.port;
		}

		console.log('Connecting to Rabbit MQ', options);

		return connect(options)
			.then(connectToQueue)
			.then(subscribeToQueue)
			.then(bindQueueToExchange);
	}

	return {
		start: startUp
	};
};
{'fraction_non_alphanumeric': 0.15696409140369968, 'fraction_numerical': 0.000544069640914037, 'mean_word_length': 4.208215297450425, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '484208', 'n_tokens_mistral': 1270, 'n_tokens_neox': 1109, 'n_words': 258}