up
This commit is contained in:
parent
3401cf6fd8
commit
8cad8986fc
|
@ -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:
|
||||
|
|
|
@ -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"]
|
||||
|
|
|
@ -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.")
|
||||
|
|
1
filler/requirements.txt
Normal file
1
filler/requirements.txt
Normal file
|
@ -0,0 +1 @@
|
|||
pymysql
|
Loading…
Reference in a new issue