2023-12-28 15:50:23 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2024-01-24 19:11:39 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
TODO
|
|
|
|
|
|
|
|
1) Check interval, and if no connection is establish after 30 seconds
|
|
|
|
then disconnect and reconnect again.
|
|
|
|
|
|
|
|
2) or Check ping, and if no ping is received after 30 seconds then
|
|
|
|
disconnect and try to reconnect again.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2024-01-02 12:42:41 +01:00
|
|
|
from slixfeed.config import get_value
|
2024-01-18 21:57:49 +01:00
|
|
|
from slixfeed.dt import current_time
|
2023-12-28 15:50:23 +01:00
|
|
|
from time import sleep
|
2024-01-02 12:42:41 +01:00
|
|
|
import logging
|
2023-12-28 15:50:23 +01:00
|
|
|
|
|
|
|
|
2024-01-02 12:42:41 +01:00
|
|
|
async def recover_connection(self, event, message):
|
|
|
|
logging.warning(message)
|
|
|
|
print(current_time(), message, "Attempting to reconnect.")
|
2023-12-28 15:50:23 +01:00
|
|
|
self.connection_attempts += 1
|
|
|
|
# if self.connection_attempts <= self.max_connection_attempts:
|
|
|
|
# self.reconnect(wait=5.0) # wait a bit before attempting to reconnect
|
|
|
|
# else:
|
|
|
|
# print(current_time(),"Maximum connection attempts exceeded.")
|
|
|
|
# logging.error("Maximum connection attempts exceeded.")
|
|
|
|
print(current_time(), "Attempt number", self.connection_attempts)
|
2024-01-04 02:16:24 +01:00
|
|
|
seconds = (get_value(
|
2024-01-24 19:11:39 +01:00
|
|
|
"accounts", "XMPP", "reconnect_timeout")) or 30
|
2024-01-03 11:37:33 +01:00
|
|
|
seconds = int(seconds)
|
2023-12-28 15:50:23 +01:00
|
|
|
print(current_time(), "Next attempt within", seconds, "seconds")
|
|
|
|
# NOTE asyncio.sleep doesn't interval as expected
|
|
|
|
# await asyncio.sleep(seconds)
|
|
|
|
sleep(seconds)
|
|
|
|
self.reconnect(wait=5.0)
|
|
|
|
|
|
|
|
|
|
|
|
async def inspect_connection(self, event):
|
|
|
|
print("Disconnected\nReconnecting...")
|
|
|
|
print(event)
|
|
|
|
try:
|
|
|
|
self.reconnect
|
|
|
|
except:
|
|
|
|
self.disconnect()
|
|
|
|
print("Problem reconnecting")
|