Skip to content

Commit 5c69e5e

Browse files
committed
Make queue creation sync again, separate ensureIndexes()
1 parent 651967c commit 5c69e5e

File tree

7 files changed

+25
-59
lines changed

7 files changed

+25
-59
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ change it and store it back.
166166
### 0.6.0 (not yet released) ###
167167

168168
* [NEW] The msg.id is now returned on successful Queue.ping() and Queue.ack() calls
169+
* [NEW] Call quueue.ensureIndexes(callback) to create them
169170
* [CHANGE] When a message is acked, 'deleted' is now set to the current time (not true)
171+
* [CHANGE] The queue is now created synchronously
170172

171173
### 0.5.0 (2014-03-21) ###
172174

mongodb-queue.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,9 @@ function datePlus(date, s) {
2424
return (new Date(delayDate)).toISOString()
2525
}
2626

27-
module.exports = function(mongoDbClient, name, opts, callback) {
28-
if ( !callback ) {
29-
callback = opts
30-
opts = {}
31-
}
32-
27+
module.exports = function(mongoDbClient, name, opts) {
3328
var queue = new Queue(mongoDbClient, name, opts)
34-
queue.col.ensureIndex({ visible : 1 }, function(err) {
35-
if (err) return callback(err)
36-
queue.col.ensureIndex({ ack : 1 }, { unique : true, sparse : true }, function(err) {
37-
if (err) return callback(err)
38-
callback(null, queue)
39-
})
40-
})
29+
return queue
4130
}
4231

4332
// the Queue object itself
@@ -56,6 +45,18 @@ function Queue(mongoDbClient, name, opts) {
5645
this.delay = opts.delay || 0
5746
}
5847

48+
Queue.prototype.ensureIndexes = function(callback) {
49+
var self = this
50+
51+
self.col.ensureIndex({ visible : 1 }, function(err) {
52+
if (err) return callback(err)
53+
self.col.ensureIndex({ ack : 1 }, { unique : true, sparse : true }, function(err) {
54+
if (err) return callback(err)
55+
callback()
56+
})
57+
})
58+
}
59+
5960
Queue.prototype.add = function(payload, callback) {
6061
var self = this
6162
var aDate = date()

test/default.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,17 @@ var mongoDbQueue = require('../')
77
setup(function(db) {
88

99
test('first test', function(t) {
10-
mongoDbQueue(db, 'default', function(err, queue) {
11-
t.ok(queue, 'Queue created ok')
12-
t.end()
13-
})
10+
var queue = mongoDbQueue(db, 'default')
11+
t.ok(queue, 'Queue created ok')
12+
t.end()
1413
});
1514

1615
test('single round trip', function(t) {
17-
var queue
16+
var queue = mongoDbQueue(db, 'default')
1817
var msg
1918

2019
async.series(
2120
[
22-
function(next) {
23-
mongoDbQueue(db, 'default', function(err, q) {
24-
queue = q
25-
next(err)
26-
})
27-
},
2821
function(next) {
2922
queue.add('Hello, World!', function(err, id) {
3023
t.ok(!err, 'There is no error when adding a message.')

test/delay.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,10 @@ var mongoDbQueue = require('../')
77
setup(function(db) {
88

99
test('delay: check messages on this queue are returned after the delay', function(t) {
10-
var queue
10+
var queue = mongoDbQueue(db, 'delay', { delay : 3 })
1111

1212
async.series(
1313
[
14-
function(next) {
15-
mongoDbQueue(db, 'delay', { delay : 3 }, function(err, q) {
16-
queue = q
17-
next(err)
18-
})
19-
},
2014
function(next) {
2115
queue.add('Hello, World!', function(err, id) {
2216
t.ok(!err, 'There is no error when adding a message.')

test/multi.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,11 @@ var total = 250
99
setup(function(db) {
1010

1111
test('multi: add ' + total + ' messages, get ' + total + ' back', function(t) {
12-
var queue
12+
var queue = mongoDbQueue(db, 'multi')
1313
var msgs = []
1414

1515
async.series(
1616
[
17-
function(next) {
18-
mongoDbQueue(db, 'multi', function(err, q) {
19-
queue = q
20-
next(err)
21-
})
22-
},
2317
function(next) {
2418
var i, done = 0
2519
for(i=0; i<total; i++) {

test/ping.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,11 @@ var mongoDbQueue = require('../')
77
setup(function(db) {
88

99
test('ping: check a retrieved message with a ping can still be acked', function(t) {
10-
var queue
10+
var queue = mongoDbQueue(db, 'ping', { visibility : 5 })
1111
var msg
1212

1313
async.series(
1414
[
15-
function(next) {
16-
mongoDbQueue(db, 'ping', { visibility : 5 }, function(err, q) {
17-
queue = q
18-
next(err)
19-
})
20-
},
2115
function(next) {
2216
queue.add('Hello, World!', function(err, id) {
2317
t.ok(!err, 'There is no error when adding a message.')

test/visibility.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,10 @@ var mongoDbQueue = require('../')
77
setup(function(db) {
88

99
test('visibility: check message is back in queue after 3s', function(t) {
10-
var queue
10+
var queue = mongoDbQueue(db, 'visibility', { visibility : 3 })
1111

1212
async.series(
1313
[
14-
function(next) {
15-
mongoDbQueue(db, 'visibility', { visibility : 3 }, function(err, q) {
16-
queue = q
17-
next(err)
18-
})
19-
},
2014
function(next) {
2115
queue.add('Hello, World!', function(err) {
2216
t.ok(!err, 'There is no error when adding a message.')
@@ -58,17 +52,11 @@ setup(function(db) {
5852
})
5953

6054
test("visibility: check that a late ack doesn't remove the msg", function(t) {
61-
var queue
55+
var queue = mongoDbQueue(db, 'visibility', { visibility : 3 })
6256
var originalAck
6357

6458
async.series(
6559
[
66-
function(next) {
67-
mongoDbQueue(db, 'visibility', { visibility : 3 }, function(err, q) {
68-
queue = q
69-
next(err)
70-
})
71-
},
7260
function(next) {
7361
queue.add('Hello, World!', function(err) {
7462
t.ok(!err, 'There is no error when adding a message.')

0 commit comments

Comments
 (0)