Skip to content

Commit

Permalink
refactor: Update watch.py to use a while loop for continuous event wa…
Browse files Browse the repository at this point in the history
…tching and handling
  • Loading branch information
luthfiarifin committed Oct 1, 2024
1 parent 5759cde commit 636774c
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions app/events/watch.py
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()

0 comments on commit 636774c

Please sign in to comment.