-
Notifications
You must be signed in to change notification settings - Fork 0
/
workqueue.js
60 lines (50 loc) · 1.44 KB
/
workqueue.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
var config = require('./config');
var mongo = require('mongodb'),
Server = mongo.Server,
Db = mongo.Db,
ObjectID = require('mongodb').ObjectID;
var log = require('./logger');
var server;
var db;
var queue;
exports.init = function(callback) {
server = new Server(config.mongodb.host, parseInt(config.mongodb.port), {auto_reconnect: true});
db = new Db(config.mongodb.database, server);
db.open(function(err, db) {
if(!err) {
log.info("We are connected to mongodb");
// Creating queue collection if it doesn't exist yet
db.createCollection('queue', function(err, collection) {
queue = collection;
callback(err);
return true;
});
}
});
return false;
};
exports.schedule = function(request, callback) {
request.date = new Date();
queue.insert(request, function(err, result) {
callback(err, result[0]._id);
});
};
exports.getQueue = function(project_id, pending, limit, callback) {
queue.find({pending: pending, project: project_id}).sort({date: -1}).limit(limit).toArray(function(err, queue) {
callback(err, queue);
});
};
exports.get = function(id, callback) {
queue.findOne({_id: new ObjectID(id)}, function(err, doc) {
callback(err, doc);
});
};
exports.delete = function(id, callback) {
queue.remove({_id: new ObjectID(id)}, {safe:true}, function(err, number) {
log.warn('Deleting ' + id);
callback(err);
});
};
exports.updateRecord = function(id, request) {
queue.update({_id: new ObjectID(id)}, request);
}