-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmonitor.py
More file actions
71 lines (58 loc) · 2.49 KB
/
Copy pathmonitor.py
File metadata and controls
71 lines (58 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import requests
import time
from pushbullet import Pushbullet
from config import Config
import subprocess
import logging
BASE_URL = Config.BASE_URL
HEALTH_CHECK_URL = f"{BASE_URL}/health"
PUSHBULLET_API_KEY = Config.PUSHBULLET_API_KEY
# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
pb = Pushbullet(PUSHBULLET_API_KEY)
def send_notification(title, body):
pb.push_note(title, body)
logger.info(f"Notification sent: {title} - {body}")
def check_health():
try:
response = requests.get(HEALTH_CHECK_URL, timeout=10)
return response.status_code == 200
except requests.RequestException:
return False
def restart_ecosystem():
try:
subprocess.run(["pm2", "restart", "ecosystem.config.js"], check=True)
send_notification("Application Restarted", "The application has been automatically restarted.")
except subprocess.CalledProcessError as e:
send_notification("Restart Failed", f"Failed to restart the application: {str(e)}")
def monitor():
# Initial delay to allow application startup
initial_delay = 3600 # 1 hour in seconds
logger.info(f"Monitor starting. Waiting {initial_delay} seconds before first health check.")
time.sleep(initial_delay)
notification_sent = False
check_interval = 3600 # 1 hour in seconds
restart_cooldown = 300 # 5 minutes in seconds
while True:
if not check_health():
logger.warning("Health check failed.")
if not notification_sent:
#send_notification("Application Down", "The application is not responding.")
notification_sent = True
# Attempt to restart the application
logger.info("Attempting to restart the application.")
restart_ecosystem()
# Wait for restart cooldown before checking again
logger.info(f"Waiting {restart_cooldown} seconds before next health check.")
time.sleep(restart_cooldown)
else:
if notification_sent:
#send_notification("Application Recovered", "The application is responding again.")
notification_sent = False
logger.info("Health check passed.")
logger.info(f"Waiting {check_interval} seconds until next health check.")
time.sleep(check_interval)
if __name__ == "__main__":
logger.info("Application monitor started.")
monitor()