-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Update watch.py to use a while loop for continuous event wa…
…tching and handling
- Loading branch information
1 parent
5759cde
commit 636774c
Showing
1 changed file
with
20 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,24 @@ | ||
from bson.json_util import dumps | ||
|
||
from crud import insert_event_vector, update_event_vector, eventCollections | ||
|
||
|
||
def watch_events(): | ||
while True: | ||
try: | ||
change_stream = eventCollections.watch() | ||
for change in change_stream: | ||
if change["operationType"] == "insert": | ||
insert_event_vector(change["fullDocument"]) | ||
elif change["operationType"] == "update": | ||
print(change["documentKey"]["_id"]) | ||
update_event_vector( | ||
change["documentKey"]["_id"], | ||
change["updateDescription"]["updatedFields"], | ||
) | ||
print(dumps(change)) | ||
except Exception as e: | ||
print(f"Error occurred: {e}") | ||
|
||
|
||
if __name__ == "__main__": | ||
change_stream = eventCollections.watch() | ||
for change in change_stream: | ||
if change["operationType"] == "insert": | ||
insert_event_vector(change["fullDocument"]) | ||
elif change["operationType"] == "update": | ||
print(change["documentKey"]["_id"]) | ||
update_event_vector( | ||
change["documentKey"]["_id"], | ||
change["updateDescription"]["updatedFields"], | ||
) | ||
print(dumps(change)) | ||
watch_events() |