-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #547 from humphd/feedparser-fixes
Parallelize feed parser, make it run forever
- Loading branch information
Showing
7 changed files
with
135 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* A processor function to be run concurrently, in its own process, and | ||
* with potentially multiple simultaneous instances, by the feed queue. | ||
* https://github.com/OptimalBits/bull#separate-processes | ||
*/ | ||
|
||
const { parse } = require('feedparser-promised'); | ||
|
||
const { logger } = require('../utils/logger'); | ||
const Post = require('../post'); | ||
|
||
module.exports = async function processor(job) { | ||
const { url } = job.data; | ||
const httpOptions = { | ||
url, | ||
// ms to wait for a connection to be assumed to have failed | ||
timeout: 20 * 1000, | ||
gzip: true, | ||
}; | ||
let articles; | ||
|
||
try { | ||
articles = await parse(httpOptions); | ||
} catch (err) { | ||
logger.error({ err }, `Unable to process feed ${url}`); | ||
throw err; | ||
} | ||
|
||
// Transform the list of articles to a list of Post objects | ||
return articles.map(article => Post.fromArticle(article)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,50 @@ | ||
const { parse } = require('feedparser-promised'); | ||
const { cpus } = require('os'); | ||
const path = require('path'); | ||
|
||
const feedQueue = require('./queue'); | ||
const { logger } = require('../utils/logger'); | ||
const Post = require('../post'); | ||
|
||
exports.workerCallback = async function(job) { | ||
const { url } = job.data; | ||
let articles; | ||
/** | ||
* We determine the number of parallel feed processor functions to run | ||
* based on the value of the environment variable FEED_QUEUE_PARALLEL_WORKERS. | ||
* Possible values are: | ||
* | ||
* *: use the number of available CPUs | ||
* <a Number>: use the given number, up to the number of available CPUs | ||
* <not set>: use 1 by default | ||
*/ | ||
function getFeedWorkersCount() { | ||
const { FEED_QUEUE_PARALLEL_WORKERS } = process.env; | ||
const cpuCount = cpus().length; | ||
|
||
try { | ||
articles = await parse(url); | ||
} catch (err) { | ||
logger.error({ err }, `Unable to process feed ${url}`); | ||
throw err; | ||
if (FEED_QUEUE_PARALLEL_WORKERS === '*') { | ||
return cpuCount; | ||
} | ||
|
||
return articles.map(article => Post.fromArticle(article)); | ||
}; | ||
const count = Number(FEED_QUEUE_PARALLEL_WORKERS); | ||
if (typeof count === 'number') { | ||
return Math.min(count, cpuCount); | ||
} | ||
|
||
exports.start = async function() { | ||
// Start processing jobs from the feed queue... | ||
feedQueue.process(exports.workerCallback); | ||
return 1; | ||
} | ||
|
||
exports.start = function() { | ||
const concurrency = getFeedWorkersCount(); | ||
logger.info(`Starting ${concurrency} instance${concurrency > 1 ? 's' : ''} of feed processor.`); | ||
feedQueue.process(concurrency, path.resolve(__dirname, 'processor.js')); | ||
|
||
// When posts are returned from the queue, save them to the database | ||
feedQueue.on('completed', async (job, posts) => { | ||
try { | ||
await Promise.all(posts.map(post => post.save())); | ||
// The posts we get back will be Objects, and we need to convert | ||
// to a full Post, then save to Redis. | ||
await Promise.all(posts.map(post => Post.parse(post).save())); | ||
} catch (err) { | ||
logger.error({ err }, 'Error inserting posts into database'); | ||
} | ||
}); | ||
|
||
return feedQueue; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,44 @@ | ||
const fixtures = require('./fixtures'); | ||
const feedWorker = require('../src/backend/feed/worker'); | ||
const processor = require('../src/backend/feed/processor'); | ||
|
||
test('Passing a valid Atom feed URI should pass', async () => { | ||
const feedURL = fixtures.getAtomUri(); | ||
fixtures.nockValidAtomResponse(); | ||
const job = fixtures.createMockJobObjectFromURL(feedURL); | ||
await expect(feedWorker.workerCallback(job)).resolves.toBeTruthy(); | ||
await expect(processor(job)).resolves.toBeTruthy(); | ||
}); | ||
|
||
test('Passing a valid RSS feed URI should pass', async () => { | ||
const feedURL = fixtures.getRssUri(); | ||
fixtures.nockValidRssResponse(); | ||
const job = fixtures.createMockJobObjectFromURL(feedURL); | ||
await expect(feedWorker.workerCallback(job)).resolves.toBeTruthy(); | ||
await expect(processor(job)).resolves.toBeTruthy(); | ||
}); | ||
|
||
test('Passing a valid URI, but not a feed URI should error', async () => { | ||
const url = fixtures.getHtmlUri(); | ||
fixtures.nockValidHtmlResponse(); | ||
const job = fixtures.createMockJobObjectFromURL(url); | ||
await expect(feedWorker.workerCallback(job)).rejects.toThrow(); | ||
await expect(processor(job)).rejects.toThrow(); | ||
}); | ||
|
||
test('Passing an invalid RSS category feed should pass', async () => { | ||
const feedURL = fixtures.getRssUri(); | ||
fixtures.nockInvalidRssResponse(); | ||
const job = fixtures.createMockJobObjectFromURL(feedURL); | ||
await expect(feedWorker.workerCallback(job)).resolves.toBeTruthy(); | ||
await expect(processor(job)).resolves.toBeTruthy(); | ||
}); | ||
|
||
test('Passing a valid RSS category feed should pass', async () => { | ||
const feedURL = fixtures.getRssUri(); | ||
fixtures.nockValidRssResponse(); | ||
const job = fixtures.createMockJobObjectFromURL(feedURL); | ||
await expect(feedWorker.workerCallback(job)).resolves.toBeTruthy(); | ||
await expect(processor(job)).resolves.toBeTruthy(); | ||
}); | ||
|
||
test('Non existent feed failure case: 404 should error', async () => { | ||
const url = fixtures.getHtmlUri(); | ||
fixtures.nock404Response(); | ||
const job = fixtures.createMockJobObjectFromURL(url); | ||
await expect(feedWorker.workerCallback(job)).rejects.toThrow(); | ||
await expect(processor(job)).rejects.toThrow(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters