_internal.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # ©️ Dan Gazizullin, 2021-2023
  2. # This file is a part of Hikka Userbot
  3. # 🌐 https://github.com/hikariatama/Hikka
  4. # You can redistribute it and/or modify it under the terms of the GNU AGPLv3
  5. # 🔑 https://www.gnu.org/licenses/agpl-3.0.html
  6. import asyncio
  7. import atexit
  8. import logging
  9. import os
  10. import random
  11. import signal
  12. import sys
  13. async def fw_protect():
  14. await asyncio.sleep(random.randint(1000, 3000) / 1000)
  15. def get_startup_callback() -> callable:
  16. return lambda *_: os.execl(
  17. sys.executable,
  18. sys.executable,
  19. "-m",
  20. os.path.relpath(os.path.abspath(os.path.dirname(os.path.abspath(__file__)))),
  21. *sys.argv[1:],
  22. )
  23. def die():
  24. """Platform-dependent way to kill the current process group"""
  25. if "DOCKER" in os.environ:
  26. sys.exit(0)
  27. else:
  28. # This one is actually better, because it kills all subprocesses
  29. # but it can't be used inside the Docker
  30. os.killpg(os.getpgid(os.getpid()), signal.SIGTERM)
  31. def restart():
  32. if "HIKKA_DO_NOT_RESTART" in os.environ:
  33. print(
  34. "Got in a loop, exiting\nYou probably need to manually remove existing"
  35. " packages and then restart Hikka. Run `pip uninstall -y telethon"
  36. " telethon-mod hikka-tl pyrogram hikka-pyro`, then restart Hikka."
  37. )
  38. sys.exit(0)
  39. logging.getLogger().setLevel(logging.CRITICAL)
  40. print("🔄 Restarting...")
  41. if "LAVHOST" in os.environ:
  42. os.system("lavhost restart")
  43. return
  44. os.environ["HIKKA_DO_NOT_RESTART"] = "1"
  45. if "DOCKER" in os.environ:
  46. atexit.register(get_startup_callback())
  47. else:
  48. # This one is requried for better way of killing to work properly,
  49. # since we kill the process group using unix signals
  50. signal.signal(signal.SIGTERM, get_startup_callback())
  51. die()