- Added new dependencies for Aliyun ECS SDK in requirements.txt and setup.py to support instance management features. - Introduced a new config module to handle TTL settings for ECS instances, allowing for auto-termination based on environment variables. - Updated the manager to utilize TTL settings, including scheduling instance termination with proper error handling and logging. - Maintained existing code logic while enhancing functionality for improved instance lifecycle management.
32 lines
845 B
Python
32 lines
845 B
Python
import os
|
|
|
|
|
|
# Default TTL minutes for instance auto-release (Aliyun-side)
|
|
# Can be overridden via environment variable DEFAULT_TTL_MINUTES
|
|
# ATTENTION: ECS requires TTL to be at least 30 minutes (if TTL > 0)
|
|
MIN_TTL_MINUTES: int = 30
|
|
|
|
_ttl_env_str = os.getenv("DEFAULT_TTL_MINUTES", "60")
|
|
try:
|
|
_ttl_env_val = int(_ttl_env_str)
|
|
except Exception:
|
|
_ttl_env_val = 60
|
|
|
|
# If TTL is positive but less than Aliyun minimum, clamp to 30 minutes
|
|
if _ttl_env_val > 0 and _ttl_env_val < MIN_TTL_MINUTES:
|
|
DEFAULT_TTL_MINUTES: int = MIN_TTL_MINUTES
|
|
else:
|
|
DEFAULT_TTL_MINUTES: int = _ttl_env_val
|
|
|
|
# Master switch for TTL feature
|
|
ENABLE_TTL: bool = os.getenv("ENABLE_TTL", "true").lower() == "true"
|
|
|
|
|
|
def compute_ttl_seconds(ttl_minutes: int) -> int:
|
|
try:
|
|
return max(0, int(ttl_minutes) * 60)
|
|
except Exception:
|
|
return 0
|
|
|
|
|