def stop_kendra_sync_job_when_all_done()

in lambda/indexer/common.py [0:0]


def stop_kendra_sync_job_when_all_done(dsId, indexId):
    logger.info(f"stop_kendra_sync_job_when_all_done(dsId={dsId}, indexId={indexId})")
    response = TABLE.scan(
                Select="COUNT",
                FilterExpression=Attr('sync_state').eq('RUNNING')
            )
    logger.info("DynamoDB scan result: " + json.dumps(response))
    if (response['Count'] == 0):
        #All DONE
        logger.info("No media files currently being transcribed. Stop Data Source Sync.")
        logger.info(f"KENDRA.stop_data_source_sync_job(Id={dsId}, IndexId={indexId})")
        KENDRA.stop_data_source_sync_job(Id=dsId, IndexId=indexId)
        i = 0
        while True: 
            logger.info(f"waiting 5sec for sync job to stop")
            time.sleep(5)
            kendra_sync_running = is_kendra_sync_running(dsId, indexId)
            if not kendra_sync_running: 
                logger.info(f"Data Source Sync is stopped.")
                break
            if kendra_sync_running == "SYNCING_INDEXING":
                logger.info(f"Data Source Sync is in SYNCING_INDEXING state.. it will stop automatically - unable to force stop.")
                break
            if i >= 10:
                logger.info(f"Data Source Sync is in state {kendra_sync_running}. Timed out waiting for it to stop.")
                break
            i += 1
    else:
        logger.info(f"Can't stop Data Source since Transcribe jobs are still running - count: {response['Count']}")
    return True