-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.py
More file actions
61 lines (50 loc) · 1.98 KB
/
worker.py
File metadata and controls
61 lines (50 loc) · 1.98 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
import sys
import time
import subprocess
import os
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from dotenv import load_dotenv
os.chdir(os.path.dirname(os.path.abspath(__file__)))
load_dotenv(override=True)
print("Environment variables loaded from .env file")
class ChangeHandler(FileSystemEventHandler):
def __init__(self, script):
self.script = script
self.process = None
self.run_script()
def run_script(self):
if self.process and self.process.poll() is None:
print(f"Terminating existing {self.script} process...")
self.process.terminate()
self.process.wait(timeout=5)
env = os.environ.copy()
print(f"Starting {self.script}...")
self.process = subprocess.Popen([sys.executable, self.script], env=env)
print(f"{self.script} is now running")
def on_modified(self, event):
if event.src_path.endswith(self.script):
print(f'{self.script} has been modified. Restarting...')
self.run_script()
if __name__ == "__main__":
script_to_watch = "main.py"
print("\n===== WhatsApp Bot Runner =====")
print(f"Starting and monitoring {script_to_watch}")
print("This script will automatically restart the bot when changes are detected")
event_handler = ChangeHandler(script_to_watch)
observer = Observer()
observer.schedule(event_handler, path='.', recursive=False)
observer.start()
try:
print(f"\nWatching for changes in {script_to_watch}...")
print("Press Ctrl+C to stop the bot")
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\nStopping observer and bot process...")
observer.stop()
if event_handler.process and event_handler.process.poll() is None:
event_handler.process.terminate()
event_handler.process.wait(timeout=5)
print("Bot stopped successfully")
observer.join()