-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhonzapda_admin.py
More file actions
35 lines (26 loc) · 1.21 KB
/
honzapda_admin.py
File metadata and controls
35 lines (26 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from pymongo import MongoClient, IndexModel, ASCENDING
from datetime import datetime, timedelta, timezone
from decouple import config
from opencv import scanPeople
def insert_document(collection,shopId):
count = scanPeople()
expire_at = datetime.now(timezone.utc) + timedelta(seconds=20)
result=collection.insert_one({'count': count, 'expire_at': expire_at, 'shopId': shopId})
return result.inserted_id
def admin_main(shopId):
# MongoDB 연결
client = MongoClient(config('MONGO_URI'))
db = client[config('MONGO_DB')]
collection = db[config('MONGO_COLLECTION')]
index = IndexModel([("expire_at", ASCENDING)], expireAfterSeconds=0)
collection.create_indexes([index])
inserted_id=insert_document(collection,shopId)
print(inserted_id)
# Change Stream 생성
with collection.watch() as stream:
for change in stream:
# 삭제된 문서의 _id를 출력하고, 데이터를 다시 삽입합니다.
if change['operationType'] == 'delete' and change['documentKey']['_id'] == inserted_id:
print(f"Document deleted: {inserted_id}")
inserted_id=insert_document(collection,shopId)
print(inserted_id)