Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziosalmi committed Feb 1, 2025
1 parent ddc536e commit 1a221bb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
16 changes: 7 additions & 9 deletions lxc_autoscale/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions lxc_autoscale/scaling_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 1a221bb

Please sign in to comment.