diff --git a/lxc_autoscale/notification.py b/lxc_autoscale/notification.py index bdd9772..d783a2a 100644 --- a/lxc_autoscale/notification.py +++ b/lxc_autoscale/notification.py @@ -37,10 +37,10 @@ def send_notification(self, title: str, message: str, priority: int = 5): headers = {'X-Gotify-Key': self.token} try: - response = requests.post(f"{self.url}/message", data=payload, headers=headers) + response = requests.post(f"{self.url}/message", json=payload, headers=headers, timeout=10) response.raise_for_status() logging.info(f"Gotify notification sent: {title} - {message}") - except requests.exceptions.RequestException as e: + except Exception as e: logging.error(f"Gotify notification failed: {e}") # Email notification implementation @@ -71,7 +71,7 @@ def send_notification(self, title: str, message: str, priority: int = 5): msg['To'] = ', '.join(self.to_addrs) try: - with smtplib.SMTP(self.smtp_server, self.port) as server: + with smtplib.SMTP(self.smtp_server, self.port, timeout=10) as server: server.starttls() # Encrypt the connection server.login(self.username, self.password) # Authenticate with the SMTP server server.sendmail(self.from_addr, self.to_addrs, msg.as_string()) # Send the email @@ -97,13 +97,11 @@ def send_notification(self, title: str, message: str, priority: int = 5): priority (int): Unused, but kept for interface consistency. """ try: - response = requests.get(self.webhook_url) - if response.status_code == 200: - logging.info("Uptime Kuma notification sent successfully") - else: - logging.error(f"Failed to send Uptime Kuma notification: {response.status_code}") + response = requests.post(self.webhook_url, json={'title': title, 'message': message, 'priority': priority}, timeout=10) + response.raise_for_status() + logging.info("Uptime Kuma notification sent successfully") except Exception as e: - logging.error(f"Error sending Uptime Kuma notification: {e}") + logging.error(f"Failed to send Uptime Kuma notification: {e}") # Send notification to all initialized notifiers def send_notification(title, message, priority=5): diff --git a/lxc_autoscale/scaling_manager.py b/lxc_autoscale/scaling_manager.py index d561ff0..b8ee939 100644 --- a/lxc_autoscale/scaling_manager.py +++ b/lxc_autoscale/scaling_manager.py @@ -98,8 +98,8 @@ def scale_memory(ctid: str, mem_usage: float, mem_upper: float, mem_lower: float int((mem_usage - mem_upper) * config['memory_min_increment'] / MEMORY_SCALE_FACTOR), ) if available_memory >= increment: - logging.info(f"Increasing memory for container {ctid} by {increment}MB (current: {current_memory}MB, new: {new_memory}MB)") new_memory = current_memory + increment + logging.info(f"Increasing memory for container {ctid} by {increment}MB (current: {current_memory}MB, new: {new_memory}MB)") run_command(f"pct set {ctid} -memory {new_memory}") available_memory -= increment memory_changed = True @@ -115,8 +115,8 @@ def scale_memory(ctid: str, mem_usage: float, mem_upper: float, mem_lower: float min_memory, ) if decrease_amount > 0: - logging.info(f"Decreasing memory for container {ctid} by {decrease_amount}MB (current: {current_memory}MB, new: {new_memory}MB)") new_memory = current_memory - decrease_amount + logging.info(f"Decreasing memory for container {ctid} by {decrease_amount}MB (current: {current_memory}MB, new: {new_memory}MB)") run_command(f"pct set {ctid} -memory {new_memory}") available_memory += decrease_amount memory_changed = True