Skip to content

Commit 776c933

Browse files
committed
fix: guard against multiple cleanup threads and register AppConfig
- Add default_app_config in __init__.py so Django picks up the AppConfig - Use a module-level lock to ensure only one cleanup thread starts (gunicorn calls ready() multiple times for master + workers) - Remove debug print statements
1 parent 4757e7c commit 776c933

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

app/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
default_app_config = 'app.apps.UsersConfig'

app/apps.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,33 @@
77

88
logger = logging.getLogger(__name__)
99

10+
_cleanup_started = False
11+
_cleanup_lock = threading.Lock()
12+
1013

1114
class UsersConfig(AppConfig):
12-
name = 'api'
15+
name = 'app'
1316

1417
def ready(self):
15-
interval = int(os.getenv('CONFERENCE_CLEANUP_INTERVAL_SECONDS', 3600))
18+
global _cleanup_started
1619

20+
interval = int(os.getenv('CONFERENCE_CLEANUP_INTERVAL_SECONDS', 3600))
1721
if interval <= 0:
1822
return
1923

24+
with _cleanup_lock:
25+
if _cleanup_started:
26+
return
27+
_cleanup_started = True
28+
2029
def cleanup_loop():
21-
# Wait before first run to let the app fully start
2230
time.sleep(60)
2331

2432
while True:
2533
try:
2634
from app.management.commands.cleanup_stale_conferences import Command
27-
Command().handle(hours=int(os.getenv('CONFERENCE_TIMEOUT_HOURS', 4)), dry_run=False, verbosity=1)
35+
hours = int(os.getenv('CONFERENCE_TIMEOUT_HOURS', 4))
36+
Command().handle(hours=hours, dry_run=False, verbosity=0)
2837
except Exception as e:
2938
logger.warning(f'[ConferenceCleanup] Error: {e}')
3039

0 commit comments

Comments
 (0)