Skip to content

Commit

Permalink
fix timeout bug and prevent cross talk
Browse files Browse the repository at this point in the history
  • Loading branch information
Nell Gawor committed Sep 9, 2016
1 parent 41b2f1a commit 0ab7eca
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/messages/en.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"monitor.ttls.prompt": "How often would you like me to check for keys without Time To Live values (in hours)?",
"monitor.ttls.busy": "I'm sorry. I'm already monitoring so I can't start another monitoring job.",
"redis.not.configured": "I haven't been configured to work with Redis. The following environment variables should be set: HUBOT_IBMCLOUD_REDIS_HOST, HUBOT_IBMCLOUD_REDIS_PORT.",
"redis.deleted.number": "Deleted %s keys without TTLs",
"redis.deleted.success": "Deleted %s key[s]",
Expand Down
16 changes: 16 additions & 0 deletions src/scripts/redis.delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,38 @@ const DELETE_KEY_REGEX = /(redis\sdelete\skey)\s(.*)/i;

const DELETE_ID = 'redis.delete.nottls';
const DELETE_KEY_ID = 'redis.delete.key';

let currentResponse;

module.exports = (robot) => {

redis.on('error', function(err) {
if (currentResponse) {
let message = i18n.__('redis.error', err);
robot.emit('ibmcloud.formatter', {
response: currentResponse,
message: message
});
}
});
// Natural Language match
robot.on(DELETE_ID, (res) => {
robot.logger.debug(`${TAG}: ${DELETE_ID} - Natural Language match - res.message.text=${res.message.text}.`);
currentResponse = res;
handleDelete(res);
});

// RegEx match
robot.respond(DELETE_REGEX, {id: DELETE_ID}, function(res) {
robot.logger.debug(`${TAG}: ${DELETE_ID} - RegEx match - res.message.text=${res.message.text}.`);
currentResponse = res;
handleDelete(res);
});

// Natural Language match
robot.on(DELETE_KEY_ID, (res, parameters) => {
robot.logger.debug(`${TAG}: ${DELETE_KEY_ID} - Natural Language match - res.message.text=${res.message.text}.`);
currentResponse = res;
if (parameters && parameters.keyName) {
handleDeleteKey(res, parameters.keyName);
}
Expand All @@ -63,6 +78,7 @@ module.exports = (robot) => {
// RegEx match
robot.respond(DELETE_KEY_REGEX, {id: DELETE_KEY_ID}, function(res) {
robot.logger.debug(`${TAG}: ${DELETE_KEY_ID} - RegEx match - res.message.text=${res.message.text}.`);
currentResponse = res;
handleDeleteKey(res, res.match[2]);
});

Expand Down
25 changes: 18 additions & 7 deletions src/scripts/redis.nottls.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,23 @@ module.exports = (robot) => {
}

function processMonitorNoTtlsWrapper(res) {
// ask how long they would like the monitoring period to be
let prompt = i18n.__('monitor.ttls.prompt');
utils.getExpectedResponse(res, robot, switchBoard, prompt, /(.*)/i).then((response) => {
let selection = parseInt(response.match[1], 10);
processMonitorNoTtls(res, selection);
});
if (currentMonitor) {
// return, I'm already monitoring so I can't help you
// this is a stopgap until we can get true multi-user support in place
let warning = i18n.__('monitor.ttls.busy');
robot.emit('ibmcloud.formatter', {
response: res,
message: warning
});
}
else {
// ask how long they would like the monitoring period to be
let prompt = i18n.__('monitor.ttls.prompt');
utils.getExpectedResponse(res, robot, switchBoard, prompt, /(.*)/i).then((response) => {
let selection = parseInt(response.match[1], 10);
processMonitorNoTtls(res, selection);
});
}
}

function processMonitorNoTtls(res, frequency) {
Expand All @@ -144,7 +155,7 @@ module.exports = (robot) => {
message: message
});

currentMonitor = setInterval(processMonitorNoTtls, alertFrequency);
currentMonitor = setTimeout(processMonitorNoTtls, alertFrequency);
}
else {
// redis has not been configured
Expand Down
14 changes: 14 additions & 0 deletions src/scripts/redis.slowlog.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,30 @@ i18n.setLocale('en');
const SLOWLOG_REGEX = /redis slowlog/i;
const SLOWLOG_ID = 'redis.slowlog';

let lastResponse;

module.exports = (robot) => {

redis.on('error', function(err) {
if (lastResponse) {
let message = i18n.__('redis.error', err);
robot.emit('ibmcloud.formatter', {
response: lastResponse,
message: message
});
}
});

// Natural Language match
robot.on(SLOWLOG_ID, (res) => {
lastResponse = res;
robot.logger.debug(`${TAG}: ${SLOWLOG_ID} - Natural Language match - res.message.text=${res.message.text}.`);
processSlowLog(res);
});

// RegEx match
robot.respond(SLOWLOG_REGEX, {id: SLOWLOG_ID}, function(res) {
lastResponse = res;
robot.logger.debug(`${TAG}: ${SLOWLOG_ID} - RegEx match - res.message.text=${res.message.text}.`);
processSlowLog(res);
});
Expand Down

0 comments on commit 0ab7eca

Please sign in to comment.