3
0
mirror of https://github.com/jlu5/PyLink.git synced 2025-01-11 12:42:34 +01:00

relay/world: remove need for world.schedulers

This commit is contained in:
James Lu 2015-12-26 17:06:28 -08:00
parent 270bdcc79a
commit 59c9b127a3
2 changed files with 22 additions and 13 deletions

View File

@ -21,7 +21,9 @@ spawnlocks = defaultdict(threading.RLock)
spawnlocks_servers = defaultdict(threading.RLock)
savecache = ExpiringDict(max_len=5, max_age_seconds=10)
killcache = ExpiringDict(max_len=5, max_age_seconds=10)
relay_started = True
exportdb_scheduler = None
exportdb_event = None
dbname = utils.getDatabaseName('pylinkrelay')
@ -42,24 +44,26 @@ def initializeAll(irc):
def main(irc=None):
"""Main function, called during plugin loading at start."""
global relay_started
relay_started = True
loadDB()
world.schedulers['relaydb'] = scheduler = sched.scheduler()
scheduler.enter(30, 1, exportDB, argument=(True,))
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=scheduler.run)
thread = threading.Thread(target=exportdb_scheduler.run)
thread.daemon = True
thread.start()
if irc is not None:
for ircobj in world.networkobjects.values():
initializeAll(ircobj)
def die(sourceirc):
"""Deinitialize PyLink Relay by quitting all relay clients."""
global relay_started
relay_started = False
"""Deinitialize PyLink Relay by quitting all relay clients and saving the
relay DB."""
for irc in world.networkobjects.values():
for user in irc.users.copy():
if isRelayClient(irc, user):
@ -70,6 +74,12 @@ def die(sourceirc):
relayservers.clear()
relayusers.clear()
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)."""
@ -143,9 +153,9 @@ def loadDB():
def exportDB(reschedule=False):
"""Exports the relay database, optionally creating a loop to do this
automatically."""
scheduler = world.schedulers.get('relaydb')
if reschedule and scheduler and relay_started:
scheduler.enter(30, 1, exportDB, argument=(True,))
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)

View File

@ -19,7 +19,6 @@ global commands, hooks
commands = defaultdict(list)
hooks = defaultdict(list)
networkobjects = {}
schedulers = {}
plugins = {}
whois_handlers = []
started = threading.Event()