3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-01 09:19:23 +01:00

relay: add 'save' command, make rescheduling optional in exportDB()

This commit is contained in:
James Lu 2015-07-19 14:56:12 -07:00
parent 688675d484
commit c00da49477

View File

@ -70,14 +70,26 @@ def loadDB():
", creating a new one in memory...", dbname) ", creating a new one in memory...", dbname)
db = {} db = {}
def exportDB(): def exportDB(reschedule=False):
scheduler = utils.schedulers.get('relaydb') scheduler = utils.schedulers.get('relaydb')
if scheduler: if reschedule and scheduler:
scheduler.enter(30, 1, exportDB) scheduler.enter(30, 1, exportDB, argument=(True,))
log.debug("Relay: exporting links database to %s", dbname) log.debug("Relay: exporting links database to %s", dbname)
with open(dbname, 'wb') as f: with open(dbname, 'wb') as f:
pickle.dump(db, f, protocol=4) pickle.dump(db, f, protocol=4)
@utils.add_cmd
def save(irc, source, args):
"""takes no arguments.
Saves the relay database to disk."""
if irc.users[source].identified:
exportDB()
utils.msg(irc, source, 'Done.')
else:
utils.msg(irc, source, 'Error: you are not authenticated!')
return
def getPrefixModes(irc, remoteirc, channel, user): def getPrefixModes(irc, remoteirc, channel, user):
modes = '' modes = ''
for pmode in ('owner', 'admin', 'op', 'halfop', 'voice'): for pmode in ('owner', 'admin', 'op', 'halfop', 'voice'):
@ -699,7 +711,7 @@ def initializeAll(irc):
def main(): def main():
loadDB() loadDB()
utils.schedulers['relaydb'] = scheduler = sched.scheduler() utils.schedulers['relaydb'] = scheduler = sched.scheduler()
scheduler.enter(30, 1, exportDB) scheduler.enter(30, 1, exportDB, argument=(True,))
# Thread this because exportDB() queues itself as part of its # Thread this because exportDB() queues itself as part of its
# execution, in order to get a repeating loop. # execution, in order to get a repeating loop.
thread = threading.Thread(target=scheduler.run) thread = threading.Thread(target=scheduler.run)