hotfix(cli): replace raw subprocess tracebacks with styled error messages and fix restart race condition #96
@@ -1,5 +1,8 @@
|
|||||||
import subprocess
|
import subprocess
|
||||||
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
import click
|
||||||
import typer
|
import typer
|
||||||
|
|
||||||
args = ["qs", "-c", "zshell"]
|
args = ["qs", "-c", "zshell"]
|
||||||
@@ -9,40 +12,65 @@ app = typer.Typer()
|
|||||||
|
|
||||||
@app.command()
|
@app.command()
|
||||||
def kill():
|
def kill():
|
||||||
subprocess.run(args + ["kill"], check=False)
|
result = subprocess.run(args + ["kill"], capture_output=True)
|
||||||
|
if result.returncode != 0:
|
||||||
|
raise click.ClickException("No running instance to kill.")
|
||||||
|
sys.stderr.write(result.stderr.decode())
|
||||||
|
|
||||||
|
|
||||||
@app.command()
|
@app.command()
|
||||||
def start(no_daemon: bool = False):
|
def start(no_daemon: bool = False):
|
||||||
subprocess.run(args + ["-n"] + ([] if no_daemon else ["-d"]), check=True)
|
check = subprocess.run(args + ["ipc"] + ["show"], capture_output=True)
|
||||||
|
zach marked this conversation as resolved
Outdated
|
|||||||
|
if check.returncode == 0:
|
||||||
|
raise click.ClickException("An instance of this configuration is already running.")
|
||||||
|
result = subprocess.run(args + ["-n"] + ([] if no_daemon else ["-d"]), capture_output=True)
|
||||||
|
if result.returncode != 0:
|
||||||
|
raise click.ClickException(result.stderr.decode().strip())
|
||||||
|
sys.stderr.write(result.stderr.decode())
|
||||||
|
|
||||||
|
|
||||||
@app.command()
|
@app.command()
|
||||||
def restart(no_daemon: bool = False):
|
def restart(no_daemon: bool = False):
|
||||||
subprocess.run(args + ["kill"], check=False)
|
subprocess.run(args + ["kill"])
|
||||||
for _ in range(50):
|
for _ in range(50):
|
||||||
|
AramJonghu marked this conversation as resolved
Outdated
zach
commented
This seems ugly. Personally think we should just set a sleep timer of ~5 seconds, then run code. This seems ugly. Personally think we should just set a sleep timer of ~5 seconds, then run code.
AramJonghu
commented
Is that not an messier implementation? A user would want a restart to be quick (quicker than running the kill and start command separately). 2.5 or higher stock sleep timer would make little sense. time.monotonic() could be used to hide the ugliness, but same logic/idea. Is that not an messier implementation? A user would want a restart to be quick (quicker than running the kill and start command separately). 2.5 or higher stock sleep timer would make little sense.
time.monotonic() could be used to hide the ugliness, but same logic/idea.
zach
commented
Running a loop would keep a CPU thread busy until it's able to launch the shell, while a sleep timer wouldn't. I don't really see much benefit in looping upwards of 50 times just to make sure we can run the start command exactly when it's available. Running a loop would keep a CPU thread busy until it's able to launch the shell, while a sleep timer wouldn't. I don't really see much benefit in looping upwards of 50 times just to make sure we can run the start command exactly when it's available.
AramJonghu
commented
Is that not okay for a rare command as restart? We could change the loop to check every 250ms instead, so it is 10 checks instead of 50. Is that not okay for a rare command as restart?
We could change the loop to check every 250ms instead, so it is 10 checks instead of 50.
zach
commented
That's a good middle ground I think, agreed. That's a good middle ground I think, agreed.
AramJonghu
commented
Changed it. Changed it.
|
|||||||
result = subprocess.run(args + ["kill"], capture_output=True)
|
result = subprocess.run(args + ["kill"], capture_output=True)
|
||||||
if result.returncode == 255:
|
if result.returncode == 255:
|
||||||
break
|
break
|
||||||
time.sleep(0.05)
|
time.sleep(0.05)
|
||||||
subprocess.run(args + ["-n"] + ([] if no_daemon else ["-d"]), check=True)
|
result = subprocess.run(args + ["-n"] + ([] if no_daemon else ["-d"]), capture_output=True)
|
||||||
|
zach marked this conversation as resolved
Outdated
zach
commented
Here we can just call Here we can just call `start()`, right? Not sure if `Typer` allows calling command functions.
AramJonghu
commented
It was to have separate tests. Simpler in my eyes (at least at time of writing). I can simplify it by having a It was to have separate tests. Simpler in my eyes (at least at time of writing). I can simplify it by having a `start_instance()` that start uses outright, and is reused by restart. This could satisfy testing and reuse of code, and perhaps future code changes.
|
|||||||
|
if result.returncode != 0:
|
||||||
|
raise click.ClickException(result.stderr.decode().strip())
|
||||||
|
sys.stderr.write(result.stderr.decode())
|
||||||
|
|
||||||
|
|
||||||
@app.command()
|
@app.command()
|
||||||
def show():
|
def show():
|
||||||
subprocess.run(args + ["ipc"] + ["show"], check=True)
|
result = subprocess.run(args + ["ipc"] + ["show"], capture_output=True)
|
||||||
|
if result.returncode != 0:
|
||||||
|
raise click.ClickException(result.stderr.decode().strip())
|
||||||
|
sys.stderr.write(result.stderr.decode())
|
||||||
|
|
||||||
|
|
||||||
@app.command()
|
@app.command()
|
||||||
def log():
|
def log():
|
||||||
subprocess.run(args + ["log"], check=True)
|
result = subprocess.run(args + ["log"], capture_output=True)
|
||||||
|
if result.returncode != 0:
|
||||||
|
raise click.ClickException(result.stderr.decode().strip())
|
||||||
|
sys.stdout.write(result.stdout.decode())
|
||||||
|
sys.stderr.write(result.stderr.decode())
|
||||||
|
|
||||||
|
|
||||||
@app.command()
|
@app.command()
|
||||||
def lock():
|
def lock():
|
||||||
subprocess.run(args + ["ipc"] + ["call"] + ["lock"] + ["lock"], check=True)
|
result = subprocess.run(args + ["ipc"] + ["call"] + ["lock"] + ["lock"], capture_output=True)
|
||||||
|
if result.returncode != 0:
|
||||||
|
raise click.ClickException(result.stderr.decode().strip())
|
||||||
|
sys.stderr.write(result.stderr.decode())
|
||||||
|
|
||||||
|
|
||||||
@app.command()
|
@app.command()
|
||||||
def call(target: str, method: str, method_args: list[str] = typer.Argument(None)):
|
def call(target: str, method: str, method_args: list[str] = typer.Argument(None)):
|
||||||
subprocess.run(args + ["ipc"] + ["call"] + [target] + [method] + (method_args or []), check=True)
|
result = subprocess.run(args + ["ipc"] + ["call"] + [target] + [method] + (method_args or []), capture_output=True)
|
||||||
|
if result.returncode != 0:
|
||||||
|
raise click.ClickException(result.stderr.decode().strip())
|
||||||
|
sys.stderr.write(result.stderr.decode())
|
||||||
|
|||||||
The start command should not need to run
qs -c zshell ipc call, IPC calls are used to trigger states and functions within the QML code. The-nflag already checks if there's a running instance of the shell.Was not aware of this. Remaining functions use ipc, which is why I did not bat an eye. Resolving now.
Change occurred since I wanted an error block such as in the screenshot. But with that info, ipc is indeed not needed in the code.