reflex/reflex/utils/exec.py
Masen Furer deae662e2a
[REF-1988] API to Get instance of Arbitrary State class (#2678)
* WiP get_state

* Refactor get_state fast path

Rudimentary protection for state instance access from a background task
(StateProxy)

* retain dirty substate marking per `_mark_dirty` call to avoid test changes

* Find common ancestor by part instead of by character

Fix StateProxy for substates and parent_state attributes (have to handle in
__getattr__, not property)

Fix type annotation for `get_state`

* test_state: workflow test for `get_state` functionality

* Do not reset _always_dirty_substates when adding vars

Reset the substate tracking only when the class is instantiated.

* test_state_tree: test substate access in a larger state tree

Ensure that `get_state` returns the proper "branch" of the state tree depending
on what substate is requested.

* test_format: fixup broken tests from adding substates of TestState

* Fix flaky integration tests with more polling

* AppHarness: reset _always_dirty_substates on rx.State

* RuntimeError unless State is instantiated with _reflex_internal_init=True

Avoid user errors trying to directly instantiate State classes

* Helper functions for _substate_key and _split_substate_key

Unify the implementation of generating and decoding the token + state name
format used for redis state sharding.

* StateManagerRedis: use create_task in get_state and set_state

read and write substates concurrently (allow redis to shine)

* test_state_inheritance: use polling cuz life too short for flaky tests

kthnxbai ❤️

* Move _is_testing_env to reflex.utils.exec.is_testing_env

Reuse the code in app.py

* Break up `BaseState.get_state` and friends into separate methods

* Add test case for pre-fetching cached var dependency

* Move on_load_internal and update_vars_internal to substates

Avoid loading the entire state tree to process these common internal events. If
the state tree is very large, this allow page navigation to occur more quickly.

Pre-fetch substates that contain cached vars, as they may need to be recomputed
if certain vars change.

* Do not copy ROUTER_DATA into all substates.

This is a waste of time and memory, and can be handled via a special case in
__getattribute__

* Track whether State instance _was_touched

Avoid wasting time serializing states that have no modifications

* Do not persist states in `StateManagerRedis.get_state`

Wait until the state is actually modified, and then persist it as part of `set_state`.

Factor out common logic into helper methods for readability and to reduce
duplication of common logic.

To avoid having to recursively call `get_state`, which would require persisting
the instance and then getting it again, some of the initialization logic
regarding parent_state and substates is duplicated when creating a new
instance. This is for performance reasons.

* Remove stray print()

* context.js.jinja2: fix check for empty local storage / cookie vars

* Add comments for onLoadInternalEvent and initialEvents

* nit: typo

* split _get_was_touched into _update_was_touched

Improve clarity in cases where _get_was_touched was being called for its side
effects only.

* Remove extraneous information from incorrect State instantiation error

* Update missing redis exception message
2024-02-27 13:02:08 -08:00

297 lines
8.6 KiB
Python

"""Everything regarding execution of the built app."""
from __future__ import annotations
import hashlib
import json
import os
import platform
import re
import sys
from pathlib import Path
from urllib.parse import urljoin
import psutil
from reflex import constants
from reflex.config import get_config
from reflex.utils import console, path_ops
from reflex.utils.watch import AssetFolderWatch
def start_watching_assets_folder(root):
"""Start watching assets folder.
Args:
root: root path of the project.
"""
asset_watch = AssetFolderWatch(root)
asset_watch.start()
def detect_package_change(json_file_path: str) -> str:
"""Calculates the SHA-256 hash of a JSON file and returns it as a hexadecimal string.
Args:
json_file_path: The path to the JSON file to be hashed.
Returns:
str: The SHA-256 hash of the JSON file as a hexadecimal string.
Example:
>>> detect_package_change("package.json")
'a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6a7b8c9d0e1f2'
"""
with open(json_file_path, "r") as file:
json_data = json.load(file)
# Calculate the hash
json_string = json.dumps(json_data, sort_keys=True)
hash_object = hashlib.sha256(json_string.encode())
return hash_object.hexdigest()
def kill(proc_pid: int):
"""Kills a process and all its child processes.
Args:
proc_pid (int): The process ID of the process to be killed.
Example:
>>> kill(1234)
"""
process = psutil.Process(proc_pid)
for proc in process.children(recursive=True):
proc.kill()
process.kill()
def run_process_and_launch_url(run_command: list[str]):
"""Run the process and launch the URL.
Args:
run_command: The command to run.
"""
from reflex.utils import processes
json_file_path = os.path.join(constants.Dirs.WEB, "package.json")
last_hash = detect_package_change(json_file_path)
process = None
first_run = True
while True:
if process is None:
process = processes.new_process(
run_command, cwd=constants.Dirs.WEB, shell=constants.IS_WINDOWS
)
if process.stdout:
for line in processes.stream_logs("Starting frontend", process):
match = re.search(constants.Next.FRONTEND_LISTENING_REGEX, line)
if match:
if first_run:
url = match.group(1)
if get_config().frontend_path != "":
url = urljoin(url, get_config().frontend_path)
console.print(f"App running at: [bold green]{url}")
first_run = False
else:
console.print("New packages detected: Updating app...")
else:
new_hash = detect_package_change(json_file_path)
if new_hash != last_hash:
last_hash = new_hash
kill(process.pid)
process = None
break # for line in process.stdout
if process is not None:
break # while True
def run_frontend(root: Path, port: str):
"""Run the frontend.
Args:
root: The root path of the project.
port: The port to run the frontend on.
"""
from reflex.utils import prerequisites
# Start watching asset folder.
start_watching_assets_folder(root)
# validate dependencies before run
prerequisites.validate_frontend_dependencies(init=False)
# Run the frontend in development mode.
console.rule("[bold green]App Running")
os.environ["PORT"] = str(get_config().frontend_port if port is None else port)
run_process_and_launch_url([prerequisites.get_package_manager(), "run", "dev"]) # type: ignore
def run_frontend_prod(root: Path, port: str):
"""Run the frontend.
Args:
root: The root path of the project (to keep same API as run_frontend).
port: The port to run the frontend on.
"""
from reflex.utils import prerequisites
# Set the port.
os.environ["PORT"] = str(get_config().frontend_port if port is None else port)
# validate dependencies before run
prerequisites.validate_frontend_dependencies(init=False)
# Run the frontend in production mode.
console.rule("[bold green]App Running")
run_process_and_launch_url([prerequisites.get_package_manager(), "run", "prod"]) # type: ignore
def run_backend(
host: str,
port: int,
loglevel: constants.LogLevel = constants.LogLevel.ERROR,
):
"""Run the backend.
Args:
host: The app host
port: The app port
loglevel: The log level.
"""
import uvicorn
config = get_config()
app_module = f"reflex.app_module_for_backend:{constants.CompileVars.APP}"
# Create a .nocompile file to skip compile for backend.
if os.path.exists(constants.Dirs.WEB):
with open(constants.NOCOMPILE_FILE, "w"):
pass
# Run the backend in development mode.
uvicorn.run(
app=f"{app_module}.{constants.CompileVars.API}",
host=host,
port=port,
log_level=loglevel.value,
reload=True,
reload_dirs=[config.app_name],
)
def run_backend_prod(
host: str,
port: int,
loglevel: constants.LogLevel = constants.LogLevel.ERROR,
):
"""Run the backend.
Args:
host: The app host
port: The app port
loglevel: The log level.
"""
from reflex.utils import processes
num_workers = processes.get_num_workers()
config = get_config()
RUN_BACKEND_PROD = f"gunicorn --worker-class {config.gunicorn_worker_class} --preload --timeout {config.timeout} --log-level critical".split()
RUN_BACKEND_PROD_WINDOWS = f"uvicorn --timeout-keep-alive {config.timeout}".split()
app_module = f"reflex.app_module_for_backend:{constants.CompileVars.APP}"
command = (
[
*RUN_BACKEND_PROD_WINDOWS,
"--host",
host,
"--port",
str(port),
app_module,
]
if constants.IS_WINDOWS
else [
*RUN_BACKEND_PROD,
"--bind",
f"{host}:{port}",
"--threads",
str(num_workers),
f"{app_module}()",
]
)
command += [
"--log-level",
loglevel.value,
"--workers",
str(num_workers),
]
processes.new_process(
command,
run=True,
show_logs=True,
env={constants.SKIP_COMPILE_ENV_VAR: "yes"}, # skip compile for prod backend
)
def output_system_info():
"""Show system information if the loglevel is in DEBUG."""
if console._LOG_LEVEL > constants.LogLevel.DEBUG:
return
from reflex.utils import prerequisites
config = get_config()
try:
config_file = sys.modules[config.__module__].__file__
except Exception:
config_file = None
console.rule(f"System Info")
console.debug(f"Config file: {config_file!r}")
console.debug(f"Config: {config}")
dependencies = [
f"[Reflex {constants.Reflex.VERSION} with Python {platform.python_version()} (PATH: {sys.executable})]",
f"[Node {prerequisites.get_node_version()} (Expected: {constants.Node.VERSION}) (PATH:{path_ops.get_node_path()})]",
]
system = platform.system()
if system != "Windows":
dependencies.extend(
[
f"[FNM {prerequisites.get_fnm_version()} (Expected: {constants.Fnm.VERSION}) (PATH: {constants.Fnm.EXE})]",
f"[Bun {prerequisites.get_bun_version()} (Expected: {constants.Bun.VERSION}) (PATH: {config.bun_path})]",
],
)
else:
dependencies.append(
f"[FNM {prerequisites.get_fnm_version()} (Expected: {constants.Fnm.VERSION}) (PATH: {constants.Fnm.EXE})]",
)
if system == "Linux":
import distro # type: ignore
os_version = distro.name(pretty=True)
else:
os_version = platform.version()
dependencies.append(f"[OS {platform.system()} {os_version}]")
for dep in dependencies:
console.debug(f"{dep}")
console.debug(
f"Using package installer at: {prerequisites.get_install_package_manager()}" # type: ignore
)
console.debug(f"Using package executer at: {prerequisites.get_package_manager()}") # type: ignore
if system != "Windows":
console.debug(f"Unzip path: {path_ops.which('unzip')}")
def is_testing_env() -> bool:
"""Whether the app is running in a testing environment.
Returns:
True if the app is running in under pytest.
"""
return constants.PYTEST_CURRENT_TEST in os.environ