from itertools import islice import time import pymysql.cursors import config def main(): db_connection = pymysql.connect( host=config.DATABASE_HOST, user=config.DATABASE_USERNAME, password=config.DATABASE_PASSWORD, database='direct_delivery', cursorclass=pymysql.cursors.DictCursor ) sql_get_assets_to_be_picked_up = 'SELECT id FROM temp_asset_received_queue_backfill WHERE processed = 0;' with db_connection: cursor = db_connection.cursor() cursor.execute(sql_get_assets_to_be_picked_up) assets_to_be_picked_up = cursor.fetchall() asset_iterator = iter(assets_to_be_picked_up) while list_of_50_assets := list(islice(asset_iterator, 50)): processed = False while not processed: sql_get_asset_received_queue_depth = f"""SELECT count(*) FROM asset_received_queue WHERE picked_up = 'N';""" cursor = db_connection.cursor() cursor.execute(sql_get_asset_received_queue_depth) db_connection.commit() asset_received_queue_depth = cursor.fetchone()['count(*)'] print('Asset received queue depth is ' + str(asset_received_queue_depth)) if asset_received_queue_depth < config.ASSET_RECEIVED_QUEUE_DEPTH_CEILING: asset_list = ','.join([str(asset['id']) for asset in list_of_50_assets]) print(f'Assets to be picked up: {asset_list}') sql_update_asset_records = f"""update asset_received_queue arq inner join temp_asset_received_queue_backfill tarq on arq.id = tarq.id set arq.picked_up = 'N', arq.priority = 3, tarq.processed = 1 where arq.id in ({str(asset_list)});""" cursor = db_connection.cursor() sql_update_asset_result = cursor.execute(sql_update_asset_records) db_connection.commit() print(f'Update asset result: changed row count is {str(sql_update_asset_result)}') processed = True else: print('Queue is too high') time.sleep(5) if __name__ == '__main__': print('Running update assets script') main()