Skip to content

Commit

Permalink
Add reconnection request handler.
Browse files Browse the repository at this point in the history
When a *.net *.split occurs, the bot isn't aware. This handler allows to
periodically send a SIGUSR1 to the bot's PID to force a reconnection.
The process is seamless is the server is still connected.
  • Loading branch information
matael committed Apr 17, 2017
1 parent 82904fb commit 74f4a9e
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion hms_irc/ircbot.py
@@ -1,8 +1,9 @@
import importlib
import logging
import signal

import irc.bot
from irc.client import NickMask
from irc.client import NickMask, ServerConnectionError

from hms_irc.irc import IRCCommand

Expand All @@ -21,6 +22,9 @@ def __init__(self, channel, nickname, server, port=6667):
self.join_callback = None
self.rabbit = None

# try to reconnect whenever a SIGUSR1 is received
signal.signal(signal.SIGUSR1, self.handle_reconnection_request)

def on_welcome(self, serv, ev):
"""Method called when we are connected to the IRC server."""
self.serv = serv
Expand Down Expand Up @@ -107,3 +111,19 @@ def handle_rabbit_msg(self, client, topic, dct):

except (ImportError, AttributeError) as e:
get_logger().error(e)

def handle_reconnection_request(self, signum, frame):
"""Method forcing a reconnection attempt when the bot's PID receives a particular
signal. Useful to handle *.net *.split. Silent if still connected."""

if self.serv:
try:
get_logger().error('Reconnection request received, processing..')
self.serv.connect()
except ServerConnectionError as e:
get_logger().error(e)
else:
get_logger().error('Reconnection request received but no serv is currently set. Ignoring.')



0 comments on commit 74f4a9e

Please sign in to comment.