up
This commit is contained in:
parent
1fb166b16b
commit
086ad877c9
|
@ -28,8 +28,10 @@ services:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
volumes:
|
volumes:
|
||||||
- ./data.csv:/app/data.csv
|
- ./data.csv:/app/data.csv
|
||||||
|
- shared_data:/shared
|
||||||
networks:
|
networks:
|
||||||
- mynetwork
|
- mynetwork
|
||||||
|
restart: "no"
|
||||||
|
|
||||||
web:
|
web:
|
||||||
build: ./web
|
build: ./web
|
||||||
|
@ -43,10 +45,15 @@ services:
|
||||||
db:
|
db:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
filler:
|
filler:
|
||||||
condition: service_completed_successfully
|
condition: service_started
|
||||||
networks:
|
networks:
|
||||||
- mynetwork
|
- mynetwork
|
||||||
|
volumes:
|
||||||
|
- shared_data:/shared
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
mynetwork:
|
mynetwork:
|
||||||
driver: bridge
|
driver: bridge
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
shared_data:
|
||||||
|
|
|
@ -50,3 +50,8 @@ 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():
|
||||||
logger.info(f"Data: {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.")
|
||||||
|
|
51
web/app.py
51
web/app.py
|
@ -1,29 +1,70 @@
|
||||||
|
# ./web/app.py
|
||||||
|
|
||||||
from flask import Flask, jsonify
|
from flask import Flask, jsonify
|
||||||
import pymysql
|
import pymysql
|
||||||
import os
|
import os
|
||||||
|
import logging
|
||||||
|
import time
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
# Configure logging
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
def get_db_connection():
|
def get_db_connection():
|
||||||
return pymysql.connect(
|
try:
|
||||||
|
connection = pymysql.connect(
|
||||||
host='db',
|
host='db',
|
||||||
user=os.getenv('MYSQL_USER'),
|
user=os.getenv('MYSQL_USER'),
|
||||||
password=os.getenv('MYSQL_PASSWORD'),
|
password=os.getenv('MYSQL_PASSWORD'),
|
||||||
db=os.getenv('MYSQL_DATABASE')
|
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('/')
|
@app.route('/')
|
||||||
def index():
|
def index():
|
||||||
conn = get_db_connection()
|
conn = get_db_connection()
|
||||||
|
if conn:
|
||||||
with conn.cursor() as cursor:
|
with conn.cursor() as cursor:
|
||||||
cursor.execute("SELECT name, age FROM data")
|
cursor.execute("SELECT name, age FROM data")
|
||||||
result = cursor.fetchall()
|
result = cursor.fetchall()
|
||||||
return jsonify(result)
|
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')
|
return jsonify(result), 200
|
||||||
def health():
|
|
||||||
return jsonify({"status": "OK"})
|
|
||||||
|
|
||||||
@app.errorhandler(404)
|
@app.errorhandler(404)
|
||||||
def not_found(e):
|
def not_found(e):
|
||||||
|
logger.warning("404 error encountered.")
|
||||||
return jsonify({"error": "Not found"}), 404
|
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)
|
||||||
|
|
Loading…
Reference in a new issue