-
Notifications
You must be signed in to change notification settings - Fork 2
/
queues.js
149 lines (112 loc) · 3.32 KB
/
queues.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var queues = (function () {
var amqp = require('amqplib');
var when = require('when');
var amqpUri = process.env.AMQP_URI || 'amqp://localhost';
function createChannel(conn) {
console.log("connected");
connection = conn;
conn.on('error', function (err) {
console.log(err);
});
return conn.createChannel();
}
function iterate(queue, callback, ch) {
return when.iterate(function() { return ch.get(queue); },
function(msg) { return msg === false; },
callback,
undefined
);
}
function parse(msg) {
return {
id: msg.fields.deliveryTag,
routingKey: msg.fields.routingKey,
properties: msg.properties,
content: msg.content.toString(),
timestamp: msg.timestamp
};
}
function listen(exchange, callback, ch) {
return ch.assertQueue(null, {autodelete: true})
.then(function(ok) {
ch.bindQueue(ok.queue, exchange, '#');
return ch.consume(ok.queue, function(msg) { callback(parse(msg));}, {noAck: true});
});
}
function disconnect() {
console.log("connection closed");
return connection.close();
}
function redeliver(content, exchange, routingkey) {
console.log("redelivering!");
return connection.createChannel()
.then(publish.bind(undefined, content, exchange, routingkey));
}
function publish(content, exchange, routingkey, ch) {
console.log("publishing to original queue");
ch.publish(exchange, routingkey, new Buffer(content));
return ch.close();
}
function ackIfMatches(ch, deliveryTag, msg) {
if (msg && msg.fields.deliveryTag == deliveryTag) {
return ch.ack(msg);
}
}
function redeliverIfMatches(ch, deliveryTag, msg) {
if (msg && msg.fields.deliveryTag == deliveryTag) {
var content = msg.content.toString();
var queue = msg.properties.headers['x-death'][0]['queue'];
ch.ack(msg);
return redeliver(content, '', queue);
}
}
return {
peek: function(queue) {
console.log('peeking queue ' + queue + ' on ' + amqpUri);
var messages = [];
return amqp.connect(amqpUri)
.then(createChannel)
.then(iterate.bind(undefined, queue, function(msg) { if (msg) messages.push(parse(msg)); } ))
.then(disconnect)
.then(function() {
console.log('done fetching messages');
return messages;
});
},
requeue: function(queue, deliveryTag) {
return amqp.connect(amqpUri)
.then(createChannel)
.then(
function(ch) {
return iterate(queue, redeliverIfMatches.bind(undefined, ch, deliveryTag), ch);
})
.delay(100)
.then(disconnect)
.then(function() {
console.log('done requeueing');
return true;
});
},
delete: function(queue, deliveryTag) {
console.log("deleting delivery tag " + deliveryTag + " on " + queue);
return amqp.connect(amqpUri)
.then(createChannel)
.then(
function(ch) {
return iterate(queue, ackIfMatches.bind(undefined, ch, deliveryTag), ch);
})
.delay(100)
.then(disconnect)
.then(function() {
console.log('done deleting');
return true;
});
},
startListening: function(exchange, callback) {
return amqp.connect(amqpUri)
.then(createChannel)
.then(listen.bind(undefined, exchange, callback));
}
};
}());
module.exports = queues;