Modules can now be reloaded

This commit is contained in:
moneromooo 2015-01-01 10:06:09 +00:00
parent 800e1d754c
commit a59e883626
3 changed files with 49 additions and 5 deletions

View File

@ -230,6 +230,25 @@ def SendToNick(nick,chan,msg):
def IsRegistered(nick,chan,cmd):
RunRegisteredCommand(nick,chan,SendToNick,"You are registered",SendToNick,"You are not registered")
def Reload(nick,chan,cmd):
sendto=GetSendTo(nick,chan)
modulename=GetParam(cmd,1)
if not modulename:
SendTo(sendto,"Usage: reload <modulename>")
return
if modulename=="builtin":
SendTo(sendto,"Cannot reload builtin module")
return
log_info('Unloading %s module' % modulename)
UnregisterCommands(modulename)
log_info('Reloading %s module' % modulename)
try:
reload(sys.modules[modulename])
SendTo(sendto,'%s reloaded' % modulename)
except Exception,e:
log_error('Failed to load module "%s": %s' % (modulename, str(e)))
SendTo(sendto,'An error occured')
def OnIdle():
RunIdleFunctions([irc,redisdb])
@ -249,6 +268,7 @@ def RegisterCommands():
RegisterCommand({'module': 'builtin', 'name': 'scanwho', 'function': ScanWho, 'admin': True, 'help': "Refresh users list in a channel"})
RegisterCommand({'module': 'builtin', 'name': 'dump_users', 'function': DumpUsers, 'admin': True, 'help': "Dump users table to log"})
RegisterCommand({'module': 'builtin', 'name': 'show_activity', 'function': ShowActivity, 'admin': True, 'help': "Show time since a user was last active"})
RegisterCommand({'module': 'builtin', 'name': 'reload', 'function': Reload, 'admin': True, 'help': "Reload a module"})
def OnCommandProxy(cmd,chan,who):
OnCommand(cmd,chan,who,RunAdminCommand,RunRegisteredCommand)

View File

@ -16,6 +16,7 @@ from tipbot.irc import *
commands = dict()
calltable=dict()
idles = []
cleanup = dict()
def RunRegisteredCommand(nick,chan,ifyes,yesdata,ifno,nodata):
if nick not in calltable:
@ -91,8 +92,11 @@ def Commands(nick,chan,cmd):
def RegisterCommand(command):
commands[command['name']] = command
def RegisterIdleFunction(function):
idles.append(function)
def RegisterIdleFunction(module,function):
idles.append((module,function))
def RegisterCleanupFunction(module,function):
cleanup.append((module,function))
def OnCommand(cmd,chan,who,check_admin,check_registered):
if cmd[0] in commands:
@ -109,8 +113,28 @@ def OnCommand(cmd,chan,who,check_admin,check_registered):
def RunIdleFunctions(param):
for f in idles:
try:
f(param)
f[1](param)
except Exception,e:
log_error("Exception running idle function %s: %s" % (str(f),str(e)))
log_error("Exception running idle function %s from module %s: %s" % (str(f[1]),str(f[2]),str(e)))
def UnregisterCommands(module):
global commands
global idles
if module in cleanup:
cleanup[module]()
del cleanup[module]
new_idles = []
for f in idles:
if f[0] != module:
new_idles.append(f)
idles = new_idles
new_commands = dict()
for cmd in commands:
c = commands[cmd]
if c['module'] != module:
new_commands[cmd] = c
commands = new_commands

View File

@ -89,5 +89,5 @@ def UpdateCoin(param):
log_error('UpdateCoin: Failed to get bulk payments: %s' % str(e))
last_wallet_update_time = time.time()
RegisterIdleFunction(UpdateCoin)
RegisterIdleFunction("payment",UpdateCoin)