diff --git a/docker-compose.yml b/docker-compose.yml index 2d9ebe1..ba8f86b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -28,8 +28,10 @@ services: condition: service_healthy volumes: - ./data.csv:/app/data.csv + - shared_data:/shared networks: - mynetwork + restart: "no" web: build: ./web @@ -43,10 +45,15 @@ services: db: condition: service_healthy filler: - condition: service_completed_successfully + condition: service_started networks: - mynetwork + volumes: + - shared_data:/shared networks: mynetwork: driver: bridge + +volumes: + shared_data: diff --git a/filler/filler_db.py b/filler/filler_db.py index 1935f67..ddaea57 100644 --- a/filler/filler_db.py +++ b/filler/filler_db.py @@ -50,3 +50,8 @@ with connection.cursor() as cursor: cursor.execute("SELECT * FROM data") for row in cursor.fetchall(): 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/web/app.py b/web/app.py index e0a2338..01b9f23 100644 --- a/web/app.py +++ b/web/app.py @@ -1,29 +1,70 @@ +# ./web/app.py + from flask import Flask, jsonify import pymysql import os +import logging +import time app = Flask(__name__) +# Configure logging +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + def get_db_connection(): - return pymysql.connect( - host='db', - user=os.getenv('MYSQL_USER'), - password=os.getenv('MYSQL_PASSWORD'), - db=os.getenv('MYSQL_DATABASE') - ) + try: + connection = pymysql.connect( + host='db', + user=os.getenv('MYSQL_USER'), + password=os.getenv('MYSQL_PASSWORD'), + db=os.getenv('MYSQL_DATABASE') + ) + logger.info("Connected to the database.") + return connection + except pymysql.MySQLError as e: + logger.error(f"Database connection failed: {e}") + return None + +def wait_for_file(file_path): + logger.info(f"Waiting for the file: {file_path} to exist...") + while not os.path.isfile(file_path): + logger.debug("File not found, sleeping for 1 second...") + time.sleep(1) + logger.info("Signal file detected, proceeding...") + +@app.route('/health') +def health(): + # Check if the application can connect to the database + conn = get_db_connection() + if conn: + logger.info("Application is healthy.") + return jsonify({"status": "OK"}), 200 + else: + logger.warning("Application health check failed.") + return jsonify({"status": "FAIL"}), 500 @app.route('/') def index(): conn = get_db_connection() - with conn.cursor() as cursor: - cursor.execute("SELECT name, age FROM data") - result = cursor.fetchall() - return jsonify(result) + if conn: + with conn.cursor() as cursor: + cursor.execute("SELECT name, age FROM data") + result = cursor.fetchall() + logger.info("Data queried successfully.") + else: + logger.error("Failed to fetch data due to database connection error.") + return jsonify({"error": "Database connection failed"}), 500 -@app.route('/health') -def health(): - return jsonify({"status": "OK"}) + return jsonify(result), 200 @app.errorhandler(404) def not_found(e): + logger.warning("404 error encountered.") return jsonify({"error": "Not found"}), 404 + +if __name__ == "__main__": + signal_file_path = "/shared/data_filled.signal" + wait_for_file(signal_file_path) + + app.run(host='0.0.0.0', port=8000)