From d7fdeed19d31c16fc9f505bcc8984705a51e6e7c Mon Sep 17 00:00:00 2001 From: James Lu Date: Tue, 29 Dec 2015 11:13:50 -0800 Subject: [PATCH] relay: remove use of sched in exportDB We were using sched and threading together? How inefficient... --- plugins/relay.py | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/plugins/relay.py b/plugins/relay.py index 9e50d34..464ff99 100644 --- a/plugins/relay.py +++ b/plugins/relay.py @@ -2,8 +2,9 @@ import sys import os sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + import pickle -import sched +import time import threading import string from collections import defaultdict @@ -22,9 +23,6 @@ spawnlocks_servers = defaultdict(threading.RLock) savecache = ExpiringDict(max_len=5, max_age_seconds=10) killcache = ExpiringDict(max_len=5, max_age_seconds=10) -exportdb_scheduler = None -exportdb_event = None - dbname = utils.getDatabaseName('pylinkrelay') ### INTERNAL FUNCTIONS @@ -47,15 +45,11 @@ def main(irc=None): loadDB() - global exportdb_scheduler, exportdb_event - exportdb_scheduler = sched.scheduler() - exportdb_event = exportdb_scheduler.enter(30, 1, exportDB, argument=(True,)) - # Thread this because exportDB() queues itself as part of its # execution, in order to get a repeating loop. - thread = threading.Thread(target=exportdb_scheduler.run) - thread.daemon = True - thread.start() + exportdb_thread = threading.Thread(target=exportDB, args=(True,), name="PyLink Relay exportDB Loop") + exportdb_thread.daemon = True + exportdb_thread.start() if irc is not None: for ircobj in world.networkobjects.values(): @@ -76,10 +70,6 @@ def die(sourceirc): exportDB(reschedule=False) - # Stop all scheduled DB exports - global exportdb_scheduler, exportdb_event - exportdb_scheduler.cancel(exportdb_event) - def normalizeNick(irc, netname, nick, separator=None, uid=''): """Creates a normalized nickname for the given nick suitable for introduction to a remote network (as a relay client).""" @@ -153,12 +143,20 @@ def loadDB(): def exportDB(reschedule=False): """Exports the relay database, optionally creating a loop to do this automatically.""" - global exportdb_scheduler, exportdb_event - if reschedule and exportdb_scheduler: - exportdb_event = exportdb_scheduler.enter(30, 1, exportDB, argument=(True,)) - log.debug("Relay: exporting links database to %s", dbname) - with open(dbname, 'wb') as f: - pickle.dump(db, f, protocol=4) + + def dump(): + log.debug("Relay: exporting links database to %s", dbname) + with open(dbname, 'wb') as f: + pickle.dump(db, f, protocol=4) + + if reschedule: + while True: + # Sleep for 30 seconds between DB exports. Seems sort of + # arbitrary, but whatever. + time.sleep(30) + dump() + else: # Rescheduling was disabled; just dump the DB once. + dump() def getPrefixModes(irc, remoteirc, channel, user, mlist=None): """