exec.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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.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. # 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.CompileVars.APP}"
  126. # Create a .nocompile file to skip compile for backend.
  127. if os.path.exists(constants.Dirs.WEB):
  128. with open(constants.NOCOMPILE_FILE, "w"):
  129. pass
  130. # Run the backend in development mode.
  131. uvicorn.run(
  132. app=f"{app_module}.{constants.CompileVars.API}",
  133. host=host,
  134. port=port,
  135. log_level=loglevel.value,
  136. reload=True,
  137. reload_dirs=[config.app_name],
  138. )
  139. def run_backend_prod(
  140. host: str,
  141. port: int,
  142. loglevel: constants.LogLevel = constants.LogLevel.ERROR,
  143. ):
  144. """Run the backend.
  145. Args:
  146. host: The app host
  147. port: The app port
  148. loglevel: The log level.
  149. """
  150. num_workers = processes.get_num_workers()
  151. config = get_config()
  152. RUN_BACKEND_PROD = f"gunicorn --worker-class uvicorn.workers.UvicornH11Worker --preload --timeout {config.timeout} --log-level critical".split()
  153. RUN_BACKEND_PROD_WINDOWS = f"uvicorn --timeout-keep-alive {config.timeout}".split()
  154. app_module = f"{config.app_name}.{config.app_name}:{constants.CompileVars.APP}"
  155. command = (
  156. [
  157. *RUN_BACKEND_PROD_WINDOWS,
  158. "--host",
  159. host,
  160. "--port",
  161. str(port),
  162. app_module,
  163. ]
  164. if constants.IS_WINDOWS
  165. else [
  166. *RUN_BACKEND_PROD,
  167. "--bind",
  168. f"{host}:{port}",
  169. "--threads",
  170. str(num_workers),
  171. f"{app_module}()",
  172. ]
  173. )
  174. command += [
  175. "--log-level",
  176. loglevel.value,
  177. "--workers",
  178. str(num_workers),
  179. ]
  180. processes.new_process(
  181. command,
  182. run=True,
  183. show_logs=True,
  184. env={constants.SKIP_COMPILE_ENV_VAR: "yes"}, # skip compile for prod backend
  185. )
  186. def output_system_info():
  187. """Show system information if the loglevel is in DEBUG."""
  188. if console._LOG_LEVEL > constants.LogLevel.DEBUG:
  189. return
  190. config = get_config()
  191. try:
  192. config_file = sys.modules[config.__module__].__file__
  193. except Exception:
  194. config_file = None
  195. console.rule(f"System Info")
  196. console.debug(f"Config file: {config_file!r}")
  197. console.debug(f"Config: {config}")
  198. dependencies = [
  199. f"[Reflex {constants.Reflex.VERSION} with Python {platform.python_version()} (PATH: {sys.executable})]",
  200. f"[Node {prerequisites.get_node_version()} (Expected: {constants.Node.VERSION}) (PATH:{path_ops.get_node_path()})]",
  201. ]
  202. system = platform.system()
  203. if system != "Windows":
  204. dependencies.extend(
  205. [
  206. f"[FNM {prerequisites.get_fnm_version()} (Expected: {constants.Fnm.VERSION}) (PATH: {constants.Fnm.EXE})]",
  207. f"[Bun {prerequisites.get_bun_version()} (Expected: {constants.Bun.VERSION}) (PATH: {config.bun_path})]",
  208. ],
  209. )
  210. else:
  211. dependencies.append(
  212. f"[FNM {prerequisites.get_fnm_version()} (Expected: {constants.Fnm.VERSION}) (PATH: {constants.Fnm.EXE})]",
  213. )
  214. if system == "Linux":
  215. import distro # type: ignore
  216. os_version = distro.name(pretty=True)
  217. else:
  218. os_version = platform.version()
  219. dependencies.append(f"[OS {platform.system()} {os_version}]")
  220. for dep in dependencies:
  221. console.debug(f"{dep}")
  222. console.debug(
  223. f"Using package installer at: {prerequisites.get_install_package_manager()}" # type: ignore
  224. )
  225. console.debug(f"Using package executer at: {prerequisites.get_package_manager()}") # type: ignore
  226. if system != "Windows":
  227. console.debug(f"Unzip path: {path_ops.which('unzip')}")