I'm using kue for node.js and I see there is sample code for removing jobs on complete, but is there a way I can remove stale jobs older than X? I'd like to see completed jobs for a day or two so that I can review what's been happening, but for it to clean up anything older than that.
我正在使用kue for node.js,我看到有完整的删除作业的示例代码,但是有没有办法可以删除早于X的陈旧作业?我希望看到一两天完成的工作,以便我可以查看发生了什么,但是要清理那些比这更旧的东西。
4 个解决方案
#1
10
The kue API appears to have improved considerably since this question was first asked. I dug into the code a bit and this much simpler version worked for me:
自从首次提出这个问题以来,kue API似乎已经有了很大的改进。我挖了一下代码,这个更简单的版本对我有用:
var kue = require('kue');
kue.Job.rangeByState('complete', 0, someLargeNumber, 1, function(err, jobs) {
jobs.forEach(function(job) {
if (job.created_at < yourTimeThreshold) return;
job.remove();
});
});
(Error handling is omitted for brevity's sake.)
(为简洁起见,省略了错误处理。)
#2
6
It would be nice if Kue provided a mechanism to clean-up completed jobs within the library, however you can implement a solution based on each jobs 'created_at' field. Here is what I use to remove completed jobs older than 7 days, scanning every 24 hours.
如果Kue提供了清理库中已完成作业的机制,那将是很好的,但是您可以基于每个作业'created_at'字段实现解决方案。以下是我用来删除超过7天的已完成作业,每24小时扫描一次。
var _ = require('underscore'),
kue = require('kue');
var jobs = kue.createQueue(),
q = new kue, // object so we can access exposed methods in the kue lib
hours = 24,
timer = hours * 60 * 60 * 1000; // timer for the setInterval function
var completedJobs = function(callback) {
/**
* completedJobs - uses the kue lib .complete function to get a list of
* all completed job ids, iterates through each id to retrieve the actual
* job object, then pushes the object into an array for the callback.
*
*/
q.complete(function(err, ids){
var jobs = [],
count = 0,
total = ids.length;
console.log('completedJobs -> ids.length:%s',ids.length);
_.each(ids, function(id){
kue.Job.get(id, function(err, job){
count++;
jobs.push(job);
if (total === count) {
callback(null, jobs);
return;
}
});
});
});
}
var removeJobs = function(jobs, callback) {
/**
* removeJobs - removes the job from kue by calling the job.remove from the
* job object collected in completedJobs().
*
*/
var count = 0,
total = jobs.length;
console.log('removeJobs -> jobs.length:%s',jobs.length);
_.each(jobs, function(job) {
job.remove(function(err) {
count++;
if (total === count) {
callback(null, count);
return;
}
});
});
}
var dateDiffInDays = function(d1, d2) {
/**
* dateDiffInDays - returns the difference between two Date objects in days
*/
var t2 = d2.getTime(),
t1 = d1.getTime();
return parseInt((t2-t1)/(24*3600*1000));
}
setInterval(function() {
/**
* setInterval - calls completedJobs in a 24-hour interval
*/
completedJobs(function(err, jobs) {
// callback to completedJobs
console.log('completedJobs -> callback-> jobs.length:%s', jobs.length);
var jobsToRemove = [],
now = new Date();
_.each(jobs, function(job){
var then = new Date(parseInt(job.created_at)),
diff = dateDiffInDays(then, now),
timePastForRemoval = 7; // remove anything older than 7 days
if (diff >= timePastForRemoval) {
jobsToRemove.push(job);
}
});
console.log('completedJobs -> callback -> jobsToRemove.length:%s', jobsToRemove.length);
if (jobsToRemove.length > 0) { // if we have jobsToRemove
removeJobs(jobsToRemove, function(err, count){
// callback to removeJobs
console.log('removeJobs -> callback -> jobs removed:%s',count);
});
} else {
console.log('completedJobs -> callback -> no jobs to remove');
}
});
}, timer);
console.log('Running kue completed job clean-up');
#3
4
I had to do this and found the code above a bit hard to follow, I made a easy to extend clear and concise method here: https://gist.github.com/4212207
我不得不这样做,发现上面的代码有点难以理解,我在这里做了一个简单明了的方法:https://gist.github.com/4212207
#4
1
I wanted to remove stale jobs from Kue as well, and modified Chris's code to work without Underscore.js.
我也希望从Kue中删除陈旧的作业,并修改了Chris的代码,以便在没有Underscore.js的情况下工作。
The code is here: https://gist.github.com/niravmehta/6112330
代码在这里:https://gist.github.com/niravmehta/6112330
You can run it on command line as
您可以在命令行上运行它
node kue_cleanup
Thanks Chris for his wonderful script! It was a great starting point.
谢谢克里斯的精彩剧本!这是一个很好的起点。
#1
10
The kue API appears to have improved considerably since this question was first asked. I dug into the code a bit and this much simpler version worked for me:
自从首次提出这个问题以来,kue API似乎已经有了很大的改进。我挖了一下代码,这个更简单的版本对我有用:
var kue = require('kue');
kue.Job.rangeByState('complete', 0, someLargeNumber, 1, function(err, jobs) {
jobs.forEach(function(job) {
if (job.created_at < yourTimeThreshold) return;
job.remove();
});
});
(Error handling is omitted for brevity's sake.)
(为简洁起见,省略了错误处理。)
#2
6
It would be nice if Kue provided a mechanism to clean-up completed jobs within the library, however you can implement a solution based on each jobs 'created_at' field. Here is what I use to remove completed jobs older than 7 days, scanning every 24 hours.
如果Kue提供了清理库中已完成作业的机制,那将是很好的,但是您可以基于每个作业'created_at'字段实现解决方案。以下是我用来删除超过7天的已完成作业,每24小时扫描一次。
var _ = require('underscore'),
kue = require('kue');
var jobs = kue.createQueue(),
q = new kue, // object so we can access exposed methods in the kue lib
hours = 24,
timer = hours * 60 * 60 * 1000; // timer for the setInterval function
var completedJobs = function(callback) {
/**
* completedJobs - uses the kue lib .complete function to get a list of
* all completed job ids, iterates through each id to retrieve the actual
* job object, then pushes the object into an array for the callback.
*
*/
q.complete(function(err, ids){
var jobs = [],
count = 0,
total = ids.length;
console.log('completedJobs -> ids.length:%s',ids.length);
_.each(ids, function(id){
kue.Job.get(id, function(err, job){
count++;
jobs.push(job);
if (total === count) {
callback(null, jobs);
return;
}
});
});
});
}
var removeJobs = function(jobs, callback) {
/**
* removeJobs - removes the job from kue by calling the job.remove from the
* job object collected in completedJobs().
*
*/
var count = 0,
total = jobs.length;
console.log('removeJobs -> jobs.length:%s',jobs.length);
_.each(jobs, function(job) {
job.remove(function(err) {
count++;
if (total === count) {
callback(null, count);
return;
}
});
});
}
var dateDiffInDays = function(d1, d2) {
/**
* dateDiffInDays - returns the difference between two Date objects in days
*/
var t2 = d2.getTime(),
t1 = d1.getTime();
return parseInt((t2-t1)/(24*3600*1000));
}
setInterval(function() {
/**
* setInterval - calls completedJobs in a 24-hour interval
*/
completedJobs(function(err, jobs) {
// callback to completedJobs
console.log('completedJobs -> callback-> jobs.length:%s', jobs.length);
var jobsToRemove = [],
now = new Date();
_.each(jobs, function(job){
var then = new Date(parseInt(job.created_at)),
diff = dateDiffInDays(then, now),
timePastForRemoval = 7; // remove anything older than 7 days
if (diff >= timePastForRemoval) {
jobsToRemove.push(job);
}
});
console.log('completedJobs -> callback -> jobsToRemove.length:%s', jobsToRemove.length);
if (jobsToRemove.length > 0) { // if we have jobsToRemove
removeJobs(jobsToRemove, function(err, count){
// callback to removeJobs
console.log('removeJobs -> callback -> jobs removed:%s',count);
});
} else {
console.log('completedJobs -> callback -> no jobs to remove');
}
});
}, timer);
console.log('Running kue completed job clean-up');
#3
4
I had to do this and found the code above a bit hard to follow, I made a easy to extend clear and concise method here: https://gist.github.com/4212207
我不得不这样做,发现上面的代码有点难以理解,我在这里做了一个简单明了的方法:https://gist.github.com/4212207
#4
1
I wanted to remove stale jobs from Kue as well, and modified Chris's code to work without Underscore.js.
我也希望从Kue中删除陈旧的作业,并修改了Chris的代码,以便在没有Underscore.js的情况下工作。
The code is here: https://gist.github.com/niravmehta/6112330
代码在这里:https://gist.github.com/niravmehta/6112330
You can run it on command line as
您可以在命令行上运行它
node kue_cleanup
Thanks Chris for his wonderful script! It was a great starting point.
谢谢克里斯的精彩剧本!这是一个很好的起点。