Merge pull request #240 from gyptazy/feature/239-add-optional-wait-time-until-service-starts

feature: Add optional wait time before service action.
This commit is contained in:
Florian
2025-06-04 16:49:24 +02:00
committed by GitHub
5 changed files with 50 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
feature:
- Add optional wait time parameter to delay execution until the service takes action (by @gyptazy). #239

View File

@@ -267,6 +267,10 @@ The following options can be set in the configuration file `proxlb.yaml`:
| | `schedule` | | | `Dict` | Schedule config block for rebalancing. |
| | | interval | 12 | `Int` | How often rebalancing should occur in daemon mode.|
| | | format | hours | `Str` | Sets the time format. [values: `hours` (default), `minutes`]|
| | `delay` | | | `Dict` | Schedule config block for an optional delay until the service starts. |
| | | enable | False | `Bool` | If a delay time should be validated.|
| | | time | 1 | `Int` | Delay time until the service starts after the initial execution.|
| | | format | hours | `Str` | Sets the time format. [values: `hours` (default), `minutes`]|
| | log_level | | INFO | `Str` | Defines the default log level that should be logged. [values: `INFO` (default), `WARNING`, `CRITICAL`, `DEBUG`] |
@@ -307,6 +311,10 @@ service:
schedule:
interval: 12
format: hours
delay:
enable: False
time: 1
format: hours
log_level: INFO
```

View File

@@ -33,4 +33,8 @@ service:
schedule:
interval: 12
format: hours
delay:
enable: False
time: 1
format: hours
log_level: INFO

View File

@@ -48,6 +48,9 @@ def main():
# Update log level from config and fallback to INFO if not defined
logger.set_log_level(proxlb_config.get('service', {}).get('log_level', 'INFO'))
# Validate of an optional service delay
Helper.get_service_delay(proxlb_config)
# Connect to Proxmox API & create API object
proxmox_api = ProxmoxApi(proxlb_config)

View File

@@ -145,6 +145,39 @@ class Helper:
logger.debug("Finished: get_daemon_mode.")
@staticmethod
def get_service_delay(proxlb_config: Dict[str, Any]) -> None:
"""
Checks if a start up delay for the service is defined and waits to proceed until
the time is up.
Parameters:
proxlb_config (Dict[str, Any]): A dictionary containing the ProxLB configuration.
Returns:
None
"""
logger.debug("Starting: get_service_delay.")
if proxlb_config.get("service", {}).get("delay", {}).get("enable", False):
# Convert hours to seconds
if proxlb_config["service"]["delay"].get("format", "hours") == "hours":
sleep_seconds = proxlb_config.get("service", {}).get("delay", {}).get("time", 1) * 3600
# Convert minutes to seconds
elif proxlb_config["service"]["delay"].get("format", "hours") == "minutes":
sleep_seconds = proxlb_config.get("service", {}).get("delay", {}).get("time", 60) * 60
else:
logger.error("Invalid format for service delay. Please use 'hours' or 'minutes'.")
sys.exit(1)
logger.info(f"Service delay active: First run in: {proxlb_config.get('service', {}).get('delay', {}).get('time', 1)} {proxlb_config['service']['delay'].get('format', 'hours')}.")
time.sleep(sleep_seconds)
else:
logger.debug("Service delay not active. Proceeding without delay.")
logger.debug("Finished: get_service_delay.")
@staticmethod
def print_json(proxlb_config: Dict[str, Any], print_json: bool = False) -> None:
"""