#NEWLINE# @ defer . inlineCallbacks #NEWLINE# def do_background_update( self , desired_duration_ms ) : #NEWLINE# #INDENT# 'Does some amount of work on a background update\n        Args:\n            desired_duration_ms(float): How long we want to spend\n                updating.\n        Returns:\n            A deferred that completes once some amount of work is done.\n            The deferred will have a value of None if there is currently\n            no more work to do.\n        ' #NEWLINE# if ( not self . _background_update_queue ) : #NEWLINE# #INDENT# updates = ( yield self . _simple_select_list ( 'background_updates' , keyvalues = None , retcols = ( 'update_name' , ) ) ) #NEWLINE# for update in updates : #NEWLINE# #INDENT# self . _background_update_queue . append ( update [ 'update_name' ] ) #NEWLINE# #UNINDENT# #UNINDENT# if ( not self . _background_update_queue ) : #NEWLINE# #INDENT# defer . returnValue ( None ) #NEWLINE# #UNINDENT# update_name = self . _background_update_queue . pop ( 0 ) #NEWLINE# self . _background_update_queue . append ( update_name ) #NEWLINE# update_handler = self . _background_update_handlers [ update_name ] #NEWLINE# performance = self . _background_update_performance . get ( update_name ) #NEWLINE# if ( performance is None ) : #NEWLINE# #INDENT# performance = BackgroundUpdatePerformance ( update_name ) #NEWLINE# self . _background_update_performance [ update_name ] = performance #NEWLINE# #UNINDENT# items_per_ms = performance . average_items_per_ms ( ) #NEWLINE# if ( items_per_ms is not None ) : #NEWLINE# #INDENT# batch_size = int ( ( desired_duration_ms * items_per_ms ) ) #NEWLINE# batch_size = max ( batch_size , self . MINIMUM_BACKGROUND_BATCH_SIZE ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# batch_size = self . DEFAULT_BACKGROUND_BATCH_SIZE #NEWLINE# #UNINDENT# progress_json = ( yield self . _simple_select_one_onecol ( 'background_updates' , keyvalues = { #NEWLINE# #INDENT# 'update_name' : update_name , #NEWLINE# #UNINDENT# } , retcol = 'progress_json' ) ) #NEWLINE# progress = json . loads ( progress_json ) #NEWLINE# time_start = self . _clock . time_msec ( ) #NEWLINE# items_updated = ( yield update_handler ( progress , batch_size ) ) #NEWLINE# time_stop = self . _clock . time_msec ( ) #NEWLINE# duration_ms = ( time_stop - time_start ) #NEWLINE# logger . info ( 'Updating %r. Updated %r items in %rms. (total_rate=%r/ms, current_rate=%r/ms, total_updated=%r)' , update_name , items_updated , duration_ms , performance . total_items_per_ms ( ) , performance . average_items_per_ms ( ) , performance . total_item_count ) #NEWLINE# performance . update ( items_updated , duration_ms ) #NEWLINE# defer . returnValue ( len ( self . _background_update_performance ) )