diff --git a/lib/Slackbot_worker.js b/lib/Slackbot_worker.js index abc2f5a98..678dc8bdb 100755 --- a/lib/Slackbot_worker.js +++ b/lib/Slackbot_worker.js @@ -18,6 +18,9 @@ module.exports = function(botkit, config) { } }; + // Set when destroy() is called - prevents a reconnect from completing + // if it was fired off prior to destroy being called + var destroyed = false; var pingIntervalId = null; var retryBackoff = null; @@ -96,7 +99,7 @@ module.exports = function(botkit, config) { botkit.log.notice('** BOT ID:', bot.identity.name, '...reconnect attempt #' + back.settings.attempt + ' of ' + options.retries + ' being made after ' + back.settings.timeout + 'ms'); bot.startRTM(function(err) { - if (err) { + if (err && !destroyed) { return reconnect(err); } retryBackoff = null; @@ -108,6 +111,9 @@ module.exports = function(botkit, config) { * Shutdown and cleanup the spawned worker */ bot.destroy = function() { + // this prevents a startRTM from completing if it was fired off + // prior to destroy being called + destroyed = true; if (retryBackoff) { retryBackoff.close(); retryBackoff = null; @@ -133,6 +139,12 @@ module.exports = function(botkit, config) { bot.identity = res.self; bot.team_info = res.team; + // Bail out if destroy() was called + if (destroyed) { + botkit.log.notice('Ignoring rtm.start response, bot was destroyed'); + return cb('Ignoring rtm.start response, bot was destroyed'); + } + /** * Also available: * res.users, res.channels, res.groups, res.ims,