diff --git a/docker-compose.yml b/docker-compose.yml index 4587167..9773da3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,12 +11,18 @@ services: networks: - mynetwork healthcheck: - test: ["CMD", "mysqladmin", "ping", "--silent"] - interval: 10s + test: ["CMD-SHELL", "mariadb-admin ping -h localhost -u root --password=${MYSQL_ROOT_PASSWORD}"] + interval: 5s + timeout: 5s retries: 5 + start_period: 5s filler: build: ./filler + environment: + MYSQL_DATABASE: ${MYSQL_DATABASE} + MYSQL_USER: ${MYSQL_USER} + MYSQL_PASSWORD: ${MYSQL_PASSWORD} depends_on: db: condition: service_healthy @@ -24,9 +30,18 @@ services: - ./data.csv:/app/data.csv networks: - mynetwork + healthcheck: + test: ["CMD-SHELL", "test", "-f", "/app/data_filled.signal"] + interval: 10s + timeout: 5s + retries: 3 web: build: ./web + environment: + MYSQL_DATABASE: ${MYSQL_DATABASE} + MYSQL_USER: ${MYSQL_USER} + MYSQL_PASSWORD: ${MYSQL_PASSWORD} ports: - "8000:8000" depends_on: diff --git a/filler/Dockerfile b/filler/Dockerfile index 7cfd7c7..70550fe 100644 --- a/filler/Dockerfile +++ b/filler/Dockerfile @@ -5,6 +5,6 @@ WORKDIR /app COPY 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"] diff --git a/filler/filler_db.py b/filler/filler_db.py index c1f4b6d..9ed8d1d 100644 --- a/filler/filler_db.py +++ b/filler/filler_db.py @@ -2,6 +2,11 @@ import os import time import pymysql import csv +import logging +import time + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) db_config = { 'host': 'db', @@ -10,23 +15,42 @@ db_config = { 'db': os.getenv('MYSQL_DATABASE'), } +logger.info("Waiting for the database to be ready...") +logger.info(f"Config: {db_config}") + while True: - print("Try connect to db") + logger.info("Try connect to db") try: connection = pymysql.connect(**db_config) break except pymysql.MySQLError: 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: reader = csv.DictReader(csv_file) for row in reader: cursor.execute("INSERT INTO data (name, age) VALUES (%s, %s)", (row['name'], row['age'])) connection.commit() + + logger.info("Database filled successfully.") with connection.cursor() as cursor: cursor.execute("SELECT * FROM data") 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.") diff --git a/filler/requirements.txt b/filler/requirements.txt new file mode 100644 index 0000000..d4a7eda --- /dev/null +++ b/filler/requirements.txt @@ -0,0 +1 @@ +pymysql