Improve database initialization logic for RQ workers
This commit is contained in:
+11
-9
@@ -133,24 +133,26 @@ def init_db(app: Flask) -> None:
|
|||||||
- Populates the IHDR CRC lookup table with initial data
|
- Populates the IHDR CRC lookup table with initial data
|
||||||
- Prints status messages to console during initialization
|
- Prints status messages to console during initialization
|
||||||
"""
|
"""
|
||||||
# Detect if running as RQ worker (not wsgi/web)
|
# Detect if running as RQ worker by checking command line
|
||||||
# rq.worker module is loaded when RQ runs a worker process
|
# Worker: /usr/local/bin/rq worker ...
|
||||||
is_worker = "rq.worker" in sys.modules
|
# 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])
|
||||||
if is_worker:
|
|
||||||
print("Running as worker, skipping database initialization.")
|
|
||||||
return
|
|
||||||
|
|
||||||
with app.app_context():
|
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...")
|
print("Clearing database and file system at restart...")
|
||||||
db.session.remove() # pylint: disable=no-member
|
db.session.remove() # pylint: disable=no-member
|
||||||
db.drop_all()
|
db.drop_all()
|
||||||
rmtree(Path("./results"), ignore_errors=True) # Clear results folder
|
rmtree(Path("./results"), ignore_errors=True) # Clear results folder
|
||||||
|
|
||||||
|
# Always create tables (idempotent - safe to call multiple times)
|
||||||
db.create_all()
|
db.create_all()
|
||||||
print("Database structure created successfully.")
|
print("Database structure created successfully.")
|
||||||
|
|
||||||
print("Filling IHDR lookup table...")
|
# Workers should never fill IHDR table
|
||||||
|
if is_worker:
|
||||||
|
print("Running as worker, skipping IHDR table fill.")
|
||||||
|
else:
|
||||||
fill_ihdr_db()
|
fill_ihdr_db()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user