upgrade: abuse recall on dedicated boards, junk-title and traffic-noise filters, single-instance bot lock
This commit is contained in:
29
omni/bot.py
29
omni/bot.py
@@ -589,5 +589,34 @@ def run_forever():
|
||||
time.sleep(0.3)
|
||||
|
||||
|
||||
|
||||
def _single_instance():
|
||||
"""Один процесс на токен: второй инстанс выходит, иначе TelegramConflictError и бот молчит."""
|
||||
import os, sys, time, tempfile
|
||||
path = os.path.join(tempfile.gettempdir(), "omni_bot.lock")
|
||||
try:
|
||||
if os.path.exists(path):
|
||||
age = time.time() - os.path.getmtime(path)
|
||||
try:
|
||||
pid = int((open(path, encoding="utf-8").read() or "0").strip() or 0)
|
||||
except ValueError:
|
||||
pid = 0
|
||||
if pid and age < 6 * 3600:
|
||||
alive = False
|
||||
try:
|
||||
import psutil
|
||||
alive = psutil.pid_exists(pid) and "omni" in " ".join(psutil.Process(pid).cmdline() or [])
|
||||
except Exception:
|
||||
alive = False
|
||||
if alive:
|
||||
print("уже запущен pid %s - выхожу" % pid, flush=True)
|
||||
sys.exit(0)
|
||||
open(path, "w", encoding="utf-8").write(str(os.getpid()))
|
||||
os.utime(path, None)
|
||||
except Exception as e:
|
||||
print("lock skip", e, flush=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
_single_instance()
|
||||
run_forever()
|
||||
Reference in New Issue
Block a user