-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclearer.js
More file actions
82 lines (61 loc) · 1.66 KB
/
clearer.js
File metadata and controls
82 lines (61 loc) · 1.66 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require('dotenv').config();
const Queue = require( 'bull' );
if ( !process.env.REDIS_URL ) {
throw new Error( 'Got no queue, exiting' );
}
const redditQueue = new Queue(
'reddit-posts',
process.env.REDIS_URL,
{
limiter: {
max: 1,
duration: 2000, // Might be 2 requests / post (content & parent)
},
}
);
let lastFailCount = false;
redditQueue.on( 'error', ( queueError ) => {
console.error( queueError );
} );
redditQueue.on( 'failed', ( job, jobError ) => {
console.error( jobError );
} );
const removeCollisions = async function removeCollisions(){
let failedCount = false;
try {
failedCount = await redditQueue.getFailedCount();
} catch (failedCountError){
console.error(failedCountError);
}
if(failedCount === 0){
return true;
}
if(lastFailCount === failedCount){
console.log(`No jobs removed, stopping`);
return true;
}
lastFailCount = failedCount;
console.log(failedCount);
let jobs;
try {
jobs = await redditQueue.getFailed(0, Math.min(100, failedCount));
} catch (someError){
console.log(someError);
}
for(const job of jobs){
if(!job.failedReason.includes('/posts returned 409')){
console.log(job.failedReason);
continue;
}
try {
await job.remove();
} catch (removeError){
console.error(removeError);
}
}
return await removeCollisions();
};
( async () => {
await removeCollisions();
redditQueue.close();
})();