25 lines
620 B
Python
25 lines
620 B
Python
from pymongo import MongoClient
|
|
from redis import Redis
|
|
import json
|
|
|
|
redis_client = Redis(host='redis', port=6379, decode_responses=True)
|
|
mongo_client = MongoClient("mongodb://mongo:27017")
|
|
mongo_db = mongo_client["logs"]
|
|
mongo_col = mongo_db["actions"]
|
|
|
|
def transfer_actions():
|
|
actions = []
|
|
while True:
|
|
entry = redis_client.lpop("user_actions")
|
|
if entry is None:
|
|
break
|
|
actions.append(json.loads(entry))
|
|
|
|
if actions:
|
|
mongo_col.insert_many(actions)
|
|
print(f"Transferred {len(actions)} actions to MongoDB")
|
|
|
|
if __name__ == "__main__":
|
|
transfer_actions()
|
|
|