diff --git a/.env b/.env index 04142fb4..cf36aacc 100644 --- a/.env +++ b/.env @@ -1 +1,3 @@ PG_PASS=guR8uchQhQFAGi4yXfHPrF2AHTgN15sOViDL7tu0bfoiPmdI +PG_USER=lismikro +PG_DB=lismikro \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index f5db5f6a..ab0f038f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -36,20 +36,21 @@ services: ports: - "8088:8080" depends_on: - - laravel-db + - postgresql command: php artisan serve --host=0.0.0.0 --port=8080 networks: - proxy python-listener: build: - context: ./htdocs/listenerlist + context: ./htdocs/listenerlis dockerfile: Dockerfile container_name: python-listener restart: unless-stopped volumes: - - ./htdocs/listenerlist:/app + - ./htdocs/listenerlis:/app depends_on: - - laravel-db + postgresql: + condition: service_healthy networks: - proxy networks: diff --git a/htdocs/Dockerfile b/htdocs/Dockerfile index 4d14b3cd..e0e24c48 100644 --- a/htdocs/Dockerfile +++ b/htdocs/Dockerfile @@ -60,7 +60,10 @@ WORKDIR /var/www/html # RUN composer create-project laravel/laravel:^10 /var/www/html # Set permission -RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache +# RUN chown -R www-data:www-data /var/www/html/htdocs/storage /var/www/html/htdocs/bootstrap/cache EXPOSE 9000 -CMD ["php-fpm"] +#CMD ["php-fpm"] +COPY entrypoint.sh /usr/local/bin/entrypoint.sh +RUN chmod +x /usr/local/bin/entrypoint.sh +ENTRYPOINT ["entrypoint.sh"] diff --git a/htdocs/entrypoint.sh b/htdocs/entrypoint.sh new file mode 100644 index 00000000..601dc006 --- /dev/null +++ b/htdocs/entrypoint.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +# Tunggu database siap +until php artisan migrate:status > /dev/null 2>&1; do + echo "Waiting for database to be ready..." + sleep 3 +done + +# Cek apakah migration sudah pernah dijalankan +if php artisan migrate:status | grep -q "| Y"; then + echo "Database already migrated, skipping migration and seeding." +else + echo "Running migrations and seeders..." + php artisan migrate --force + php artisan db:seed --force +fi + +# Jalankan Laravel server +exec php artisan serve --host=0.0.0.0 --port=8080 diff --git a/htdocs/listenerlis/Dockerfile b/htdocs/listenerlis/Dockerfile index 741042cf..06f913e4 100644 --- a/htdocs/listenerlis/Dockerfile +++ b/htdocs/listenerlis/Dockerfile @@ -2,10 +2,8 @@ FROM python:3.11-slim WORKDIR /app -COPY app.py . +COPY . /app -# Jika ada requirements.txt, tambahkan: -# COPY requirements.txt . -# RUN pip install --no-cache-dir -r requirements.txt +RUN pip install --no-cache-dir -r requirements.txt -CMD ["python", "app.py"] \ No newline at end of file +CMD ["python", "app.py"] diff --git a/htdocs/listenerlis/app.py b/htdocs/listenerlis/app.py index fe639692..63b08b7c 100644 --- a/htdocs/listenerlis/app.py +++ b/htdocs/listenerlis/app.py @@ -6,7 +6,8 @@ from flask import Flask, request, jsonify from sqlalchemy import create_engine, Column, Integer, String, Text, ForeignKey, DateTime from sqlalchemy.orm import declarative_base, sessionmaker import datetime - +import time +from sqlalchemy.exc import OperationalError # Konfigurasi logging logging.basicConfig( filename='listener.log', @@ -18,9 +19,21 @@ logging.basicConfig( app = Flask(__name__) # Konfigurasi Database dengan SQLAlchemy -DATABASE_URL = "postgresql://lismikro:lismikro@localhost:5432/lismikro" +DATABASE_URL = "postgresql://lismikro:lismikro@postgresql:5432/lismikro" engine = create_engine(DATABASE_URL) SessionLocal = sessionmaker(bind=engine) +for i in range(10): + try: + engine = create_engine(DATABASE_URL) + connection = engine.connect() + connection.close() + print("Database connected successfully.") + break + except OperationalError as e: + print(f"Database not ready yet ({i+1}/10): {e}") + time.sleep(5) +else: + raise Exception("Database not available after 10 attempts") Base = declarative_base() # Model Database diff --git a/htdocs/listenerlis/requirements.txt b/htdocs/listenerlis/requirements.txt new file mode 100644 index 00000000..03489e93 --- /dev/null +++ b/htdocs/listenerlis/requirements.txt @@ -0,0 +1,4 @@ +flask +sqlalchemy +hl7 +psycopg2-binary