-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (46 loc) · 1.41 KB
/
index.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
const Queue = require( 'bull' );
const reddit = require( './indexers/Reddit' );
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)
},
}
);
redditQueue.on( 'error', ( queueError ) => {
console.error( queueError );
} );
redditQueue.on( 'failed', ( job, jobError ) => {
// If the API returns duplicate, don't keep it around
if(jobError.message.includes('returned 409')){
console.log(`Removed job ${job.id} as the content is a duplicate`);
job.remove();
return true;
}
console.error( jobError );
} );
redditQueue.process( ( job ) => {
console.log( `Running job ${ job.id } for ${ job.data.game }` );
if ( !job.data.accountId ) {
return job.discard();
}
return reddit.parsePost( job.data.accountId, job.data.post )
.then( ( post ) => {
if ( !post ) {
console.log( `Discarding job ${ job.id } because we didn't get a post` );
job.discard();
return false;
}
return post.save( job.data.game );
} )
.catch( ( someError ) => {
console.log( someError );
throw someError;
} );
} );