Python – a single call to "Source DB" to get the updates and copy it to few "DB" in parallel

correctly I have a code- that take watching to any update on "Source DB" and copy it to "Destination DB"

I need make it that with One call from the "Source DB" it will copy parallels to few "Destination DB"

stream = None
while True:
    try:

        collection_in = source_db.get_collection(collection_name)
        collection_out = destination_db.get_collection(collection_name)

        with collection_in.watch(full_document='updateLookup') as stream:

            for change in stream:
                oper_type = change['operationType']

                logger.debug(f"{oper_type} received: {collection_name}")

                if oper_type in ignored_ops:
                    logger.debug(f"{oper_type} operation ignored")
                    continue

                if oper_type == "insert":
                    callback = db_insert_callback

                elif oper_type == "replace":
                    # db_update_callback(change)
                    callback = db_update_callback

                elif oper_type == "delete":
                    callback = db_remove_callback

                callback(change, collection_out=collection_out)


    except Exception as ex:
        logger.exception(ex)

    finally:
        if stream:
            stream.close()

and example to one of callbaks

def db_insert_callback(insert_change, collection_out):
    doc = insert_change['fullDocument']
    # logger.info(insert_change)
    try:
        collection_out.insert(doc)
    except Exception as ex:
        logger.exception(ex)


Read more here: https://stackoverflow.com/questions/65704789/python-a-single-call-to-source-db-to-get-the-updates-and-copy-it-to-few-db

Content Attribution

This content was originally published by Sportac at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: