refactor: unify config retrieval in Consul ID generator functions
Co-authored-by: aider (openrouter/openai/o3-mini-high) <aider@aider.chat>
This commit is contained in:
@@ -153,9 +153,10 @@ def get_unique_instance_id() -> int:
|
||||
Returns:
|
||||
int: A globally unique integer ID
|
||||
"""
|
||||
consul_host, consul_port, consul_counter_key, max_retries, retry_delay_seconds = get_config_values()
|
||||
try:
|
||||
# Connect to Consul
|
||||
c = consul.Consul(host=CONSUL_HOST, port=CONSUL_PORT)
|
||||
c = consul.Consul(host=consul_host, port=consul_port)
|
||||
|
||||
# Try to get the current counter value
|
||||
index, data = c.kv.get(CONSUL_COUNTER_KEY)
|
||||
@@ -206,3 +207,27 @@ def get_unique_instance_id() -> int:
|
||||
except Exception as e:
|
||||
logger.error(f"Error accessing Consul: {str(e)}")
|
||||
return generate_fallback_id()
|
||||
def get_config_values():
|
||||
"""
|
||||
Get configuration values from AMQConfiguration or environment variables.
|
||||
|
||||
Returns:
|
||||
tuple: (consul_host, consul_port, consul_counter_key, max_retries, retry_delay_seconds)
|
||||
"""
|
||||
try:
|
||||
config = AMQConfiguration("application.properties")
|
||||
consul_host = os.environ.get('CONSUL_HOST', 'localhost')
|
||||
consul_port = int(os.environ.get('CONSUL_PORT', 8500))
|
||||
consul_counter_key = os.environ.get('CONSUL_COUNTER_KEY', 'service/ids/counter')
|
||||
max_retries = int(os.environ.get('CONSUL_MAX_RETRIES', 5))
|
||||
retry_delay_seconds = float(os.environ.get('CONSUL_RETRY_DELAY_SECONDS', 0.1))
|
||||
return consul_host, consul_port, consul_counter_key, max_retries, retry_delay_seconds
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to load configuration: {str(e)}. Using default values.")
|
||||
return (
|
||||
os.environ.get('CONSUL_HOST', 'localhost'),
|
||||
int(os.environ.get('CONSUL_PORT', 8500)),
|
||||
os.environ.get('CONSUL_COUNTER_KEY', 'service/ids/counter'),
|
||||
int(os.environ.get('CONSUL_MAX_RETRIES', 5)),
|
||||
float(os.environ.get('CONSUL_RETRY_DELAY_SECONDS', 0.1))
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user