@@ -1252,32 +1252,31 @@ def execute_rtm_loop(self, slackAPIToken):
1252
1252
1253
1253
while sc == None or not sc .server .connected :
1254
1254
try :
1255
+ ##Roll over the counter to keep delay calculations under control
1256
+ if connection_attempt > 100 :
1257
+ connection_attempt = 0
1258
+
1255
1259
self ._logger .debug (
1256
1260
"Attempting to connect Slack RTM API (iteration="
1257
1261
+ str (connection_attempt )
1258
1262
+ ")"
1259
1263
)
1260
1264
1261
- if sc == None :
1262
- wait_delay = 0
1263
- else :
1264
- wait_delay = self .get_rtm_reconnect_delay (
1265
- connection_attempt
1266
- )
1265
+ wait_delay = self .get_rtm_reconnect_delay (connection_attempt )
1267
1266
1268
1267
if wait_delay > 0 :
1269
1268
self ._logger .debug (
1270
1269
"Sleeping for "
1271
1270
+ str (wait_delay )
1272
1271
+ " seconds before attempting connection"
1273
1272
)
1274
- time .sleep (wait )
1273
+ time .sleep (wait_delay )
1275
1274
1276
1275
##Slack's client doesn't expose the underlying websocket/socket
1277
1276
##so we unfortunately need to rely on Python's GC to handle
1278
1277
##the socket disconnect
1279
1278
sc = SlackClient (slackAPIToken )
1280
- if sc .rtm_connect (with_team_state = False , auto_reconnect = True ):
1279
+ if sc .rtm_connect (with_team_state = False ):
1281
1280
self ._logger .debug (
1282
1281
"Successfully reconnected via Slack RTM API"
1283
1282
)
@@ -1288,8 +1287,7 @@ def execute_rtm_loop(self, slackAPIToken):
1288
1287
connection_attempt += 1
1289
1288
except Exception as e :
1290
1289
self ._logger .error (
1291
- "Error Slack RTM API connection error (Exception): "
1292
- + str (e )
1290
+ "Slack RTM API connection error (Exception): " + str (e )
1293
1291
)
1294
1292
connection_attempt += 1
1295
1293
@@ -1326,13 +1324,22 @@ def execute_rtm_loop(self, slackAPIToken):
1326
1324
)
1327
1325
1328
1326
def get_rtm_reconnect_delay (self , iteration ):
1329
- max_delay = 300 ##5 minutes
1327
+ max_delay = 1800 ##30 minutes
1330
1328
1331
- delay = math .pow (2 , iteration ) * 5
1332
- if delay <= 0 or delay > max_delay :
1333
- return max_delay
1329
+ try :
1330
+ delay = (2 ** iteration ) * 5
1331
+ if delay <= 0 or delay > max_delay :
1332
+ return max_delay
1334
1333
1335
- return delay
1334
+ return delay
1335
+ except Exception as e :
1336
+ self ._logger .exception (
1337
+ "Slack RTM reconnect delay calculation error (iteration="
1338
+ + str (iteration )
1339
+ + "), Error: "
1340
+ + str (e .message )
1341
+ )
1342
+ return max_delay
1336
1343
1337
1344
def process_rtm_message (self , slackAPIToken , message ):
1338
1345
if not self ._settings .get (["slack_apitoken_config" ], merged = True ).get (
0 commit comments