#NEWLINE# def _pick_master( self , job_name , is_diff = False ) : #NEWLINE# #INDENT# '\n        Identify a master to run the given job on.\n\n        The master with the lowest queue for the given job is chosen. By random\n        sorting the first empty queue will be prioritized.\n        ' #NEWLINE# candidate_urls = self . master_urls #NEWLINE# if ( is_diff and self . diff_urls ) : #NEWLINE# #INDENT# candidate_urls = self . diff_urls #NEWLINE# #UNINDENT# blacklist = redis . smembers ( MASTER_BLACKLIST_KEY ) #NEWLINE# master_urls = [ c for c in candidate_urls if ( c not in blacklist ) ] #NEWLINE# if ( len ( master_urls ) == 0 ) : #NEWLINE# #INDENT# raise ValueError ( 'No masters to pick from.' ) #NEWLINE# #UNINDENT# if ( len ( master_urls ) == 1 ) : #NEWLINE# #INDENT# return master_urls [ 0 ] #NEWLINE# #UNINDENT# random . shuffle ( master_urls ) #NEWLINE# best_match = ( sys . maxint , None ) #NEWLINE# for url in master_urls : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# queued_jobs = self . _count_queued_jobs ( url , job_name ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# self . logger . exception ( "Couldn't count queued jobs on master %s" , url ) #NEWLINE# continue #NEWLINE# #UNINDENT# if ( queued_jobs == 0 ) : #NEWLINE# #INDENT# return url #NEWLINE# #UNINDENT# if ( best_match [ 0 ] > queued_jobs ) : #NEWLINE# #INDENT# best_match = ( queued_jobs , url ) #NEWLINE# #UNINDENT# #UNINDENT# best = best_match [ 1 ] #NEWLINE# if ( not best ) : #NEWLINE# #INDENT# raise Exception ( 'Unable to successfully pick a master from {}.' . format ( master_urls ) ) #NEWLINE# #UNINDENT# return best