Skip to content

Commit

Permalink
Merge pull request #267 from SockDrawer/integration
Browse files Browse the repository at this point in the history
Reduce resource usage of autoreader
  • Loading branch information
RaceProUK committed Dec 3, 2015
2 parents 2197932 + 0558e14 commit fdf290b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ node_js:
- iojs
- '0.11'
- '0.12'
- iojs-v1.7
- iojs-v1.8
- iojs-v2.5
- iojs-v3.0
- '4.0'
- '4.1'
before_script:
- rm -rf ./coverage
- git config credential.helper "store --file=.git/credentials"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sockbot",
"version": "2.11.5",
"version": "2.11.6",
"releaseName": "Cheery Chiffon",
"description": "A sockpuppet bot to use on http://what.thedailywtf.com.",
"repository": "https://github.com/SockDrawer/SockBot",
Expand Down
15 changes: 11 additions & 4 deletions plugins/autoreader.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,31 @@ exports.stop = function () {
* reading any unread posts it finds that are older than the configured interval.
*/
exports.readify = function () {
const minAge = new Date().getTime() - internals.config.minAge;
const additional = Math.max(internals.config.minAge / 2, 25 * 60 * 60 * 1000);
const readCutoff = minAge - additional;
internals.browser.getTopics((topic, nextTopic) => {
if (!topic) {
return;
return nextTopic();
}
const lastPost = Date.parse(topic.last_posted_at);
if (lastPost < readCutoff) {
return nextTopic();
}

internals.events.emit('logMessage', 'Reading topic `' + topic.slug + '`');
const now = new Date().getTime() - internals.config.minAge;
const postNumbers = [];
internals.browser.getPosts(topic.id, (post, nextPost) => {
if (post && !post.read && Date.parse(post.created_at) < now) {
if (post && !post.read && Date.parse(post.created_at) < minAge) {
postNumbers.push(post.post_number);
}
nextPost();
}, () => {
if (postNumbers.length > 0) {
internals.browser.readPosts(topic.id, postNumbers, () => 0);
}
nextTopic();
});
nextTopic();
}, () => 0);
};

Expand Down
23 changes: 19 additions & 4 deletions test/plugins/autoreaderTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ describe('autoreader', () => {
});
describe('stop()', () => {
it('should stop timer', () => {
autoreader.internals.timer = {clear: () => 0};
autoreader.internals.timer = {
clear: () => 0
};
autoreader.stop();
expect(autoreader.internals.timer).to.be.undefined;
});
Expand All @@ -103,11 +105,13 @@ describe('autoreader', () => {
sandbox.restore();
});
it('should not read anything', () => {
const spy = sandbox.stub(browser, 'getTopics');
spy.callsArgWith(0, undefined);
const spy = sandbox.stub(browser, 'getTopics'),
spy2 = sinon.spy();
spy.callsArgWith(0, undefined, spy2);
autoreader.prepare(undefined, dummyCfg, events, browser);
autoreader.readify();
events.emit.calledWith('logMessage').should.be.false;
spy2.called.should.be.true;
});
it('should read the topic', () => {
const topicSpy = sandbox.stub(browser, 'getTopics');
Expand All @@ -118,7 +122,18 @@ describe('autoreader', () => {
sandbox.stub(browser, 'getPosts');
autoreader.prepare(undefined, dummyCfg, events, browser);
autoreader.readify();
events.emit.calledWith('logMessage', 'Reading topic `Test`').should.be.true;
});
it('should not read the topic when no new posts could have been made', () => {
const topicSpy = sandbox.stub(browser, 'getTopics');
topicSpy.callsArgWith(0, {
id: 1,
slug: 'Test',
'last_posted_at': new Date(Date.now() - 5 * 25 * 60 * 60 * 1000).toISOString()
}, () => 0);
sandbox.stub(browser, 'getPosts');
autoreader.prepare(undefined, dummyCfg, events, browser);
autoreader.readify();
events.emit.calledWith('logMessage', 'Reading topic `Test`').should.be.false;
});
/*eslint-disable camelcase */
it('should read the unread post', () => {
Expand Down

0 comments on commit fdf290b

Please sign in to comment.