Gabeczkag commited on
Commit
c010bc1
·
1 Parent(s): 294b69f

Fix mainrun.py - add signal handling, timeouts, bugfix

Browse files
Files changed (2) hide show
  1. au.version +1 -1
  2. mainrun.py +48 -18
au.version CHANGED
@@ -1 +1 @@
1
- 22
 
1
+ 23
mainrun.py CHANGED
@@ -1,15 +1,25 @@
1
  import os
 
2
  import subprocess
 
3
  import time
4
  from datetime import datetime
5
 
 
 
 
 
6
 
7
  def log(msg):
8
- print(f"[{datetime.now().strftime('%H:%M:%S')}] {msg}")
9
 
10
 
11
- def run(cmd, **kwargs):
12
- return subprocess.run(cmd, shell=True, capture_output=True, text=True, **kwargs)
 
 
 
 
13
 
14
 
15
  def read_local_version():
@@ -22,7 +32,7 @@ def read_local_version():
22
 
23
  def read_remote_version():
24
  result = run("git show origin/main:au.version")
25
- if result.returncode == 0:
26
  lines = result.stdout.strip().splitlines()
27
  return lines[0] if lines else "0"
28
  return "0"
@@ -30,28 +40,43 @@ def read_remote_version():
30
 
31
  def ensure_bot():
32
  result = run("screen -ls")
33
- if result.returncode != 0 or "bot" not in result.stdout:
34
  log("Bot nie dziala, uruchamiam...")
35
- subprocess.Popen(
36
- ["python", "bot.py"],
37
- cwd=os.path.expanduser("~/BotDC"),
38
- creationflags=subprocess.CREATE_NEW_PROCESS_GROUP,
39
- )
40
 
41
 
42
  def main():
43
- os.chdir(os.path.expanduser("~/BotDC"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  local_ver = read_local_version()
46
  log(f"Start updatera, aktualna wersja: {local_ver}")
47
 
48
  log("Bot uruchomiony")
49
- bot_proc = subprocess.Popen(["python", "bot.py"])
50
 
51
  while True:
52
  time.sleep(30)
53
 
54
- run("git fetch origin main")
 
 
 
55
  remote_ver = read_remote_version()
56
 
57
  if remote_ver != local_ver:
@@ -67,20 +92,25 @@ def main():
67
 
68
  if "install-packages=true" in au_content:
69
  log("Instaluje pakiety...")
70
- run("pip install -r requirements.txt")
71
 
72
  local_ver = read_local_version()
73
  log("Restartuje bota po aktualizacji...")
74
 
75
- if bot_proc.poll() is None:
76
  bot_proc.terminate()
77
- bot_proc.wait()
 
 
 
78
 
79
- bot_proc = subprocess.Popen(["python", "bot.py"])
80
  time.sleep(1)
81
 
82
  if bot_proc.poll() is not None:
83
- ensure_bot()
 
 
84
 
85
 
86
  if __name__ == "__main__":
 
1
  import os
2
+ import signal
3
  import subprocess
4
+ import sys
5
  import time
6
  from datetime import datetime
7
 
8
+ BOT_DIR = os.path.expanduser("~/BotDC")
9
+ GIT_TIMEOUT = 30
10
+ BOT_TIMEOUT = 10
11
+
12
 
13
  def log(msg):
14
+ print(f"[{datetime.now().strftime('%H:%M:%S')}] {msg}", flush=True)
15
 
16
 
17
+ def run(cmd, timeout=GIT_TIMEOUT, **kwargs):
18
+ try:
19
+ return subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=timeout, **kwargs)
20
+ except subprocess.TimeoutExpired:
21
+ log(f"Timeout: {cmd}")
22
+ return None
23
 
24
 
25
  def read_local_version():
 
32
 
33
  def read_remote_version():
34
  result = run("git show origin/main:au.version")
35
+ if result and result.returncode == 0:
36
  lines = result.stdout.strip().splitlines()
37
  return lines[0] if lines else "0"
38
  return "0"
 
40
 
41
  def ensure_bot():
42
  result = run("screen -ls")
43
+ if result is None or result.returncode != 0 or "bot" not in result.stdout:
44
  log("Bot nie dziala, uruchamiam...")
45
+ return subprocess.Popen(["python", "bot.py"], cwd=BOT_DIR)
46
+ return None
 
 
 
47
 
48
 
49
  def main():
50
+ os.chdir(BOT_DIR)
51
+
52
+ bot_proc = None
53
+
54
+ def cleanup(sig=None, frame=None):
55
+ log("Zatrzymywanie...")
56
+ if bot_proc and bot_proc.poll() is None:
57
+ bot_proc.terminate()
58
+ try:
59
+ bot_proc.wait(timeout=BOT_TIMEOUT)
60
+ except subprocess.TimeoutExpired:
61
+ bot_proc.kill()
62
+ sys.exit(0)
63
+
64
+ signal.signal(signal.SIGINT, cleanup)
65
+ signal.signal(signal.SIGTERM, cleanup)
66
 
67
  local_ver = read_local_version()
68
  log(f"Start updatera, aktualna wersja: {local_ver}")
69
 
70
  log("Bot uruchomiony")
71
+ bot_proc = subprocess.Popen(["python", "bot.py"], cwd=BOT_DIR)
72
 
73
  while True:
74
  time.sleep(30)
75
 
76
+ result = run("git fetch origin main")
77
+ if result is None:
78
+ continue
79
+
80
  remote_ver = read_remote_version()
81
 
82
  if remote_ver != local_ver:
 
92
 
93
  if "install-packages=true" in au_content:
94
  log("Instaluje pakiety...")
95
+ run("pip install -r requirements.txt", timeout=120)
96
 
97
  local_ver = read_local_version()
98
  log("Restartuje bota po aktualizacji...")
99
 
100
+ if bot_proc and bot_proc.poll() is None:
101
  bot_proc.terminate()
102
+ try:
103
+ bot_proc.wait(timeout=BOT_TIMEOUT)
104
+ except subprocess.TimeoutExpired:
105
+ bot_proc.kill()
106
 
107
+ bot_proc = subprocess.Popen(["python", "bot.py"], cwd=BOT_DIR)
108
  time.sleep(1)
109
 
110
  if bot_proc.poll() is not None:
111
+ new_proc = ensure_bot()
112
+ if new_proc:
113
+ bot_proc = new_proc
114
 
115
 
116
  if __name__ == "__main__":