From f5a4bb99e4cd0679100c76d6f2a6a342a8daf9aa Mon Sep 17 00:00:00 2001 From: aradhyacp Date: Sat, 3 Jan 2026 23:54:52 +0530 Subject: [PATCH 01/12] Add SKIP_IHDR_FILL environment variable for worker service --- compose.dev.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/compose.dev.yml b/compose.dev.yml index 77baee1..afb1244 100644 --- a/compose.dev.yml +++ b/compose.dev.yml @@ -39,6 +39,7 @@ services: - FLASK_DEBUG=true - FLASK_ENV=development - CLEAR_AT_RESTART=0 # Always false for worker to prevent double clearing + - SKIP_IHDR_FILL=1 redis: image: redis:7 From cc0da503537288569623bfed1916e2fac864eea9 Mon Sep 17 00:00:00 2001 From: aradhyacp Date: Sat, 3 Jan 2026 23:55:00 +0530 Subject: [PATCH 02/12] Add SKIP_IHDR_FILL environment variable to worker service --- compose.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/compose.yml b/compose.yml index 4489e66..07c2c59 100644 --- a/compose.yml +++ b/compose.yml @@ -39,6 +39,7 @@ services: - .env environment: - CLEAR_AT_RESTART=0 # Always false for worker to prevent double clearing + - SKIP_IHDR_FILL=1 postgres: image: postgres:18 From 251902d22c57d2649e70d7047317b0aec5ef650b Mon Sep 17 00:00:00 2001 From: aradhyacp Date: Sat, 3 Jan 2026 23:55:07 +0530 Subject: [PATCH 03/12] Skip IHDR table population and database initialization when running as a worker --- aperisolve/models.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/aperisolve/models.py b/aperisolve/models.py index 22d6d34..300f285 100644 --- a/aperisolve/models.py +++ b/aperisolve/models.py @@ -8,6 +8,7 @@ from datetime import datetime, timezone from os import getenv from pathlib import Path from shutil import rmtree +import sys from flask import Flask from flask_sqlalchemy import SQLAlchemy @@ -73,6 +74,11 @@ def fill_ihdr_db() -> None: Returns: None """ + # Skip if table already has entries + if db.session.query(IHDR.iid).first() is not None: + print("IHDR table already populated, skipping fill.") + return + # Standard PNG IHDR parameters resolutions = get_resolutions() bit_color_pairs = get_valid_depth_color_pairs() @@ -127,6 +133,12 @@ def init_db(app: Flask) -> None: - Populates the IHDR CRC lookup table with initial data - Prints status messages to console during initialization """ + # Workers should never clear/fill the database + # Detect if running as RQ worker + if 'rq.worker' in sys.modules: + print("Running as worker, skipping database initialization.") + return + with app.app_context(): if getenv("CLEAR_AT_RESTART", "0") == "1": # Force clear if CLEAR_AT_RESTART print("Clearing database and file system at restart...") From f0c77f8a5d717b722b449e6e447aaf7d19310c70 Mon Sep 17 00:00:00 2001 From: aradhyacp Date: Sun, 4 Jan 2026 00:05:12 +0530 Subject: [PATCH 04/12] Remove database initialization check for RQ worker in init_db function --- aperisolve/models.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/aperisolve/models.py b/aperisolve/models.py index 300f285..72bc8f1 100644 --- a/aperisolve/models.py +++ b/aperisolve/models.py @@ -8,7 +8,6 @@ from datetime import datetime, timezone from os import getenv from pathlib import Path from shutil import rmtree -import sys from flask import Flask from flask_sqlalchemy import SQLAlchemy @@ -133,12 +132,6 @@ def init_db(app: Flask) -> None: - Populates the IHDR CRC lookup table with initial data - Prints status messages to console during initialization """ - # Workers should never clear/fill the database - # Detect if running as RQ worker - if 'rq.worker' in sys.modules: - print("Running as worker, skipping database initialization.") - return - with app.app_context(): if getenv("CLEAR_AT_RESTART", "0") == "1": # Force clear if CLEAR_AT_RESTART print("Clearing database and file system at restart...") From 93564d2e5fe59a84b4369f8b6f34b199c64ad031 Mon Sep 17 00:00:00 2001 From: aradhyacp Date: Sun, 4 Jan 2026 00:05:21 +0530 Subject: [PATCH 05/12] Add CLEAR_AT_RESTART environment variable to web service for Flask debug --- compose.dev.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/compose.dev.yml b/compose.dev.yml index afb1244..19f3af9 100644 --- a/compose.dev.yml +++ b/compose.dev.yml @@ -21,6 +21,7 @@ services: environment: - FLASK_DEBUG=true - FLASK_ENV=development + - CLEAR_AT_RESTART=0 # Don't clear on Flask debug auto-reload worker: #image: ghcr.io/zeecka/aperisolve:latest From 2ce2b82b494333fcbdf4dd65984df2c9ba6e3766 Mon Sep 17 00:00:00 2001 From: aradhyacp Date: Sun, 4 Jan 2026 00:15:00 +0530 Subject: [PATCH 06/12] Enhance IHDR table fill logging for clarity --- aperisolve/models.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/aperisolve/models.py b/aperisolve/models.py index 72bc8f1..5ae1436 100644 --- a/aperisolve/models.py +++ b/aperisolve/models.py @@ -110,7 +110,7 @@ def fill_ihdr_db() -> None: db.session.commit() db.session.commit() # pylint: disable=no-member - print(f"Precomputed {count} common IHDR entries.") + print(f"Precomputed {count} common IHDR entries. IHDR table filled successfully.") def init_db(app: Flask) -> None: @@ -145,6 +145,4 @@ def init_db(app: Flask) -> None: if getenv("SKIP_IHDR_FILL", "0") == "1": print("Skipping IHDR lookup table fill as per configuration.") else: - print("Filling IHDR lookup table...") fill_ihdr_db() - print("IHDR table filled successfully.") From a7783e174e40e2a1168471c9a1b2d87c8d488f79 Mon Sep 17 00:00:00 2001 From: aradhyacp Date: Sun, 4 Jan 2026 00:15:35 +0530 Subject: [PATCH 07/12] Add logging for IHDR lookup table fill process --- aperisolve/models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/aperisolve/models.py b/aperisolve/models.py index 5ae1436..157459b 100644 --- a/aperisolve/models.py +++ b/aperisolve/models.py @@ -145,4 +145,5 @@ def init_db(app: Flask) -> None: if getenv("SKIP_IHDR_FILL", "0") == "1": print("Skipping IHDR lookup table fill as per configuration.") else: + print("Filling IHDR lookup table...") fill_ihdr_db() From f5dd083d10f9bf8396a8b85ecd4e19e998faf771 Mon Sep 17 00:00:00 2001 From: aradhyacp Date: Sun, 4 Jan 2026 01:26:57 +0530 Subject: [PATCH 08/12] Remove SKIP_IHDR_FILL environment variable from worker service --- compose.dev.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/compose.dev.yml b/compose.dev.yml index 19f3af9..ce677ea 100644 --- a/compose.dev.yml +++ b/compose.dev.yml @@ -40,7 +40,6 @@ services: - FLASK_DEBUG=true - FLASK_ENV=development - CLEAR_AT_RESTART=0 # Always false for worker to prevent double clearing - - SKIP_IHDR_FILL=1 redis: image: redis:7 From 370405ab24088a3ae1331867b1036f1092c37906 Mon Sep 17 00:00:00 2001 From: aradhyacp Date: Sun, 4 Jan 2026 01:27:05 +0530 Subject: [PATCH 09/12] Remove SKIP_IHDR_FILL environment variable from worker service --- compose.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/compose.yml b/compose.yml index 07c2c59..4489e66 100644 --- a/compose.yml +++ b/compose.yml @@ -39,7 +39,6 @@ services: - .env environment: - CLEAR_AT_RESTART=0 # Always false for worker to prevent double clearing - - SKIP_IHDR_FILL=1 postgres: image: postgres:18 From 4faf7fd9b517e6694fb9fd6ac2587eb4da3fc0c6 Mon Sep 17 00:00:00 2001 From: aradhyacp Date: Sun, 4 Jan 2026 01:27:12 +0530 Subject: [PATCH 10/12] Skip database initialization when running as RQ worker --- aperisolve/models.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/aperisolve/models.py b/aperisolve/models.py index 157459b..cda1c9e 100644 --- a/aperisolve/models.py +++ b/aperisolve/models.py @@ -4,6 +4,7 @@ """This module defines the database models for the Aperi'Solve application.""" import itertools +import sys from datetime import datetime, timezone from os import getenv from pathlib import Path @@ -132,6 +133,13 @@ def init_db(app: Flask) -> None: - Populates the IHDR CRC lookup table with initial data - Prints status messages to console during initialization """ + # Detect if running as RQ worker (not wsgi/web) + is_worker = len(sys.argv) > 0 and ("rq" in sys.argv[0] or "rq.worker" in sys.modules) + + if is_worker: + print("Running as worker, skipping database initialization.") + return + with app.app_context(): if getenv("CLEAR_AT_RESTART", "0") == "1": # Force clear if CLEAR_AT_RESTART print("Clearing database and file system at restart...") @@ -142,8 +150,6 @@ def init_db(app: Flask) -> None: db.create_all() print("Database structure created successfully.") - if getenv("SKIP_IHDR_FILL", "0") == "1": - print("Skipping IHDR lookup table fill as per configuration.") - else: - print("Filling IHDR lookup table...") - fill_ihdr_db() + print("Filling IHDR lookup table...") + fill_ihdr_db() + From 2e54cf0b468899122499998b2718673ae1dc5718 Mon Sep 17 00:00:00 2001 From: aradhyacp Date: Sun, 4 Jan 2026 01:27:57 +0530 Subject: [PATCH 11/12] Refactor worker detection logic in database initialization --- aperisolve/models.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/aperisolve/models.py b/aperisolve/models.py index cda1c9e..358ed5b 100644 --- a/aperisolve/models.py +++ b/aperisolve/models.py @@ -134,7 +134,8 @@ def init_db(app: Flask) -> None: - Prints status messages to console during initialization """ # Detect if running as RQ worker (not wsgi/web) - is_worker = len(sys.argv) > 0 and ("rq" in sys.argv[0] or "rq.worker" in sys.modules) + # rq.worker module is loaded when RQ runs a worker process + is_worker = "rq.worker" in sys.modules if is_worker: print("Running as worker, skipping database initialization.") From 6bdcf990452890577149036de8d4de0b8034e412 Mon Sep 17 00:00:00 2001 From: aradhyacp Date: Sun, 4 Jan 2026 01:35:27 +0530 Subject: [PATCH 12/12] Improve database initialization logic for RQ workers --- aperisolve/models.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/aperisolve/models.py b/aperisolve/models.py index 358ed5b..e9f82b6 100644 --- a/aperisolve/models.py +++ b/aperisolve/models.py @@ -133,24 +133,26 @@ def init_db(app: Flask) -> None: - Populates the IHDR CRC lookup table with initial data - Prints status messages to console during initialization """ - # Detect if running as RQ worker (not wsgi/web) - # rq.worker module is loaded when RQ runs a worker process - is_worker = "rq.worker" in sys.modules - - if is_worker: - print("Running as worker, skipping database initialization.") - return + # Detect if running as RQ worker by checking command line + # Worker: /usr/local/bin/rq worker ... + # Web: /usr/local/bin/flask run ... or gunicorn/wsgi + is_worker = any("rq" in arg and "flask" not in arg for arg in sys.argv[:2]) with app.app_context(): - if getenv("CLEAR_AT_RESTART", "0") == "1": # Force clear if CLEAR_AT_RESTART + # Workers should never clear the database + if not is_worker and getenv("CLEAR_AT_RESTART", "0") == "1": print("Clearing database and file system at restart...") db.session.remove() # pylint: disable=no-member db.drop_all() rmtree(Path("./results"), ignore_errors=True) # Clear results folder + # Always create tables (idempotent - safe to call multiple times) db.create_all() print("Database structure created successfully.") - print("Filling IHDR lookup table...") - fill_ihdr_db() + # Workers should never fill IHDR table + if is_worker: + print("Running as worker, skipping IHDR table fill.") + else: + fill_ihdr_db()