exec.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. """Everything regarding execution of the built app."""
  2. from __future__ import annotations
  3. import hashlib
  4. import json
  5. import os
  6. import platform
  7. import re
  8. import sys
  9. from pathlib import Path
  10. from urllib.parse import urljoin
  11. import psutil
  12. from reflex import constants
  13. from reflex.config import get_config
  14. from reflex.utils import console, path_ops
  15. from reflex.utils.watch import AssetFolderWatch
  16. def start_watching_assets_folder(root):
  17. """Start watching assets folder.
  18. Args:
  19. root: root path of the project.
  20. """
  21. asset_watch = AssetFolderWatch(root)
  22. asset_watch.start()
  23. def detect_package_change(json_file_path: str) -> str:
  24. """Calculates the SHA-256 hash of a JSON file and returns it as a hexadecimal string.
  25. Args:
  26. json_file_path: The path to the JSON file to be hashed.
  27. Returns:
  28. str: The SHA-256 hash of the JSON file as a hexadecimal string.
  29. Example:
  30. >>> detect_package_change("package.json")
  31. 'a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6a7b8c9d0e1f2'
  32. """
  33. with open(json_file_path, "r") as file:
  34. json_data = json.load(file)
  35. # Calculate the hash
  36. json_string = json.dumps(json_data, sort_keys=True)
  37. hash_object = hashlib.sha256(json_string.encode())
  38. return hash_object.hexdigest()
  39. def kill(proc_pid: int):
  40. """Kills a process and all its child processes.
  41. Args:
  42. proc_pid (int): The process ID of the process to be killed.
  43. Example:
  44. >>> kill(1234)
  45. """
  46. process = psutil.Process(proc_pid)
  47. for proc in process.children(recursive=True):
  48. proc.kill()
  49. process.kill()
  50. def run_process_and_launch_url(run_command: list[str]):
  51. """Run the process and launch the URL.
  52. Args:
  53. run_command: The command to run.
  54. """
  55. from reflex.utils import processes
  56. json_file_path = os.path.join(constants.Dirs.WEB, "package.json")
  57. last_hash = detect_package_change(json_file_path)
  58. process = None
  59. first_run = True
  60. while True:
  61. if process is None:
  62. process = processes.new_process(
  63. run_command, cwd=constants.Dirs.WEB, shell=constants.IS_WINDOWS
  64. )
  65. if process.stdout:
  66. for line in processes.stream_logs("Starting frontend", process):
  67. match = re.search(constants.Next.FRONTEND_LISTENING_REGEX, line)
  68. if match:
  69. if first_run:
  70. url = match.group(1)
  71. if get_config().frontend_path != "":
  72. url = urljoin(url, get_config().frontend_path)
  73. console.print(f"App running at: [bold green]{url}")
  74. first_run = False
  75. else:
  76. console.print("New packages detected: Updating app...")
  77. else:
  78. new_hash = detect_package_change(json_file_path)
  79. if new_hash != last_hash:
  80. last_hash = new_hash
  81. kill(process.pid)
  82. process = None
  83. break # for line in process.stdout
  84. if process is not None:
  85. break # while True
  86. def run_frontend(root: Path, port: str):
  87. """Run the frontend.
  88. Args:
  89. root: The root path of the project.
  90. port: The port to run the frontend on.
  91. """
  92. from reflex.utils import prerequisites
  93. # Start watching asset folder.
  94. start_watching_assets_folder(root)
  95. # validate dependencies before run
  96. prerequisites.validate_frontend_dependencies(init=False)
  97. # Run the frontend in development mode.
  98. console.rule("[bold green]App Running")
  99. os.environ["PORT"] = str(get_config().frontend_port if port is None else port)
  100. run_process_and_launch_url([prerequisites.get_package_manager(), "run", "dev"]) # type: ignore
  101. def run_frontend_prod(root: Path, port: str):
  102. """Run the frontend.
  103. Args:
  104. root: The root path of the project (to keep same API as run_frontend).
  105. port: The port to run the frontend on.
  106. """
  107. from reflex.utils import prerequisites
  108. # Set the port.
  109. os.environ["PORT"] = str(get_config().frontend_port if port is None else port)
  110. # validate dependencies before run
  111. prerequisites.validate_frontend_dependencies(init=False)
  112. # Run the frontend in production mode.
  113. console.rule("[bold green]App Running")
  114. run_process_and_launch_url([prerequisites.get_package_manager(), "run", "prod"]) # type: ignore
  115. def run_backend(
  116. host: str,
  117. port: int,
  118. loglevel: constants.LogLevel = constants.LogLevel.ERROR,
  119. ):
  120. """Run the backend.
  121. Args:
  122. host: The app host
  123. port: The app port
  124. loglevel: The log level.
  125. """
  126. import uvicorn
  127. config = get_config()
  128. app_module = f"{config.app_name}.{config.app_name}:{constants.CompileVars.APP}"
  129. # Create a .nocompile file to skip compile for backend.
  130. if os.path.exists(constants.Dirs.WEB):
  131. with open(constants.NOCOMPILE_FILE, "w"):
  132. pass
  133. # Run the backend in development mode.
  134. uvicorn.run(
  135. app=f"{app_module}.{constants.CompileVars.API}",
  136. host=host,
  137. port=port,
  138. log_level=loglevel.value,
  139. reload=True,
  140. reload_dirs=[config.app_name],
  141. )
  142. def run_backend_prod(
  143. host: str,
  144. port: int,
  145. loglevel: constants.LogLevel = constants.LogLevel.ERROR,
  146. ):
  147. """Run the backend.
  148. Args:
  149. host: The app host
  150. port: The app port
  151. loglevel: The log level.
  152. """
  153. from reflex.utils import processes
  154. num_workers = processes.get_num_workers()
  155. config = get_config()
  156. RUN_BACKEND_PROD = f"gunicorn --worker-class {config.gunicorn_worker_class} --preload --timeout {config.timeout} --log-level critical".split()
  157. RUN_BACKEND_PROD_WINDOWS = f"uvicorn --timeout-keep-alive {config.timeout}".split()
  158. app_module = f"{config.app_name}.{config.app_name}:{constants.CompileVars.APP}"
  159. command = (
  160. [
  161. *RUN_BACKEND_PROD_WINDOWS,
  162. "--host",
  163. host,
  164. "--port",
  165. str(port),
  166. app_module,
  167. ]
  168. if constants.IS_WINDOWS
  169. else [
  170. *RUN_BACKEND_PROD,
  171. "--bind",
  172. f"{host}:{port}",
  173. "--threads",
  174. str(num_workers),
  175. f"{app_module}()",
  176. ]
  177. )
  178. command += [
  179. "--log-level",
  180. loglevel.value,
  181. "--workers",
  182. str(num_workers),
  183. ]
  184. processes.new_process(
  185. command,
  186. run=True,
  187. show_logs=True,
  188. env={constants.SKIP_COMPILE_ENV_VAR: "yes"}, # skip compile for prod backend
  189. )
  190. def output_system_info():
  191. """Show system information if the loglevel is in DEBUG."""
  192. if console._LOG_LEVEL > constants.LogLevel.DEBUG:
  193. return
  194. from reflex.utils import prerequisites
  195. config = get_config()
  196. try:
  197. config_file = sys.modules[config.__module__].__file__
  198. except Exception:
  199. config_file = None
  200. console.rule(f"System Info")
  201. console.debug(f"Config file: {config_file!r}")
  202. console.debug(f"Config: {config}")
  203. dependencies = [
  204. f"[Reflex {constants.Reflex.VERSION} with Python {platform.python_version()} (PATH: {sys.executable})]",
  205. f"[Node {prerequisites.get_node_version()} (Expected: {constants.Node.VERSION}) (PATH:{path_ops.get_node_path()})]",
  206. ]
  207. system = platform.system()
  208. if system != "Windows":
  209. dependencies.extend(
  210. [
  211. f"[FNM {prerequisites.get_fnm_version()} (Expected: {constants.Fnm.VERSION}) (PATH: {constants.Fnm.EXE})]",
  212. f"[Bun {prerequisites.get_bun_version()} (Expected: {constants.Bun.VERSION}) (PATH: {config.bun_path})]",
  213. ],
  214. )
  215. else:
  216. dependencies.append(
  217. f"[FNM {prerequisites.get_fnm_version()} (Expected: {constants.Fnm.VERSION}) (PATH: {constants.Fnm.EXE})]",
  218. )
  219. if system == "Linux":
  220. import distro # type: ignore
  221. os_version = distro.name(pretty=True)
  222. else:
  223. os_version = platform.version()
  224. dependencies.append(f"[OS {platform.system()} {os_version}]")
  225. for dep in dependencies:
  226. console.debug(f"{dep}")
  227. console.debug(
  228. f"Using package installer at: {prerequisites.get_install_package_manager()}" # type: ignore
  229. )
  230. console.debug(f"Using package executer at: {prerequisites.get_package_manager()}") # type: ignore
  231. if system != "Windows":
  232. console.debug(f"Unzip path: {path_ops.which('unzip')}")