Skip to content

Commit eb86f33

Browse files
committed
Initial commit
1 parent f757f1c commit eb86f33

File tree

7 files changed

+480
-0
lines changed

7 files changed

+480
-0
lines changed

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./lib/toureiro.js');

lib/models/job.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
var _ = require('lodash');
2+
var client = require('../redis');
3+
var queue = require('./queue');
4+
5+
module.exports = {
6+
7+
/**
8+
* Get a job by id
9+
* @param {string} qName Queue name
10+
* @param {string} id Job id
11+
*/
12+
get: function(qName, id) {
13+
var q = queue.get(qName);
14+
return q.getJob(id);
15+
},
16+
17+
/**
18+
* Remove a job by id
19+
* @param {string} qName Queue name
20+
* @param {string} id Job id
21+
*/
22+
remove: function(qName, id) {
23+
var q = queue.get(qName);
24+
return q.getJob(id).then(function(job) {
25+
return job.remove();
26+
});
27+
},
28+
29+
/**
30+
* Get the total number of jobs of type
31+
* @param {string} qName Queue name
32+
* @param {string} type Job type: {wait|active|delayed|completed|failed}
33+
* @returns {number} Total number of jobs
34+
*/
35+
total: function(qName, type) {
36+
var key = 'bull:' + qName + ':' + type;
37+
if (type === 'wait' || type === 'active') {
38+
return client.llenAsync(key);
39+
} else if (type === 'delayed') {
40+
return client.zcardAsync(key);
41+
} else if (type === 'completed' || type === 'failed') {
42+
return client.scardAsync(key);
43+
}
44+
throw new Error('You must provide a valid job type.');
45+
},
46+
47+
/**
48+
* Fetch a number of jobs of certain type
49+
* @param {string} qName Queue name
50+
* @param {string} type Job type: {wait|active|delayed|completed|failed}
51+
* @param {number} offset Index offset (optional)
52+
* @param {number} limit Limit of the number of jobs returned (optional)
53+
* @returns {Promise} A promise that resolves to an array of jobs
54+
*/
55+
fetch: function(qName, type, offset, limit) {
56+
var q = queue.get(qName);
57+
if (!(offset >= 0)) {
58+
offset = 0;
59+
}
60+
if (!(limit >= 0)) {
61+
limit = 30;
62+
}
63+
if (type === 'wait' || type === 'active') {
64+
return q.getJobs(type, 'LIST', offset, offset + limit - 1);
65+
} else if (type === 'delayed') {
66+
return q.getJobs(type, 'ZSET', offset, offset + limit - 1);
67+
} else if (type === 'completed' || type === 'failed') {
68+
var key = 'bull:' + qName + ':' + type;
69+
return client.smembersAsync(key).then(function(ids) {
70+
var _ids = ids.slice(offset, offset + limit);
71+
return Promise.all(_.map(_ids, function(id) {
72+
return q.getJob(id);
73+
}));
74+
});
75+
}
76+
throw new Error('You must provide a valid job type.');
77+
}
78+
79+
};

lib/models/queue.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
var _ = require('lodash');
2+
var bull = require('bull');
3+
var client = require('../redis');
4+
5+
module.exports = {
6+
7+
/**
8+
* Internal Queue instance
9+
* since each queue creates separate connections to redis,
10+
* we want to store an instance of the client here for reuse
11+
*/
12+
_q: {
13+
name: undefined,
14+
instance: undefined
15+
},
16+
17+
/**
18+
* List all queues
19+
* @returns {Promise} A promise that resolves to the keys of all queues
20+
*/
21+
list: function() {
22+
return client.keysAsync('bull:*:id').then(function(keys) {
23+
return _.map(keys, function(key) {
24+
return key.slice(5, -3);
25+
});
26+
});
27+
},
28+
29+
/**
30+
* Get total number of jobs
31+
* @param {string} qName Queue name
32+
* @returns {Promise} A promise that resovles to the total number of jobs
33+
*/
34+
total: function(qName) {
35+
return client.getAsync('bull:' + qName + ':id')
36+
},
37+
38+
/**
39+
* Delete all data of a queue
40+
* @param {string} qName Queue name
41+
* @returns {Promise} A promise when all data of the queue is deleted
42+
*/
43+
remove: function(qName) {
44+
if (!qName || qName.length === 0) {
45+
throw new Error('You must specify a queue name.');
46+
}
47+
return client.keysAsync('bull:' + qName + ':*').then(function(keys) {
48+
if (keys.length) {
49+
return client.del(keys);
50+
}
51+
});
52+
},
53+
54+
/**
55+
* Get a queue by name
56+
* @param {string} qName Queue name
57+
* @returns {Object} An instance of the queue
58+
*/
59+
get: function(qName) {
60+
if (this._q.name !== qName) {
61+
this._q.name = qName;
62+
this._q.instance = new bull(qName);
63+
}
64+
return this._q.instance;
65+
}
66+
67+
};

lib/redis.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var Promise = require('bluebird');
2+
var redis = require('redis');
3+
4+
var client = redis.createClient();
5+
6+
module.exports = Promise.promisifyAll(client);

lib/toureiro.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var express = require('express');

package.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "toureiro",
3+
"version": "0.1.0",
4+
"description": "A graphic interface for bull.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "mocha tests/test_*"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/evanhuang8/Toureiro.git"
12+
},
13+
"keywords": [
14+
"bull",
15+
"distributed",
16+
"queue",
17+
"ui"
18+
],
19+
"author": "Evan Huang <[email protected]>",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/evanhuang8/Toureiro/issues"
23+
},
24+
"homepage": "https://github.com/evanhuang8/Toureiro#readme",
25+
"dependencies": {
26+
"bluebird": "2.10.x",
27+
"bull": "0.6.x",
28+
"express": "4.13.x",
29+
"lodash": "3.10.x",
30+
"redis": "2.1.x"
31+
},
32+
"devDependencies": {
33+
"chai": "3.3.x",
34+
"mocha": "2.3.x",
35+
"node-uuid": "1.4.x"
36+
}
37+
}

0 commit comments

Comments
 (0)