Skip to content
This repository has been archived by the owner on Nov 6, 2019. It is now read-only.

Commit

Permalink
Migrate to mongojs to avoid peerDependency hell
Browse files Browse the repository at this point in the history
  • Loading branch information
saintedlama committed Dec 9, 2015
1 parent 0a2a88b commit 6489030
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 34 deletions.
4 changes: 2 additions & 2 deletions lib/connection.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
var mongo = require('mongoskin');
var mongo = require('mongojs');
var job = require('./job');
var Queue = require('./queue');
var Worker = require('./worker');

module.exports = Connection;

function Connection(uri, options) {
this.db = mongo.db(uri, options);
this.db = mongo(uri, [], options);
}

Connection.prototype.worker = function (queues, options) {
Expand Down
28 changes: 10 additions & 18 deletions lib/db.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
exports.index = function (collection) {
// Drop old indexes
collection.getIndexes(function(err, indexes) {
if (err) { return console.log(err); }

collection.indexExists('status_1_queue_1_enqueued_1', function (err, indexes) {
if (err) console.error(err);
dropIndex('status_1_queue_1_enqueued_1');
dropIndex('status_1_queue_1_enqueued_1_delay_1');

if (indexes === true) {
collection.dropIndex('status_1_queue_1_enqueued_1', function (err, result) {
if (err) console.error(err);
});
}
});

collection.indexExists('status_1_queue_1_enqueued_1_delay_1', function (err, indexes) {
if (err) console.error(err);

if (indexes === true) {
collection.dropIndex('status_1_queue_1_enqueued_1_delay_1', function (err, result) {
if (err) console.error(err);
});
function dropIndex(name) {
if (indexes.some(function(index) { return index.name == name; })) {
collection.dropIndex(name, function(err) {
if (err) { console.error(err); }
});
}
}
});

// Ensures there's a reasonable index for the poling dequeue
// Status is first b/c querying by status = queued should be very selective

collection.ensureIndex({ status: 1, queue: 1, priority: 1, _id: 1, delay: 1 }, function (err) {
if (err) console.error(err);
});
Expand Down
1 change: 0 additions & 1 deletion lib/job.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var events = require('events');
var mongoskin = require('mongoskin');
var util = require('util');

module.exports = Job;
Expand Down
22 changes: 15 additions & 7 deletions lib/queue.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var mongoskin = require('mongoskin');
var mongo = require('mongojs');
var db = require('./db');
var Job = require('./job');

Expand Down Expand Up @@ -33,7 +33,7 @@ Queue.prototype.get = function (id, callback) {
var self = this;

if (typeof id === 'string') {
id = new mongoskin.helper.toObjectID(id);
id = new mongo.ObjectID(id);
}

var query = { _id: id };
Expand Down Expand Up @@ -94,15 +94,23 @@ Queue.prototype.dequeue = function (options, callback) {
query.name = { $in: callback_names };
}

var sort = [['priority', 'desc'], ['_id', 'asc']];
var sort = {
'priority': -1,
'_id': 1
};

var update = { $set: { status: Job.DEQUEUED, dequeued: new Date() }};
var options = { new: true };

this.collection.findAndModify(query, sort, update, options, function (err, doc) {
this.collection.findAndModify({
query: query,
sort: sort,
update: update,
new: true
}, function (err, doc) {
if (err) return callback(err);
if (!doc || !doc.value) return callback();
if (!doc) return callback();

callback(null, self.job(doc.value));
callback(null, self.job(doc));
});
};

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"url": "git://github.com/scttnlsn/monq.git"
},
"dependencies": {
"mongodb": "~2.0.44",
"mongoskin": "^2.0.0"
"mongojs": "^2.1.0"
},
"devDependencies": {
"async": "^1.5.0",
Expand Down
4 changes: 2 additions & 2 deletions test/helpers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var async = require('async');
var mongo = require('mongoskin');
var mongo = require('mongojs');

exports.uri = process.env.MONGODB_URI || 'mongodb://localhost:27017/monq_tests';

exports.db = mongo.db(exports.uri, { safe: true });
exports.db = mongo(exports.uri, [], { safe: true });

exports.each = function (fixture, fn, done) {
async.each(fixture, function (args, callback) {
Expand Down
4 changes: 2 additions & 2 deletions test/test_job.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('Job', function () {
});

it('is inserted into collection', function (done) {
collection.findById(job.data._id, function (err, doc) {
collection.findOne({ _id : job.data._id }, function (err, doc) {
if (err) return done(err);

assert.ok(doc);
Expand All @@ -43,7 +43,7 @@ describe('Job', function () {
});

it('contains a string id', function (done) {
collection.findById(job.data._id, function (err, doc) {
collection.findOne({ _id : job.data._id }, function (err, doc) {
if (err) return done(err);

assert.equal(doc._id.toString(), job.data.id);
Expand Down

0 comments on commit 6489030

Please sign in to comment.