exec.py 8.0 KB

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