-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #104: Manage, throttle comments with Redis
- Loading branch information
Showing
9 changed files
with
112 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,6 @@ language: node_js | |
|
||
node_js: | ||
- "0.12" | ||
|
||
services: | ||
- redis-server |
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,2 @@ | ||
web: node bin/www.js | ||
worker: node worker.js |
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
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,32 @@ | ||
/** | ||
* Return an instance of the Kue queue singleton. | ||
* | ||
* This file is only needed to work around bug #681 of Kue: | ||
* https://github.com/Automattic/kue/issues/681 | ||
* | ||
* Once the bug is fixed, we'll be able to get the queue with much less code, | ||
* making this file unnecessary: | ||
* | ||
* var kue = require('kue'); | ||
* var redisQueue = kue.createQueue({ | ||
* redis: process.env.REDIS_URL | ||
* }); | ||
*/ | ||
|
||
var kue = require('kue'); | ||
var url = require('url'); | ||
|
||
var parsedRedisURL = url.parse(process.env.REDIS_URL); | ||
|
||
var config = { | ||
redis: { | ||
port: parseInt(parsedRedisURL.port), | ||
host: parsedRedisURL.hostname | ||
} | ||
}; | ||
|
||
if (parsedRedisURL.auth) { | ||
config.redis.auth = parsedRedisURL.auth.replace(/.*?:/, ''); | ||
} | ||
|
||
module.exports = kue.createQueue(config); |
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,24 @@ | ||
'use strict'; | ||
|
||
var commenter = require('./commenter'); | ||
var redisQueue = require('./redisQueue'); | ||
|
||
var lastCommentTimestamp = 0; | ||
|
||
// Process comment tasks | ||
// Pause for COMMENT_WAIT milleseconds between comment submissions | ||
redisQueue.process('comment', function(job, done) { | ||
var now = Date.now(); | ||
var waitRemaining = 0; | ||
|
||
var nextCommentTimestamp = lastCommentTimestamp + parseInt(process.env.COMMENT_WAIT); | ||
|
||
if (nextCommentTimestamp > now) { | ||
waitRemaining = nextCommentTimestamp - now; | ||
} | ||
|
||
setTimeout(function() { | ||
commenter.postPullRequestComment(job.data.commentURL, job.data.comment, job.data.filename, job.data.sha, job.data.line, done); | ||
lastCommentTimestamp = Date.now(); // Use a new, fresh value of Date.now() | ||
}, waitRemaining); | ||
}); |