mongodb-queue
Advanced tools
Comparing version 0.5.0 to 0.6.0
@@ -27,16 +27,5 @@ /** | ||
module.exports = function(mongoDbClient, name, opts, callback) { | ||
if ( !callback ) { | ||
callback = opts | ||
opts = {} | ||
} | ||
module.exports = function(mongoDbClient, name, opts) { | ||
var queue = new Queue(mongoDbClient, name, opts) | ||
queue.col.ensureIndex({ visible : 1 }, function(err) { | ||
if (err) return callback(err) | ||
queue.col.ensureIndex({ ack : 1 }, { unique : true, sparse : true }, function(err) { | ||
if (err) return callback(err) | ||
callback(null, queue) | ||
}) | ||
}) | ||
return queue | ||
} | ||
@@ -60,2 +49,14 @@ | ||
Queue.prototype.ensureIndexes = function(callback) { | ||
var self = this | ||
self.col.ensureIndex({ visible : 1 }, function(err) { | ||
if (err) return callback(err) | ||
self.col.ensureIndex({ ack : 1 }, { unique : true, sparse : true }, function(err) { | ||
if (err) return callback(err) | ||
callback() | ||
}) | ||
}) | ||
} | ||
Queue.prototype.add = function(payload, callback) { | ||
@@ -126,3 +127,3 @@ var self = this | ||
} | ||
callback() | ||
callback(null, '' + msg._id) | ||
}) | ||
@@ -140,3 +141,3 @@ } | ||
$set : { | ||
deleted : true, | ||
deleted : (new Date()).toISOString(), | ||
} | ||
@@ -149,4 +150,4 @@ } | ||
} | ||
callback() | ||
callback(null, '' + msg._id) | ||
}) | ||
} |
{ | ||
"name": "mongodb-queue", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Message queues which uses MongoDB.", | ||
@@ -5,0 +5,0 @@ "main": "mongodb-queue.js", |
@@ -19,5 +19,3 @@ # mongodb-queue # | ||
mongodb.MongoClient.connect(con, function(err, db) { | ||
mongoDbQueue(db, 'my-queue', function(err, queue) { | ||
// the 'queue' | ||
}) | ||
var myQueue = mongoDbQueue(db, 'my-queue') | ||
}) | ||
@@ -29,4 +27,5 @@ ``` | ||
```js | ||
queue.add('Hello, World!', function(err) { | ||
// message with payload 'Hello, World!' added | ||
queue.add('Hello, World!', function(err, id) { | ||
// Message with payload 'Hello, World!' added. | ||
// 'id' is returned, useful for logging. | ||
}) | ||
@@ -49,4 +48,5 @@ ``` | ||
```js | ||
queue.ping(msg.ack, function(err) { | ||
// visibility window has now been increased | ||
queue.ping(msg.ack, function(err, id) { | ||
// Visibility window now increased for this message id. | ||
// 'id' is returned, useful for logging. | ||
}) | ||
@@ -59,6 +59,18 @@ ``` | ||
queue.ack(msg.ack, function(err) { | ||
// msg removed from queue | ||
// This msg removed from queue for this message id. | ||
// 'id' is returned, useful for logging. | ||
}) | ||
``` | ||
And if you haven't already, you should call this to make sure indexes have | ||
been added in MongoDB. Of course, if you've called this once (in some kind | ||
one-off script) you don't need to call it in your program. Of course, check | ||
the changelock to see if you need to update them with new releases: | ||
```js | ||
queue.ensureIndexes(function(err) { | ||
// The indexes needed have been added to MongoDB. | ||
}) | ||
``` | ||
## Creating a Queue ## | ||
@@ -73,8 +85,6 @@ | ||
mongoDbQueue(db, 'a-queue', function(err, queue) { | ||
// a queue | ||
}) | ||
mongoDbQueue(db, 'a-queue', function(err, queue) { | ||
// another queue which uses the same collection as above | ||
}) | ||
// an instance of a queue | ||
var queue1 = mongoDbQueue(db, 'a-queue') | ||
// another queue which uses the same collection as above | ||
var queue2 = mongoDbQueue(db, 'a-queue') | ||
``` | ||
@@ -87,5 +97,3 @@ | ||
``` | ||
mongoDbQueue(db, 'resize-queue', { visibility : 30, delay : 15 }, function(err, resizeQueue) { | ||
// the resizeQueue | ||
}) | ||
var resizeQueue = mongoDbQueue(db, 'resize-queue', { visibility : 30, delay : 15 }) | ||
``` | ||
@@ -103,8 +111,4 @@ | ||
``` | ||
mongoDbQueue(db, 'resize-queue', function(err, resizeQueue) { | ||
// the resizeQueue | ||
}) | ||
mongoDbQueue(db, 'notify-queue', function(err, notifyQueue) { | ||
// the notifyQueue | ||
}) | ||
var resizeQueue = mongoDbQueue(db, 'resize-queue') | ||
var notifyQueue = mongoDbQueue(db, 'notify-queue') | ||
``` | ||
@@ -126,5 +130,3 @@ | ||
``` | ||
mongoDbQueue(db, 'queue', { visibility : 15 }, function(err, queue) { | ||
// the queue | ||
}) | ||
var queue = mongoDbQueue(db, 'queue', { visibility : 15 }) | ||
``` | ||
@@ -147,5 +149,3 @@ | ||
``` | ||
mongoDbQueue(db, 'queue', { delay : 10 }, function(err, queue) { | ||
// the queue | ||
}) | ||
var queue = mongoDbQueue(db, 'queue', { delay : 10 }) | ||
``` | ||
@@ -172,2 +172,9 @@ | ||
### 0.6.0 (not yet released) ### | ||
* [NEW] The msg.id is now returned on successful Queue.ping() and Queue.ack() calls | ||
* [NEW] Call quueue.ensureIndexes(callback) to create them | ||
* [CHANGE] When a message is acked, 'deleted' is now set to the current time (not true) | ||
* [CHANGE] The queue is now created synchronously | ||
### 0.5.0 (2014-03-21) ### | ||
@@ -174,0 +181,0 @@ |
@@ -10,10 +10,9 @@ var async = require('async') | ||
test('first test', function(t) { | ||
mongoDbQueue(db, 'default', function(err, queue) { | ||
t.ok(queue, 'Queue created ok') | ||
t.end() | ||
}) | ||
var queue = mongoDbQueue(db, 'default') | ||
t.ok(queue, 'Queue created ok') | ||
t.end() | ||
}); | ||
test('single round trip', function(t) { | ||
var queue | ||
var queue = mongoDbQueue(db, 'default') | ||
var msg | ||
@@ -24,8 +23,2 @@ | ||
function(next) { | ||
mongoDbQueue(db, 'default', function(err, q) { | ||
queue = q | ||
next(err) | ||
}) | ||
}, | ||
function(next) { | ||
queue.add('Hello, World!', function(err, id) { | ||
@@ -52,4 +45,5 @@ t.ok(!err, 'There is no error when adding a message.') | ||
function(next) { | ||
queue.ack(msg.ack, function(err) { | ||
queue.ack(msg.ack, function(err, id) { | ||
t.ok(!err, 'No error when acking the message') | ||
t.ok(id, 'Received an id when acking this message') | ||
next() | ||
@@ -56,0 +50,0 @@ }) |
@@ -10,3 +10,3 @@ var async = require('async') | ||
test('delay: check messages on this queue are returned after the delay', function(t) { | ||
var queue | ||
var queue = mongoDbQueue(db, 'delay', { delay : 3 }) | ||
@@ -16,8 +16,2 @@ async.series( | ||
function(next) { | ||
mongoDbQueue(db, 'delay', { delay : 3 }, function(err, q) { | ||
queue = q | ||
next(err) | ||
}) | ||
}, | ||
function(next) { | ||
queue.add('Hello, World!', function(err, id) { | ||
@@ -24,0 +18,0 @@ t.ok(!err, 'There is no error when adding a message.') |
@@ -12,3 +12,3 @@ var async = require('async') | ||
test('multi: add ' + total + ' messages, get ' + total + ' back', function(t) { | ||
var queue | ||
var queue = mongoDbQueue(db, 'multi') | ||
var msgs = [] | ||
@@ -19,8 +19,2 @@ | ||
function(next) { | ||
mongoDbQueue(db, 'multi', function(err, q) { | ||
queue = q | ||
next(err) | ||
}) | ||
}, | ||
function(next) { | ||
var i, done = 0 | ||
@@ -27,0 +21,0 @@ for(i=0; i<total; i++) { |
@@ -10,3 +10,3 @@ var async = require('async') | ||
test('ping: check a retrieved message with a ping can still be acked', function(t) { | ||
var queue | ||
var queue = mongoDbQueue(db, 'ping', { visibility : 5 }) | ||
var msg | ||
@@ -17,8 +17,2 @@ | ||
function(next) { | ||
mongoDbQueue(db, 'ping', { visibility : 5 }, function(err, q) { | ||
queue = q | ||
next(err) | ||
}) | ||
}, | ||
function(next) { | ||
queue.add('Hello, World!', function(err, id) { | ||
@@ -42,4 +36,5 @@ t.ok(!err, 'There is no error when adding a message.') | ||
// ping this message so it will be kept alive longer, another 5s | ||
queue.ping(msg.ack, function(err) { | ||
queue.ping(msg.ack, function(err, id) { | ||
t.ok(!err, 'No error when pinging a message') | ||
t.ok(id, 'Received an id when acking this message') | ||
// now wait 4s | ||
@@ -50,4 +45,5 @@ setTimeout(next, 4 * 1000) | ||
function(next) { | ||
queue.ack(msg.ack, function(err) { | ||
queue.ack(msg.ack, function(err, id) { | ||
t.ok(!err, 'No error when acking this message') | ||
t.ok(id, 'Received an id when acking this message') | ||
next() | ||
@@ -54,0 +50,0 @@ }) |
@@ -10,3 +10,3 @@ var async = require('async') | ||
test('visibility: check message is back in queue after 3s', function(t) { | ||
var queue | ||
var queue = mongoDbQueue(db, 'visibility', { visibility : 3 }) | ||
@@ -16,8 +16,2 @@ async.series( | ||
function(next) { | ||
mongoDbQueue(db, 'visibility', { visibility : 3 }, function(err, q) { | ||
queue = q | ||
next(err) | ||
}) | ||
}, | ||
function(next) { | ||
queue.add('Hello, World!', function(err) { | ||
@@ -63,3 +57,3 @@ t.ok(!err, 'There is no error when adding a message.') | ||
test("visibility: check that a late ack doesn't remove the msg", function(t) { | ||
var queue | ||
var queue = mongoDbQueue(db, 'visibility', { visibility : 3 }) | ||
var originalAck | ||
@@ -70,8 +64,2 @@ | ||
function(next) { | ||
mongoDbQueue(db, 'visibility', { visibility : 3 }, function(err, q) { | ||
queue = q | ||
next(err) | ||
}) | ||
}, | ||
function(next) { | ||
queue.add('Hello, World!', function(err) { | ||
@@ -78,0 +66,0 @@ t.ok(!err, 'There is no error when adding a message.') |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
229
25467
480