Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use when.ensure to close connections regardless of result

Previously, I was closing the connection only success; however, I
really want to close the connection whatever happens in the connection
open continuation. Closing only on success means the program keeps
running (because I/O to do ..) on errors.
  • Loading branch information...
commit c59975286aba752f7cf23d48aee1f2e15fda7d6a 1 parent 2b1acae
Michael Bridgen authored
13 examples/tutorials/emit_log.js
View
@@ -1,18 +1,19 @@
#!/usr/bin/env node
var amqp = require('amqplib');
+var when = require('when');
amqp.connect('amqp://localhost').then(function(conn) {
- return conn.createChannel().then(function(ch) {
- var ok = ch.assertExchange('logs', 'fanout', {durable: false})
+ return when(conn.createChannel().then(function(ch) {
+ var ex = 'logs';
+ var ok = ch.assertExchange(ex, 'fanout', {durable: false})
var message = process.argv.slice(2).join(' ') ||
'info: Hello World!';
- ok = ok.then(function() {
- ch.publish('logs', '', new Buffer(message));
+ return ok.then(function() {
+ ch.publish(ex, '', new Buffer(message));
console.log(" [x] Sent '%s'", message);
});
- return ok.then(function() { conn.close(); });
- });
+ })).ensure(function() { conn.close(); });
}).then(null, console.warn);
8 examples/tutorials/emit_log_direct.js
View
@@ -1,20 +1,20 @@
#!/usr/bin/env node
var amqp = require('amqplib');
+var when = require('when');
var args = process.argv.slice(2);
var severity = (args.length > 0) ? args[0] : 'info';
var message = args.slice(1).join(' ') || 'Hello World!';
amqp.connect('amqp://localhost').then(function(conn) {
- return conn.createChannel().then(function(ch) {
+ return when(conn.createChannel().then(function(ch) {
var ex = 'direct_logs';
var ok = ch.assertExchange(ex, 'direct', {durable: false});
- ok = ok.then(function() {
+ return ok.then(function() {
ch.publish(ex, severity, new Buffer(message));
console.log(" [x] Sent %s:'%s'", severity, message);
});
- return ok.then(function() { conn.close(); });
- });
+ })).ensure(function() { conn.close(); });
}).then(null, console.warn);
14 examples/tutorials/new_task.js
View
@@ -2,17 +2,17 @@
// Post a new task to the work queue
var amqp = require('amqplib');
+var when = require('when');
amqp.connect('amqp://localhost').then(function(conn) {
- return conn.createChannel().then(function(ch) {
- var ok = ch.assertQueue('task_queue', {durable: true});
+ return when(conn.createChannel().then(function(ch) {
+ var q = 'task_queue';
+ var ok = ch.assertQueue(q, {durable: true});
- ok.then(function() {
+ return ok.then(function() {
var msg = process.argv.slice(2).join(' ') || "Hello World!"
- ch.sendToQueue('task_queue', new Buffer(msg),
- {deliveryMode: true});
+ ch.sendToQueue(q, new Buffer(msg), {deliveryMode: true});
console.log(" [x] Sent '%s'", msg);
});
- return ok;
- }).then(function() { conn.close(); });
+ })).ensure(function() { conn.close(); });
}).then(null, console.warn);
12 examples/tutorials/receive_logs_direct.js
View
@@ -30,14 +30,16 @@ amqp.connect('amqp://localhost').then(function(conn) {
});
ok = ok.then(function(queue) {
- return ch.consume(queue, function(msg) {
- console.log(" [x] %s:'%s'",
- msg.fields.routingKey,
- msg.content.toString());
- }, {noAck: true});
+ return ch.consume(queue, logMessage, {noAck: true});
});
return ok.then(function() {
console.log(' [*] Waiting for logs. To exit press CTRL+C.');
});
+
+ function logMessage(msg) {
+ console.log(" [x] %s:'%s'",
+ msg.fields.routingKey,
+ msg.content.toString());
+ }
});
}).then(null, console.warn);
12 examples/tutorials/send.js
View
@@ -1,16 +1,18 @@
#!/usr/bin/env node
var amqp = require('amqplib');
+var when = require('when');
amqp.connect('amqp://localhost').then(function(conn) {
- return conn.createChannel().then(function(ch) {
+ return when(conn.createChannel().then(function(ch) {
+ var q = 'hello';
var msg = 'Hello World!';
- var ok = ch.assertQueue('hello', {durable: false});
+ var ok = ch.assertQueue(q, {durable: false});
return ok.then(function(_qok) {
- ch.sendToQueue('hello', new Buffer(msg));
+ ch.sendToQueue(q, new Buffer(msg));
console.log(" [x] Sent '%s'", msg);
- }).then(function() { conn.close(); });
- });
+ });
+ })).ensure(function() { conn.close(); });;
}).then(null, console.warn);
Please sign in to comment.
Something went wrong with that request. Please try again.