-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathprocesses.py
More file actions
512 lines (422 loc) ยท 16.2 KB
/
processes.py
File metadata and controls
512 lines (422 loc) ยท 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
"""Process operations."""
from __future__ import annotations
import collections
import contextlib
import os
import signal
import socket
import subprocess
from collections.abc import Callable, Generator, Sequence
from concurrent import futures
from contextlib import closing
from pathlib import Path
from typing import Any, Literal, overload
import click
import rich.markup
from redis.exceptions import RedisError
from rich.progress import Progress
from reflex import constants
from reflex.environment import environment
from reflex.utils import console, path_ops, prerequisites
from reflex.utils.registry import get_npm_registry
def kill(pid: int):
"""Kill a process.
Args:
pid: The process ID.
"""
os.kill(pid, signal.SIGTERM)
def get_num_workers() -> int:
"""Get the number of backend worker processes.
Raises:
Exit: If unable to connect to Redis.
Returns:
The number of backend worker processes.
"""
if (redis_client := prerequisites.get_redis_sync()) is None:
return 1
try:
redis_client.ping()
except RedisError as re:
console.error(f"Unable to connect to Redis: {re}")
raise click.exceptions.Exit(1) from re
return (os.cpu_count() or 1) * 2 + 1
def _can_bind_at_port(
address_family: socket.AddressFamily | int, address: str, port: int
) -> bool:
"""Check if a given address and port are responsive.
Args:
address_family: The address family (e.g., socket.AF_INET or socket.AF_INET6).
address: The address to check.
port: The port to check.
Returns:
Whether the address and port are responsive.
"""
try:
with closing(socket.socket(address_family, socket.SOCK_STREAM)) as sock:
sock.bind((address, port))
except PermissionError:
return False
except OSError:
return False
return True
def is_process_on_port(port: int) -> bool:
"""Check if a process is running on the given port.
Args:
port: The port.
Returns:
Whether a process is running on the given port.
"""
return not _can_bind_at_port( # Test IPv4 localhost (127.0.0.1)
socket.AF_INET, "127.0.0.1", port
) or not _can_bind_at_port(
socket.AF_INET6, "::1", port
) # Test IPv6 localhost (::1)
def change_port(port: int, _type: str) -> int:
"""Change the port.
Args:
port: The port.
_type: The type of the port.
Returns:
The new port.
"""
new_port = port + 1
if is_process_on_port(new_port):
return change_port(new_port, _type)
console.info(
f"The {_type} will run on port [bold underline]{new_port}[/bold underline]."
)
return new_port
def handle_port(service_name: str, port: int, auto_increment: bool) -> int:
"""Change port if the specified port is in use and is not explicitly specified as a CLI arg or config arg.
Otherwise tell the user the port is in use and exit the app.
Args:
service_name: The frontend or backend.
port: The provided port.
auto_increment: Whether to automatically increment the port.
Returns:
The port to run the service on.
Raises:
Exit:when the port is in use.
"""
console.debug(f"Checking if {service_name.capitalize()} port: {port} is in use.")
if not is_process_on_port(port):
console.debug(f"{service_name.capitalize()} port: {port} is not in use.")
return port
if auto_increment:
return change_port(port, service_name)
console.error(f"{service_name.capitalize()} port: {port} is already in use.")
raise click.exceptions.Exit
@overload
def new_process(
args: str | list[str] | list[str | None] | list[str | Path | None],
run: Literal[False] = False,
show_logs: bool = False,
**kwargs,
) -> subprocess.Popen[str]: ...
@overload
def new_process(
args: str | list[str] | list[str | None] | list[str | Path | None],
run: Literal[True],
show_logs: bool = False,
**kwargs,
) -> subprocess.CompletedProcess[str]: ...
def new_process(
args: str | list[str] | list[str | None] | list[str | Path | None],
run: bool = False,
show_logs: bool = False,
**kwargs,
) -> subprocess.CompletedProcess[str] | subprocess.Popen[str]:
"""Wrapper over subprocess.Popen to unify the launch of child processes.
Args:
args: A string, or a sequence of program arguments.
run: Whether to run the process to completion.
show_logs: Whether to show the logs of the process.
**kwargs: Kwargs to override default wrap values to pass to subprocess.Popen as arguments.
Returns:
Execute a child program in a new process.
Raises:
Exit: When attempting to run a command with a None value.
"""
# Check for invalid command first.
non_empty_args = list(filter(None, args)) if isinstance(args, list) else [args]
if isinstance(args, list) and len(non_empty_args) != len(args):
console.error(f"Invalid command: {args}")
raise click.exceptions.Exit(1)
path_env: str = os.environ.get("PATH", "")
# Add node_bin_path to the PATH environment variable.
if not environment.REFLEX_BACKEND_ONLY.get():
node_bin_path = path_ops.get_node_bin_path()
if node_bin_path:
path_env = os.pathsep.join([str(node_bin_path), path_env])
env: dict[str, str] = {
**os.environ,
"PATH": path_env,
**kwargs.pop("env", {}),
}
kwargs = {
"env": env,
"stderr": None if show_logs else subprocess.STDOUT,
"stdout": None if show_logs else subprocess.PIPE,
"universal_newlines": True,
"encoding": "UTF-8",
"errors": "replace", # Avoid UnicodeDecodeError in unknown command output
**kwargs,
}
console.debug(f"Running command: {non_empty_args}")
def subprocess_p_open(args: subprocess._CMD, **kwargs):
return subprocess.Popen(args, **kwargs)
fn: Callable[..., subprocess.CompletedProcess[str] | subprocess.Popen[str]] = (
subprocess.run if run else subprocess_p_open
)
return fn(non_empty_args, **kwargs)
@contextlib.contextmanager
def run_concurrently_context(
*fns: Callable[..., Any] | tuple[Callable[..., Any], ...],
) -> Generator[list[futures.Future], None, None]:
"""Run functions concurrently in a thread pool.
Args:
*fns: The functions to run.
Yields:
The futures for the functions.
"""
# If no functions are provided, yield an empty list and return.
if not fns:
yield []
return
# Convert the functions to tuples.
fns = tuple(fn if isinstance(fn, tuple) else (fn,) for fn in fns)
# Run the functions concurrently.
executor = None
try:
executor = futures.ThreadPoolExecutor(max_workers=len(fns))
# Submit the tasks.
tasks = [executor.submit(*fn) for fn in fns]
# Yield control back to the main thread while tasks are running.
yield tasks
# Get the results in the order completed to check any exceptions.
for task in futures.as_completed(tasks):
# if task throws something, we let it bubble up immediately
task.result()
finally:
# Shutdown the executor
if executor:
executor.shutdown(wait=False)
def run_concurrently(*fns: Callable | tuple) -> None:
"""Run functions concurrently in a thread pool.
Args:
*fns: The functions to run.
"""
with run_concurrently_context(*fns):
pass
def stream_logs(
message: str,
process: subprocess.Popen,
progress: Progress | None = None,
suppress_errors: bool = False,
analytics_enabled: bool = False,
prior_logs: tuple[tuple[str, ...], ...] = (),
):
"""Stream the logs for a process.
Args:
message: The message to display.
process: The process.
progress: The ongoing progress bar if one is being used.
suppress_errors: If True, do not exit if errors are encountered (for fallback).
analytics_enabled: Whether analytics are enabled for this command.
prior_logs: The logs of the prior processes that have been run.
Yields:
The lines of the process output.
Raises:
Exit: If the process failed.
ValueError: If the process stdout pipe is closed, but the process remains running.
"""
from reflex.utils import telemetry
# Store the tail of the logs.
logs = collections.deque(maxlen=512)
with process:
console.debug(message, progress=progress)
if process.stdout is None:
return
try:
for line in process.stdout:
console.debug(rich.markup.escape(line), end="", progress=progress)
logs.append(line)
yield line
except ValueError:
# The stream we were reading has been closed,
if process.poll() is None:
# But if the process is still running that is weird.
raise
# If the process exited, break out of the loop for post processing.
# Check if the process failed (not printing the logs for SIGINT).
# Windows uvicorn bug
# https://github.com/reflex-dev/reflex/issues/2335
# 130 is the exit code that react router returns when it is interrupted by a signal.
accepted_return_codes = [0, -2, 15, 130] if constants.IS_WINDOWS else [0, -2, 130]
if process.returncode not in accepted_return_codes and not suppress_errors:
console.error(f"{message} failed with exit code {process.returncode}")
if "".join(logs).count("CERT_HAS_EXPIRED") > 0:
bunfig = prerequisites.get_web_dir() / constants.Bun.CONFIG_PATH
npm_registry_line = next(
(
line
for line in bunfig.read_text().splitlines()
if line.startswith("registry")
),
None,
)
if not npm_registry_line or "=" not in npm_registry_line:
npm_registry = get_npm_registry()
else:
npm_registry = npm_registry_line.split("=")[1].strip()
console.error(
f"Failed to fetch securely from [bold]{npm_registry}[/bold]. Please check your network connection. "
"You can try running the command again or changing the registry by setting the "
"NPM_CONFIG_REGISTRY environment variable. If TLS is the issue, and you know what "
"you are doing, you can disable it by setting the SSL_NO_VERIFY environment variable."
)
raise click.exceptions.Exit(1)
for set_of_logs in (*prior_logs, tuple(logs)):
for line in set_of_logs:
console.error(line, end="")
console.error("\n\n")
if analytics_enabled:
telemetry.send("error", context=message)
console.error("Run with [bold]--loglevel debug [/bold] for the full log.")
raise click.exceptions.Exit(1)
def show_logs(message: str, process: subprocess.Popen):
"""Show the logs for a process.
Args:
message: The message to display.
process: The process.
"""
for _ in stream_logs(message, process):
pass
def show_status(
message: str,
process: subprocess.Popen,
suppress_errors: bool = False,
analytics_enabled: bool = False,
prior_logs: tuple[tuple[str, ...], ...] = (),
) -> list[str]:
"""Show the status of a process.
Args:
message: The initial message to display.
process: The process.
suppress_errors: If True, do not exit if errors are encountered (for fallback).
analytics_enabled: Whether analytics are enabled for this command.
prior_logs: The logs of the prior processes that have been run.
Returns:
The lines of the process output.
"""
lines = []
with console.status(message) as status:
for line in stream_logs(
message,
process,
suppress_errors=suppress_errors,
analytics_enabled=analytics_enabled,
prior_logs=prior_logs,
):
status.update(f"{message} {line}")
lines.append(line)
return lines
def show_progress(message: str, process: subprocess.Popen, checkpoints: list[str]):
"""Show a progress bar for a process.
Args:
message: The message to display.
process: The process.
checkpoints: The checkpoints to advance the progress bar.
"""
# Iterate over the process output.
with console.progress() as progress:
task = progress.add_task(f"{message}: ", total=len(checkpoints))
for line in stream_logs(message, process, progress=progress):
# Check for special strings and update the progress bar.
special_string = checkpoints[0]
if special_string in line:
progress.update(task, advance=1)
checkpoints.pop(0)
if not checkpoints:
break
def atexit_handler():
"""Display a custom message with the current time when exiting an app."""
console.log("Reflex app stopped.")
def get_command_with_loglevel(command: list[str]) -> list[str]:
"""Add the right loglevel flag to the designated command.
npm uses --loglevel <level>, Bun doesn't use the --loglevel flag and
runs in debug mode by default.
Args:
command:The command to add loglevel flag.
Returns:
The updated command list
"""
npm_path = path_ops.get_npm_path()
npm_path = str(npm_path) if npm_path else None
if command[0] == npm_path:
return [*command, "--loglevel", "silly"]
return command
def run_process_with_fallbacks(
args: list[str],
*,
show_status_message: str,
fallbacks: str | Sequence[str] | Sequence[Sequence[str]] | None = None,
analytics_enabled: bool = False,
prior_logs: tuple[tuple[str, ...], ...] = (),
**kwargs,
):
"""Run subprocess and retry using fallback command if initial command fails.
Args:
args: A string, or a sequence of program arguments.
show_status_message: The status message to be displayed in the console.
fallbacks: The fallback command to run if the initial command fails.
analytics_enabled: Whether analytics are enabled for this command.
prior_logs: The logs of the prior processes that have been run.
**kwargs: Kwargs to pass to new_process function.
"""
process = new_process(get_command_with_loglevel(args), **kwargs)
if not fallbacks:
# No fallback given, or this _is_ the fallback command.
show_status(
show_status_message,
process,
analytics_enabled=analytics_enabled,
prior_logs=prior_logs,
)
else:
# Suppress errors for initial command, because we will try to fallback
logs = show_status(show_status_message, process, suppress_errors=True)
current_fallback = fallbacks[0] if not isinstance(fallbacks, str) else fallbacks
next_fallbacks = fallbacks[1:] if not isinstance(fallbacks, str) else None
if process.returncode != 0:
# retry with fallback command.
fallback_with_args = (
[current_fallback, *args[1:]]
if isinstance(current_fallback, str)
else [*current_fallback, *args[1:]]
)
console.warn(
f"There was an error running command: {args}. Falling back to: {fallback_with_args}."
)
run_process_with_fallbacks(
fallback_with_args,
show_status_message=show_status_message,
fallbacks=next_fallbacks,
analytics_enabled=analytics_enabled,
prior_logs=(*prior_logs, tuple(logs)),
**kwargs,
)
def execute_command_and_return_output(command: str) -> str | None:
"""Execute a command and return the output.
Args:
command: The command to run.
Returns:
The output of the command.
"""
try:
return subprocess.check_output(command, shell=True).decode().strip()
except subprocess.SubprocessError as err:
console.error(
f"The command `{command}` failed with error: {err}. This will return None."
)
return None