-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (60 loc) · 2.07 KB
/
Copy pathindex.js
File metadata and controls
77 lines (60 loc) · 2.07 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
require( 'dotenv' ).config();
const Queue = require( 'bull' );
const indexers = require( './indexers' );
if ( !process.env.REDIS_URL ) {
throw new Error( 'Got no queue, exiting' );
}
const postsQueue = new Queue(
'posts',
process.env.REDIS_URL,
{
limiter: {
max: 1,
duration: 10000,
},
}
);
postsQueue.on( 'error', ( queueError ) => {
console.error( queueError );
} );
postsQueue.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 );
} );
// Same parse-and-save flow for every service; the per-service difference lives
// in the indexer's parsePost. Each job name needs its own registration because
// the single 'posts' consumer fails any name it has no handler for.
const processPost = function processPost ( job ) {
console.log( `Running ${ job.name } job ${ job.id } for ${ job.data.game }` );
if ( !indexers[ job.name ] ) {
console.error( `No indexer specified for ${ job.name }` );
return Promise.reject();
}
if ( !job.data.accountId ) {
return job.discard();
}
const postIndexer = new indexers[ job.name ]( job.data.post.indexerConfig );
return postIndexer.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;
} );
};
postsQueue.process( 'reddit', processPost );
postsQueue.process( 'rss', processPost );
postsQueue.process( 'bluesky', processPost );
postsQueue.process( 'strapi', processPost );
postsQueue.process( 'discourse', processPost );