Comparing version
@@ -639,3 +639,56 @@ /*eslint-env node */ | ||
/*@function startCleaner | ||
* | ||
* Cleans jobs from a queue. Similar to remove but keeps jobs within a certian | ||
* grace period. | ||
* | ||
* @param {int} grace - The grace period | ||
* @param {string} [type=completed] - The type of job to clean. Possible values | ||
* are completed, waiting, active, delayed, failed. Defaults to completed. | ||
*/ | ||
Queue.prototype.clean = function (grace, type) { | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
var getter; | ||
if(grace === undefined || grace === null) { | ||
return reject(new Error('You must define a grace period.')); | ||
} | ||
if(!type) { | ||
type = 'completed'; | ||
} | ||
//a super hack to deal with different job types | ||
getter = 'get' + type.charAt(0).toUpperCase() + type.slice(1); | ||
if(getter !== 'getCompleted' && | ||
getter !== 'getWaiting' && | ||
getter !== 'getActive' && | ||
getter !== 'getDelayed' && | ||
getter !== 'getFailed') { | ||
return reject(new Error('Cannot clean unkown queue type')); | ||
} | ||
_this[getter]().then(function (jobs) { | ||
//take all jobs outside of the grace period | ||
return Promise.filter(jobs, function (job) { | ||
return job.timestamp < Date.now() - grace; | ||
}); | ||
}).then(function (jobs) { | ||
//remove those old jobs | ||
return Promise.each(jobs, function (job) { | ||
return job.remove(); | ||
}); | ||
}).then(function (jobs) { | ||
//let everyone know we cleaned up | ||
_this.emit('cleaned', jobs, type); | ||
resolve(jobs); | ||
}).catch(function (err) { | ||
_this.emit('error', err); | ||
reject(err); | ||
}); | ||
}); | ||
}; | ||
// | ||
@@ -642,0 +695,0 @@ // Private local functions |
{ | ||
"name": "bull", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Job manager", | ||
@@ -20,3 +20,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"bluebird": "^2.9.27", | ||
"bluebird": "^2.9.30", | ||
"lodash": "^3.9.3", | ||
@@ -23,0 +23,0 @@ "node-uuid": "^1.4.3", |
@@ -128,2 +128,7 @@ Bull Job Manager | ||
}) | ||
.on('cleaned', function(jobs, type) { | ||
//jobs is an array of cleaned jobs | ||
//type is the type of job cleaned | ||
//see clean for details | ||
}); | ||
``` | ||
@@ -241,2 +246,3 @@ | ||
* [Queue##empty](#empty) | ||
* [Queue##clean](#clean) | ||
* [Queue##close](#close) | ||
@@ -412,3 +418,45 @@ * [Job](#job) | ||
<a name="clean"/> | ||
#### Queue##clean(options) | ||
Tells the queue remove all jobs created outside of a grace period. | ||
You can clean the jobs with the following states: completed, waiting, active, | ||
delayed, and failed. | ||
__Example__ | ||
```javascript | ||
//cleans all jobs that completed over 5 seconds ago. | ||
queue.clean(5000); | ||
//clean all jobs that failed over 10 seconds ago. | ||
queue.clean(10000, 'failed'); | ||
queue.on('cleaned', function (job, type) { | ||
console.log('Cleaned %s %s jobs', job.length, type); | ||
}); | ||
``` | ||
__Arguments__ | ||
```javascript | ||
grace {int} Grace period in milliseconds. | ||
type {string} type of job to clean. Values are completed, waiting, active, | ||
delayed, and failed. Defaults to completed. | ||
returns {Promise} A promise that resolves with an array of removed jobs. | ||
``` | ||
__Events__ | ||
The cleaner emits the `cleaned` event anytime the queue is cleaned. | ||
```javascript | ||
queue.on('cleaned', function (jobs, type) {}); | ||
jobs {Array} An array of jobs that have been cleaned. | ||
type {String} The type of job cleaned. Options are completed, waiting, active, | ||
delayed, or failed. | ||
``` | ||
--------------------------------------- | ||
<a name="priorityQueue"/> | ||
@@ -415,0 +463,0 @@ ###PriorityQueue(queueName, redisPort, redisHost, [redisOpts]) |
@@ -1036,2 +1036,81 @@ /*eslint-env node */ | ||
}); | ||
describe('Cleaner', function () { | ||
beforeEach(function () { | ||
queue = buildQueue('cleaner'); | ||
}); | ||
it('should reject the cleaner with no grace', function(done){ | ||
queue.clean().then(function () { | ||
done(new Error('Promise should not resolve')); | ||
}, function (err) { | ||
expect(err).to.be.a(Error); | ||
done(); | ||
}); | ||
}); | ||
it('should reject the cleaner an unknown type', function (done) { | ||
queue.clean(0, 'bad').then(function () { | ||
done(new Error('Promise should not resolve')); | ||
}, function (e) { | ||
expect(e).to.be.a(Error); | ||
done(); | ||
}); | ||
}); | ||
it('should clean an empty queue', function (done) { | ||
queue.clean(0); | ||
queue.on('error', function (err) { | ||
done(err); | ||
}); | ||
queue.on('cleaned', function (jobs, type) { | ||
expect(type).to.be('completed'); | ||
expect(jobs.length).to.be(0); | ||
done(); | ||
}); | ||
}); | ||
it('should clean two jobs from the queue', function (done) { | ||
queue.add({some: 'data'}); | ||
queue.add({some: 'data'}); | ||
queue.process(function (job, jobDone) { | ||
jobDone(); | ||
}); | ||
Promise.delay(100).then(function () { | ||
return queue.clean(0); | ||
}).then(function (jobs) { | ||
expect(jobs.length).to.be(2); | ||
done(); | ||
}, function (err) { | ||
done(err); | ||
}); | ||
}); | ||
it('should only remove a job outside of the grace period', function (done) { | ||
queue.process(function (job, jobDone) { | ||
jobDone(); | ||
}); | ||
queue.add({some: 'data'}); | ||
queue.add({some: 'data'}); | ||
Promise.delay(200).then(function () { | ||
queue.add({some: 'data'}); | ||
queue.clean(100); | ||
}).delay(100).then(function () { | ||
return queue.getCompleted(); | ||
}).then(function (jobs) { | ||
expect(jobs.length).to.be(1); | ||
return queue.empty(); | ||
}).then(function () { | ||
done(); | ||
}); | ||
}); | ||
it('should clean all failed jobs', function (done) { | ||
queue.add({some: 'data'}); | ||
queue.add({some: 'data'}); | ||
queue.process(function (job, jobDone) { | ||
jobDone(new Error('It failed')); | ||
}); | ||
Promise.delay(100).then(function () { | ||
return queue.clean(0, 'failed'); | ||
}).then(function (jobs) { | ||
expect(jobs.length).to.be(2); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
115166
4.75%2999
4.39%535
9.86%Updated