@ -5,6 +5,9 @@ from gevent.event import AsyncResult
from gevent . queue import Queue , Full
from gevent . queue import Queue , Full
from disco . util . logging import LoggingClass
class Priority ( object ) :
class Priority ( object ) :
# BEFORE is the most dangerous priority level. Every event that flows through
# BEFORE is the most dangerous priority level. Every event that flows through
# the given emitter instance will be dispatched _sequentially_ to all BEFORE
# the given emitter instance will be dispatched _sequentially_ to all BEFORE
@ -106,7 +109,7 @@ class EmitterSubscription(object):
self . detach ( emitter )
self . detach ( emitter )
class Emitter ( object ) :
class Emitter ( LoggingClass ) :
def __init__ ( self ) :
def __init__ ( self ) :
self . event_handlers = {
self . event_handlers = {
k : defaultdict ( list ) for k in Priority . ALL
k : defaultdict ( list ) for k in Priority . ALL
@ -117,15 +120,25 @@ class Emitter(object):
for listener in self . event_handlers [ Priority . BEFORE ] . get ( name , [ ] ) :
for listener in self . event_handlers [ Priority . BEFORE ] . get ( name , [ ] ) :
try :
try :
listener ( * args , * * kwargs )
listener ( * args , * * kwargs )
except Exception :
except Exception as e :
pass
self . log . warning ( ' BEFORE {} event handler ` {} ` raised {} : {} ' . format (
name ,
listener . callback . __name__ ,
e . __class__ . __name__ ,
e ,
) )
# Next execute all AFTER handlers sequentially
# Next execute all AFTER handlers sequentially
for listener in self . event_handlers [ Priority . AFTER ] . get ( name , [ ] ) :
for listener in self . event_handlers [ Priority . AFTER ] . get ( name , [ ] ) :
try :
try :
listener ( * args , * * kwargs )
listener ( * args , * * kwargs )
except Exception :
except Exception as e :
pass
self . log . warning ( ' AFTER {} event handler ` {} ` raised {} : {} ' . format (
name ,
listener . callback . __name__ ,
e . __class__ . __name__ ,
e ,
) )
# Next enqueue all sequential handlers. This just puts stuff into a queue
# Next enqueue all sequential handlers. This just puts stuff into a queue
# without blocking, so we don't have to worry too much
# without blocking, so we don't have to worry too much