Files
eveAI/common/utils/startup_eveai.py
Josako 25213f2004 - Implementation of specialist execution api, including SSE protocol
- eveai_chat becomes deprecated and should be replaced with SSE
- Adaptation of STANDARD_RAG specialist
- Base class definition allowing to realise specialists with crewai framework
- Implementation of SPIN_SPECIALIST
- Implementation of test app for testing specialists (test_specialist_client). Also serves as an example for future SSE-based client
- Improvements to startup scripts to better handle and scale multiple connections
- Small improvements to the interaction forms and views
- Caching implementation improved and augmented with additional caches
2025-02-20 05:50:16 +01:00

52 lines
1.9 KiB
Python

import time
from redis import Redis
from common.extensions import cache_manager
def perform_startup_actions(app):
perform_startup_invalidation(app)
def perform_startup_invalidation(app):
"""
Perform cache invalidation only once during startup using a persistent marker (also called flag or semaphore
- see docs).
Uses a combination of lock and marker to ensure invalidation happens exactly once
per deployment.
"""
redis_client = Redis.from_url(app.config['REDIS_BASE_URI'])
startup_time = int(time.time())
marker_key = 'startup_invalidation_completed'
lock_key = 'startup_invalidation_lock'
try:
# First try to get the lock
lock = redis_client.lock(lock_key, timeout=30)
if lock.acquire(blocking=False):
try:
# Check if invalidation was already performed
if not redis_client.get(marker_key):
app.logger.debug(f"Performing cache invalidation at startup time {startup_time}")
app.logger.debug(f"Current cache keys: {redis_client.keys('*')}")
# Perform invalidation
cache_manager.invalidate_region('eveai_config')
cache_manager.invalidate_region('eveai_chat_workers')
app.logger.debug(f"Cache keys after invalidation: {redis_client.keys('*')}")
redis_client.setex(marker_key, 180, str(startup_time))
app.logger.info("Startup cache invalidation completed")
else:
app.logger.info("Startup cache invalidation already performed")
finally:
lock.release()
else:
app.logger.info("Another process is handling startup invalidation")
except Exception as e:
app.logger.error(f"Error during startup invalidation: {e}")
# In case of error, we don't want to block the application startup
pass