mirror of
https://github.com/jlu5/PyLink.git
synced 2024-11-01 01:09:22 +01:00
coreplugin: modularize rehash, and add a SIGHUP handler for it
Closes #179.
This commit is contained in:
parent
8f32727a3f
commit
264c803d8e
@ -5,6 +5,7 @@ coreplugin.py - Implements core PyLink functions as a plugin.
|
||||
import gc
|
||||
import sys
|
||||
import signal
|
||||
import os
|
||||
|
||||
import utils
|
||||
import conf
|
||||
@ -334,6 +335,51 @@ def reload(irc, source, args):
|
||||
if unload(irc, source, args):
|
||||
load(irc, source, args)
|
||||
|
||||
def _rehash():
|
||||
"""Rehashes the PyLink daemon."""
|
||||
old_conf = conf.conf.copy()
|
||||
fname = conf.fname
|
||||
new_conf = conf.loadConf(fname, errors_fatal=False)
|
||||
new_conf = conf.validateConf(new_conf)
|
||||
conf.conf = new_conf
|
||||
for network, ircobj in world.networkobjects.copy().items():
|
||||
# Server was removed from the config file, disconnect them.
|
||||
log.debug('rehash: checking if %r is in new conf still.', network)
|
||||
if network not in new_conf['servers']:
|
||||
log.debug('rehash: removing connection to %r (removed from config).', network)
|
||||
# Disable autoconnect first.
|
||||
ircobj.serverdata['autoconnect'] = -1
|
||||
ircobj.disconnect()
|
||||
del world.networkobjects[network]
|
||||
else:
|
||||
ircobj.conf = new_conf
|
||||
ircobj.serverdata = new_conf['servers'][network]
|
||||
ircobj.botdata = new_conf['bot']
|
||||
|
||||
# Clear the IRC object's channel loggers and replace them with
|
||||
# new ones by re-running logSetup().
|
||||
while ircobj.loghandlers:
|
||||
log.removeHandler(ircobj.loghandlers.pop())
|
||||
|
||||
ircobj.logSetup()
|
||||
|
||||
# TODO: update file loggers here too.
|
||||
|
||||
for network, sdata in new_conf['servers'].items():
|
||||
# New server was added. Connect them if not already connected.
|
||||
if network not in world.networkobjects:
|
||||
proto = utils.getProtocolModule(sdata['protocol'])
|
||||
world.networkobjects[network] = classes.Irc(network, proto, new_conf)
|
||||
|
||||
if os.name == 'posix':
|
||||
# Only register SIGHUP on *nix.
|
||||
def sighup_handler(_signo, _stack_frame):
|
||||
"""Handles SIGHUP by rehashing the PyLink daemon."""
|
||||
log.info("SIGHUP received, reloading config.")
|
||||
_rehash()
|
||||
|
||||
signal.signal(signal.SIGHUP, sighup_handler)
|
||||
|
||||
@utils.add_cmd
|
||||
def rehash(irc, source, args):
|
||||
"""takes no arguments.
|
||||
@ -341,45 +387,13 @@ def rehash(irc, source, args):
|
||||
Reloads the configuration file for PyLink, (dis)connecting added/removed networks.
|
||||
Plugins must be manually reloaded."""
|
||||
utils.checkAuthenticated(irc, source, allowOper=False)
|
||||
old_conf = conf.conf.copy()
|
||||
fname = conf.fname
|
||||
try:
|
||||
new_conf = conf.loadConf(fname, errors_fatal=False)
|
||||
_rehash()
|
||||
except Exception as e: # Something went wrong, abort.
|
||||
log.exception("Error REHASH'ing config: ")
|
||||
log.exception("Error REHASHing config: ")
|
||||
irc.reply("Error loading configuration file: %s: %s" % (type(e).__name__, e))
|
||||
return
|
||||
else:
|
||||
new_conf = conf.validateConf(new_conf)
|
||||
conf.conf = new_conf
|
||||
for network, ircobj in world.networkobjects.copy().items():
|
||||
# Server was removed from the config file, disconnect them.
|
||||
log.debug('(%s) rehash: checking if %r is in new conf still.', irc.name, network)
|
||||
if network not in new_conf['servers']:
|
||||
log.debug('(%s) rehash: removing connection to %r (removed from config).', irc.name, network)
|
||||
# Disable autoconnect first.
|
||||
ircobj.serverdata['autoconnect'] = -1
|
||||
ircobj.disconnect()
|
||||
del world.networkobjects[network]
|
||||
else:
|
||||
ircobj.conf = new_conf
|
||||
ircobj.serverdata = new_conf['servers'][network]
|
||||
ircobj.botdata = new_conf['bot']
|
||||
|
||||
# Clear the IRC object's channel loggers and replace them with
|
||||
# new ones by re-running logSetup().
|
||||
while ircobj.loghandlers:
|
||||
log.removeHandler(ircobj.loghandlers.pop())
|
||||
|
||||
ircobj.logSetup()
|
||||
|
||||
# TODO: update file loggers here too.
|
||||
|
||||
for network, sdata in new_conf['servers'].items():
|
||||
# New server was added. Connect them if not already connected.
|
||||
if network not in world.networkobjects:
|
||||
proto = utils.getProtocolModule(sdata['protocol'])
|
||||
world.networkobjects[network] = classes.Irc(network, proto, new_conf)
|
||||
irc.reply("Done.")
|
||||
|
||||
def main(irc=None):
|
||||
|
Loading…
Reference in New Issue
Block a user