first commit
This commit is contained in:
7
worker/Dockerfile
Normal file
7
worker/Dockerfile
Normal file
@ -0,0 +1,7 @@
|
||||
FROM python:3.11-slim
|
||||
WORKDIR /app
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
COPY . .
|
||||
CMD ["python", "worker.py"]
|
||||
|
3
worker/requirements.txt
Normal file
3
worker/requirements.txt
Normal file
@ -0,0 +1,3 @@
|
||||
pymongo
|
||||
redis
|
||||
|
24
worker/worker.py
Normal file
24
worker/worker.py
Normal file
@ -0,0 +1,24 @@
|
||||
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()
|
||||
|
Reference in New Issue
Block a user