#NEWLINE# @ defer . inlineCallbacks #NEWLINE# def run_as_leader( self ) : #NEWLINE# #INDENT# logger . info ( 'Running as leader for service [{0}]. pid: [{1}]' . format ( self . service_name , os . getpid ( ) ) ) #NEWLINE# try : #NEWLINE# #INDENT# notification_queue = ( yield self . cancellable ( self . listener . listen ( self . channels ) ) ) #NEWLINE# ( yield self . cancellable ( self . process_initial ( ) ) ) #NEWLINE# while self . running : #NEWLINE# #INDENT# event = ( yield self . cancellable ( notification_queue . get ( ) ) ) #NEWLINE# handler = self . get_handler ( event . channel ) #NEWLINE# if ( not handler ) : #NEWLINE# #INDENT# logger . warn ( 'no handler for event [{0}]' . format ( event . channel ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# payload = self . get_payload ( event ) #NEWLINE# #UNINDENT# except ( ValueError , TypeError ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# result = ( yield handler ( payload ) ) #NEWLINE# #UNINDENT# except defer . CancelledError : #NEWLINE# #INDENT# raise #NEWLINE# #UNINDENT# except Exception as e : #NEWLINE# #INDENT# logger . exception ( 'unhandled exception in run_as_leader' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# except defer . CancelledError : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# self . listener . unlisten ( notification_queue )