This commit is contained in:
Timofey K 2024-11-23 14:59:55 +00:00
parent 3401cf6fd8
commit 8cad8986fc
4 changed files with 47 additions and 7 deletions

View file

@ -11,12 +11,18 @@ services:
networks: networks:
- mynetwork - mynetwork
healthcheck: healthcheck:
test: ["CMD", "mysqladmin", "ping", "--silent"] test: ["CMD-SHELL", "mariadb-admin ping -h localhost -u root --password=${MYSQL_ROOT_PASSWORD}"]
interval: 10s interval: 5s
timeout: 5s
retries: 5 retries: 5
start_period: 5s
filler: filler:
build: ./filler build: ./filler
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
depends_on: depends_on:
db: db:
condition: service_healthy condition: service_healthy
@ -24,9 +30,18 @@ services:
- ./data.csv:/app/data.csv - ./data.csv:/app/data.csv
networks: networks:
- mynetwork - mynetwork
healthcheck:
test: ["CMD-SHELL", "test", "-f", "/app/data_filled.signal"]
interval: 10s
timeout: 5s
retries: 3
web: web:
build: ./web build: ./web
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
ports: ports:
- "8000:8000" - "8000:8000"
depends_on: depends_on:

View file

@ -5,6 +5,6 @@ WORKDIR /app
COPY requirements.txt ./ COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt RUN pip install --no-cache-dir -r requirements.txt
COPY fill_db.py ./ COPY filler_db.py ./
ENTRYPOINT ["python", "fill_db.py"] ENTRYPOINT ["python", "filler_db.py"]

View file

@ -2,6 +2,11 @@ import os
import time import time
import pymysql import pymysql
import csv import csv
import logging
import time
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
db_config = { db_config = {
'host': 'db', 'host': 'db',
@ -10,15 +15,28 @@ db_config = {
'db': os.getenv('MYSQL_DATABASE'), 'db': os.getenv('MYSQL_DATABASE'),
} }
logger.info("Waiting for the database to be ready...")
logger.info(f"Config: {db_config}")
while True: while True:
print("Try connect to db") logger.info("Try connect to db")
try: try:
connection = pymysql.connect(**db_config) connection = pymysql.connect(**db_config)
break break
except pymysql.MySQLError: except pymysql.MySQLError:
time.sleep(3) time.sleep(3)
print("") with connection.cursor() as cursor:
cursor.execute("""
CREATE TABLE IF NOT EXISTS data (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
age INT NOT NULL
);
""")
logger.info("Table 'data' is ready.")
logger.info("Start reading csv and insert to db")
with connection.cursor() as cursor, open('data.csv', 'r') as csv_file: with connection.cursor() as cursor, open('data.csv', 'r') as csv_file:
reader = csv.DictReader(csv_file) reader = csv.DictReader(csv_file)
@ -26,7 +44,13 @@ with connection.cursor() as cursor, open('data.csv', 'r') as csv_file:
cursor.execute("INSERT INTO data (name, age) VALUES (%s, %s)", (row['name'], row['age'])) cursor.execute("INSERT INTO data (name, age) VALUES (%s, %s)", (row['name'], row['age']))
connection.commit() connection.commit()
logger.info("Database filled successfully.")
with connection.cursor() as cursor: with connection.cursor() as cursor:
cursor.execute("SELECT * FROM data") cursor.execute("SELECT * FROM data")
for row in cursor.fetchall(): for row in cursor.fetchall():
print(row) logger.info(f"Data: {row}")
with open('/app/data_filled.signal', 'w') as signal_file:
signal_file.write('done')
logger.info("Filler task completed. Signal file created.")

1
filler/requirements.txt Normal file
View file

@ -0,0 +1 @@
pymysql