Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #434 from selfcontained/destroy-reconnecting
Browse files Browse the repository at this point in the history
Ensure no reconnect happens after destroy()
  • Loading branch information
anonrig authored Oct 12, 2016
2 parents 936211f + d4a50e9 commit b4f69f4
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/Slackbot_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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,
Expand Down

0 comments on commit b4f69f4

Please sign in to comment.