2016-06-21 20:25:47 +02:00
|
|
|
"""
|
|
|
|
control.py - Implements SHUTDOWN and REHASH functionality.
|
|
|
|
"""
|
|
|
|
import signal
|
|
|
|
import os
|
2016-12-17 04:42:12 +01:00
|
|
|
import threading
|
2017-02-18 21:19:42 +01:00
|
|
|
import sys
|
2017-05-13 04:02:25 +02:00
|
|
|
import atexit
|
2016-06-21 20:25:47 +02:00
|
|
|
|
2017-08-08 02:06:56 +02:00
|
|
|
from pylinkirc import world, utils, conf # Do not import classes, it'll import loop
|
2017-06-02 17:42:32 +02:00
|
|
|
from pylinkirc.log import log, makeFileLogger, stopFileLoggers, getConsoleLogLevel
|
2016-08-25 21:07:55 +02:00
|
|
|
from . import permissions
|
2016-06-21 20:25:47 +02:00
|
|
|
|
2016-07-02 05:08:38 +02:00
|
|
|
def remove_network(ircobj):
|
|
|
|
"""Removes a network object from the pool."""
|
|
|
|
# Disable autoconnect first by setting the delay negative.
|
|
|
|
ircobj.serverdata['autoconnect'] = -1
|
|
|
|
ircobj.disconnect()
|
|
|
|
del world.networkobjects[ircobj.name]
|
|
|
|
|
2017-05-07 22:39:46 +02:00
|
|
|
def _print_remaining_threads():
|
2017-07-13 06:50:20 +02:00
|
|
|
log.debug('shutdown(): Remaining threads: %s', ['%s/%s' % (t.name, t.ident) for t in threading.enumerate()])
|
2017-05-07 22:39:46 +02:00
|
|
|
|
2017-05-13 04:02:25 +02:00
|
|
|
def _remove_pid():
|
2017-07-13 07:17:00 +02:00
|
|
|
pidfile = "%s.pid" % conf.confname
|
2017-07-20 15:13:01 +02:00
|
|
|
if world._should_remove_pid:
|
|
|
|
# Remove our pid file.
|
|
|
|
log.info("Removing PID file %r.", pidfile)
|
|
|
|
try:
|
|
|
|
os.remove(pidfile)
|
|
|
|
except OSError:
|
|
|
|
log.exception("Failed to remove PID file %r, ignoring..." % pidfile)
|
|
|
|
else:
|
|
|
|
log.debug('Not removing PID file %s as world._should_remove_pid is False.' % pidfile)
|
2017-02-18 21:19:42 +01:00
|
|
|
|
2017-05-13 04:02:25 +02:00
|
|
|
def _kill_plugins(irc=None):
|
2017-08-16 21:17:08 +02:00
|
|
|
if not world.plugins:
|
|
|
|
# No plugins were loaded or we were in a pre-initialized state, ignore.
|
|
|
|
return
|
|
|
|
|
2017-05-13 04:02:25 +02:00
|
|
|
log.info("Shutting down plugins.")
|
2016-06-21 20:25:47 +02:00
|
|
|
for name, plugin in world.plugins.items():
|
|
|
|
# Before closing connections, tell all plugins to shutdown cleanly first.
|
|
|
|
if hasattr(plugin, 'die'):
|
2016-07-02 06:08:50 +02:00
|
|
|
log.debug('coremods.control: Running die() on plugin %s due to shutdown.', name)
|
2016-06-21 20:25:47 +02:00
|
|
|
try:
|
2017-07-13 06:42:50 +02:00
|
|
|
plugin.die(irc=irc)
|
2016-06-21 20:25:47 +02:00
|
|
|
except: # But don't allow it to crash the server.
|
2016-07-02 06:08:50 +02:00
|
|
|
log.exception('coremods.control: Error occurred in die() of plugin %s, skipping...', name)
|
2016-06-21 20:25:47 +02:00
|
|
|
|
2017-05-13 04:02:25 +02:00
|
|
|
# We use atexit to register certain functions so that when PyLink cleans up after itself if it
|
|
|
|
# shuts down because all networks have been disconnected.
|
|
|
|
atexit.register(_remove_pid)
|
|
|
|
atexit.register(_kill_plugins)
|
|
|
|
|
2017-07-13 06:50:20 +02:00
|
|
|
def shutdown(irc=None):
|
2017-05-13 04:02:25 +02:00
|
|
|
"""Shuts down the Pylink daemon."""
|
2017-08-31 22:36:46 +02:00
|
|
|
if world.shutting_down.is_set(): # We froze on shutdown last time, so immediately abort.
|
2017-05-13 04:02:25 +02:00
|
|
|
_print_remaining_threads()
|
|
|
|
raise KeyboardInterrupt("Forcing shutdown.")
|
|
|
|
|
2017-08-31 22:36:46 +02:00
|
|
|
world.shutting_down.set()
|
2017-05-13 04:02:25 +02:00
|
|
|
|
|
|
|
# HACK: run the _kill_plugins trigger with the current IRC object. XXX: We should really consider removing this
|
|
|
|
# argument, since no plugins actually use it to do anything.
|
|
|
|
atexit.unregister(_kill_plugins)
|
2017-07-13 06:42:50 +02:00
|
|
|
_kill_plugins(irc=irc)
|
2017-05-13 04:02:25 +02:00
|
|
|
|
2016-07-27 01:26:01 +02:00
|
|
|
# Remove our main PyLink bot as well.
|
|
|
|
utils.unregisterService('pylink')
|
|
|
|
|
2016-07-02 07:20:24 +02:00
|
|
|
for ircobj in world.networkobjects.copy().values():
|
2016-07-02 05:08:38 +02:00
|
|
|
# Disconnect all our networks.
|
|
|
|
remove_network(ircobj)
|
2016-06-21 20:25:47 +02:00
|
|
|
|
2016-12-17 04:42:12 +01:00
|
|
|
log.info("Waiting for remaining threads to stop; this may take a few seconds. If PyLink freezes "
|
|
|
|
"at this stage, press Ctrl-C to force a shutdown.")
|
2017-05-07 22:39:46 +02:00
|
|
|
_print_remaining_threads()
|
2016-12-17 04:42:12 +01:00
|
|
|
|
2016-10-15 22:50:25 +02:00
|
|
|
# Done.
|
|
|
|
|
2017-07-13 06:51:28 +02:00
|
|
|
def _sigterm_handler(signo, stack_frame):
|
2016-07-27 01:16:23 +02:00
|
|
|
"""Handles SIGTERM and SIGINT gracefully by shutting down the PyLink daemon."""
|
|
|
|
log.info("Shutting down on signal %s." % signo)
|
2017-07-13 06:50:20 +02:00
|
|
|
shutdown()
|
2016-06-21 20:25:47 +02:00
|
|
|
|
2017-07-13 06:51:28 +02:00
|
|
|
signal.signal(signal.SIGTERM, _sigterm_handler)
|
|
|
|
signal.signal(signal.SIGINT, _sigterm_handler)
|
2016-06-21 20:25:47 +02:00
|
|
|
|
2017-07-13 06:50:20 +02:00
|
|
|
def rehash():
|
2016-06-21 20:25:47 +02:00
|
|
|
"""Rehashes the PyLink daemon."""
|
2017-06-02 17:46:27 +02:00
|
|
|
log.info('Reloading PyLink configuration...')
|
2016-06-21 20:25:47 +02:00
|
|
|
old_conf = conf.conf.copy()
|
|
|
|
fname = conf.fname
|
2016-12-06 07:43:01 +01:00
|
|
|
new_conf = conf.loadConf(fname, errors_fatal=False, logger=log)
|
2016-06-21 20:25:47 +02:00
|
|
|
conf.conf = new_conf
|
2016-08-18 06:39:46 +02:00
|
|
|
|
|
|
|
# Reset any file logger options.
|
|
|
|
stopFileLoggers()
|
|
|
|
files = new_conf['logging'].get('files')
|
|
|
|
if files:
|
|
|
|
for filename, config in files.items():
|
|
|
|
makeFileLogger(filename, config.get('loglevel'))
|
|
|
|
|
2017-06-02 17:42:32 +02:00
|
|
|
log.debug('rehash: updating console log level')
|
|
|
|
world.console_handler.setLevel(getConsoleLogLevel())
|
2017-01-07 09:10:47 +01:00
|
|
|
|
2016-06-21 20:25:47 +02:00
|
|
|
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)
|
2016-07-02 05:08:38 +02:00
|
|
|
remove_network(ircobj)
|
2016-06-21 20:25:47 +02:00
|
|
|
else:
|
2017-03-11 09:21:30 +01:00
|
|
|
# XXX: we should really just add abstraction to Irc to update config settings...
|
2016-06-21 20:25:47 +02:00
|
|
|
ircobj.conf = new_conf
|
|
|
|
ircobj.serverdata = new_conf['servers'][network]
|
|
|
|
ircobj.botdata = new_conf['bot']
|
2017-03-11 10:05:07 +01:00
|
|
|
ircobj.autoconnect_active_multiplier = 1
|
2016-06-21 20:25:47 +02:00
|
|
|
|
|
|
|
# Clear the IRC object's channel loggers and replace them with
|
2017-07-12 16:50:34 +02:00
|
|
|
# new ones by re-running log_setup().
|
2016-06-21 20:25:47 +02:00
|
|
|
while ircobj.loghandlers:
|
|
|
|
log.removeHandler(ircobj.loghandlers.pop())
|
|
|
|
|
2017-07-12 16:50:34 +02:00
|
|
|
ircobj.log_setup()
|
2016-06-21 20:25:47 +02:00
|
|
|
|
2017-03-09 07:30:32 +01:00
|
|
|
utils.resetModuleDirs()
|
|
|
|
|
2016-06-21 20:25:47 +02:00
|
|
|
for network, sdata in new_conf['servers'].items():
|
2016-07-02 05:33:00 +02:00
|
|
|
# Connect any new networks or disconnected networks if they aren't already.
|
2017-07-13 07:56:30 +02:00
|
|
|
if (network not in world.networkobjects) or (not world.networkobjects[network]._connection_thread.is_alive()):
|
2016-06-21 20:25:47 +02:00
|
|
|
proto = utils.getProtocolModule(sdata['protocol'])
|
2017-06-30 09:41:10 +02:00
|
|
|
|
|
|
|
# API note: 2.0.x style of starting network connections
|
|
|
|
world.networkobjects[network] = newirc = proto.Class(network)
|
|
|
|
newirc.connect()
|
|
|
|
|
2017-06-02 17:46:27 +02:00
|
|
|
log.info('Finished reloading PyLink configuration.')
|
2016-06-21 20:25:47 +02:00
|
|
|
|
|
|
|
if os.name == 'posix':
|
2018-01-21 22:35:21 +01:00
|
|
|
# Only register SIGHUP/SIGUSR1 on *nix.
|
|
|
|
def _sighup_handler(signo, _stack_frame):
|
|
|
|
"""Handles SIGHUP/SIGUSR1 by rehashing the PyLink daemon."""
|
|
|
|
log.info("Signal %s received, reloading config." % signo)
|
2017-07-13 06:50:20 +02:00
|
|
|
rehash()
|
2016-06-21 20:25:47 +02:00
|
|
|
|
2018-01-21 22:35:21 +01:00
|
|
|
signal.signal(signal.SIGHUP, _sighup_handler)
|
|
|
|
signal.signal(signal.SIGUSR1, _sighup_handler)
|