#NEWLINE# def pop( self , queue , grabfor = None , ordered = True ) : #NEWLINE# #INDENT# c = self . get_queue_collection ( queue ) #NEWLINE# now = datetime . datetime . utcnow ( ) #NEWLINE# result = None #NEWLINE# if grabfor : #NEWLINE# #INDENT# extra = [ ( 'update' , { #NEWLINE# #INDENT# '$set' : { #NEWLINE# #INDENT# 'scheduled_time' : ( now + datetime . timedelta ( seconds = grabfor ) ) , #NEWLINE# #UNINDENT# } , #NEWLINE# #UNINDENT# } ) ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# extra = [ ( 'remove' , True ) ] #NEWLINE# #UNINDENT# query = dict ( scheduled_time = { #NEWLINE# #INDENT# '$lte' : now , #NEWLINE# #UNINDENT# } , retries = { #NEWLINE# #INDENT# '$gt' : 0 , #NEWLINE# #UNINDENT# } ) #NEWLINE# if ( not ordered ) : #NEWLINE# #INDENT# capture_token = self . _random_token ( ) #NEWLINE# directions = ( ( '$gte' , pymongo . ASCENDING ) , ( '$lt' , pymongo . DESCENDING ) ) #NEWLINE# for ( operator , order ) in directions : #NEWLINE# #INDENT# query [ 'random_token' ] = { #NEWLINE# #INDENT# operator : capture_token , #NEWLINE# #UNINDENT# } #NEWLINE# try : #NEWLINE# #INDENT# result = self . mongodb . command ( pymongo . son . SON ( ( [ ( 'findandmodify' , c . name ) , ( 'query' , query ) , ( 'sort' , dict ( random_token = order ) ) ] + extra ) ) ) #NEWLINE# #UNINDENT# except pymongo . errors . OperationFailure : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# result = self . mongodb . command ( pymongo . son . SON ( ( [ ( 'findandmodify' , c . name ) , ( 'query' , query ) , ( 'sort' , dict ( scheduled_time = pymongo . ASCENDING ) ) ] + extra ) ) ) #NEWLINE# #UNINDENT# except pymongo . errors . OperationFailure : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# #UNINDENT# if ( not result ) : #NEWLINE# #INDENT# return None #NEWLINE# #UNINDENT# result = result [ 'value' ] #NEWLINE# result [ '_id' ] = str ( result [ '_id' ] ) #NEWLINE# return result