Fix and improve reload process

This commit is contained in:
nemunaire 2015-05-24 16:47:22 +02:00
parent c0fed51fde
commit 8983b9b67c
4 changed files with 23 additions and 9 deletions

View File

@ -87,6 +87,9 @@ def reload():
import imp
import nemubot.bot
imp.reload(nemubot.bot)
import nemubot.channel
imp.reload(nemubot.channel)

View File

@ -139,7 +139,8 @@ def main():
# Register the hook for futur import
from nemubot.importer import ModuleFinder
sys.meta_path.append(ModuleFinder(context.modules_paths, context.add_module))
module_finder = ModuleFinder(context.modules_paths, context.add_module)
sys.meta_path.append(module_finder)
# Load requested configuration files
for path in args.files:
@ -162,6 +163,9 @@ def main():
def sighuphandler(signum, frame):
"""On SIGHUP, perform a deep reload"""
import imp
nonlocal nemubot, context, module_finder
logger.debug("SIGHUP receive, iniate reload procedure...")
# Reload nemubot Python modules
imp.reload(nemubot)
@ -171,6 +175,11 @@ def main():
import nemubot.bot
context = nemubot.bot.hotswap(context)
# Reload ModuleFinder
sys.meta_path.remove(module_finder)
module_finder = ModuleFinder(context.modules_paths, context.add_module)
sys.meta_path.append(module_finder)
# Reload configuration file
for path in args.files:
if os.path.isfile(path):
@ -186,9 +195,6 @@ def main():
"".join(traceback.format_stack(stack)))
signal.signal(signal.SIGUSR1, sigusr1handler)
# Here we go!
context.start()
if args.socketfile:
from nemubot.server.socket import SocketListener
context.add_server(SocketListener(context.add_server, "master_socket",
@ -198,6 +204,7 @@ def main():
oldcontext = None
while oldcontext != context:
oldcontext = context
context.start()
context.join()
# Wait for consumers

View File

@ -145,6 +145,7 @@ class Bot(threading.Thread):
from select import select
from nemubot.server import _lock, _rlist, _wlist, _xlist
logger.info("Starting main loop")
self.stop = False
while not self.stop:
with _lock:
@ -211,6 +212,7 @@ class Bot(threading.Thread):
self.load_file(path)
logger.info("Configurations successfully loaded")
self.sync_queue.task_done()
logger.info("Ending main loop")
@ -568,14 +570,16 @@ def hotswap(bak):
bak.stop = True
if bak.event_timer is not None:
bak.event_timer.cancel()
# Unload modules
for mod in [k for k in bak.modules.keys()]:
bak.unload_module(mod)
# Save datastore
bak.datastore.close()
new = Bot(str(bak.ip), bak.modules_paths, bak.datastore)
new.servers = bak.servers
new.modules = bak.modules
new.modules_configuration = bak.modules_configuration
new.events = bak.events
new.hooks = bak.hooks
new._update_event_timer()
return new

View File

@ -113,7 +113,7 @@ class AbstractServer(io.IOBase):
"""
self._sending_queue.put(self.format(message))
self.logger.debug("Message '%s' appended to Queue", message)
self.logger.debug("Message '%s' appended to write queue", message)
if self not in _wlist:
_wlist.append(self)