Skip to content

Commit 96086bc

Browse files
authored
change error connecting to backend when backend is cold started (#4814)
* change error connecting to backend when backend is cold started * do as simon wanted * prefix with REFLEX
1 parent 6e4522c commit 96086bc

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

reflex/components/core/banner.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from reflex.components.radix.themes.layout.flex import Flex
1818
from reflex.components.radix.themes.typography.text import Text
1919
from reflex.components.sonner.toast import Toaster, ToastProps
20+
from reflex.config import environment
2021
from reflex.constants import Dirs, Hooks, Imports
2122
from reflex.constants.compiler import CompileVars
2223
from reflex.utils.imports import ImportVar
@@ -109,9 +110,41 @@ def add_hooks(self) -> list[str | Var]:
109110
id=toast_id,
110111
) # pyright: ignore [reportCallIssue]
111112

113+
if environment.REFLEX_DOES_BACKEND_COLD_START.get():
114+
loading_message = Var.create("Backend is starting.")
115+
backend_is_loading_toast_var = Var(
116+
f"toast.loading({loading_message!s}, {{...toast_props, description: '', closeButton: false, onDismiss: () => setUserDismissed(true)}},)"
117+
)
118+
backend_is_not_responding = Var.create("Backend is not responding.")
119+
backend_is_down_toast_var = Var(
120+
f"toast.error({backend_is_not_responding!s}, {{...toast_props, description: '', onDismiss: () => setUserDismissed(true)}},)"
121+
)
122+
toast_var = Var(
123+
f"""
124+
if (waitedForBackend) {{
125+
{backend_is_down_toast_var!s}
126+
}} else {{
127+
{backend_is_loading_toast_var!s};
128+
}}
129+
setTimeout(() => {{
130+
if ({has_too_many_connection_errors!s}) {{
131+
setWaitedForBackend(true);
132+
}}
133+
}}, {environment.REFLEX_BACKEND_COLD_START_TIMEOUT.get() * 1000});
134+
"""
135+
)
136+
else:
137+
loading_message = Var.create(
138+
f"Cannot connect to server: {connection_error}."
139+
)
140+
toast_var = Var(
141+
f"toast.error({loading_message!s}, {{...toast_props, onDismiss: () => setUserDismissed(true)}},)"
142+
)
143+
112144
individual_hooks = [
113145
f"const toast_props = {LiteralVar.create(props)!s};",
114146
"const [userDismissed, setUserDismissed] = useState(false);",
147+
"const [waitedForBackend, setWaitedForBackend] = useState(false);",
115148
FunctionStringVar(
116149
"useEffect",
117150
_var_data=VarData(
@@ -127,10 +160,7 @@ def add_hooks(self) -> list[str | Var]:
127160
() => {{
128161
if ({has_too_many_connection_errors!s}) {{
129162
if (!userDismissed) {{
130-
toast.error(
131-
`Cannot connect to server: ${{{connection_error}}}.`,
132-
{{...toast_props, onDismiss: () => setUserDismissed(true)}},
133-
)
163+
{toast_var!s}
134164
}}
135165
}} else {{
136166
toast.dismiss("{toast_id}");
@@ -139,7 +169,7 @@ def add_hooks(self) -> list[str | Var]:
139169
}}
140170
"""
141171
),
142-
LiteralArrayVar.create([connect_errors]),
172+
LiteralArrayVar.create([connect_errors, Var("waitedForBackend")]),
143173
),
144174
]
145175

reflex/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,12 @@ class EnvironmentVariables:
713713
# Paths to exclude from the hot reload. Takes precedence over include paths. Separated by a colon.
714714
REFLEX_HOT_RELOAD_EXCLUDE_PATHS: EnvVar[List[Path]] = env_var([])
715715

716+
# Enables different behavior for when the backend would do a cold start if it was inactive.
717+
REFLEX_DOES_BACKEND_COLD_START: EnvVar[bool] = env_var(False)
718+
719+
# The timeout for the backend to do a cold start in seconds.
720+
REFLEX_BACKEND_COLD_START_TIMEOUT: EnvVar[int] = env_var(10)
721+
716722
# Used by flexgen to enumerate the pages.
717723
REFLEX_ADD_ALL_ROUTES_ENDPOINT: EnvVar[bool] = env_var(False)
718724

0 commit comments

Comments
 (0)