Skip to content
This repository was archived by the owner on Nov 13, 2024. It is now read-only.

Commit 020a3de

Browse files
CLI and server bug fixes on Windows (#170)
* [cli] Bug fix - used `os.path.join()` instead of 'urljoin()' That created an error on Windows * [server] Support `canopy stop` on Windows Two bugs: 1. Trying to `pgrep gunicorn` on Windows, even though windows doesn't have `pgrep` and Gunicorn isn't supported on windows. 2. In windows we need to send the `CTRL_C` signal, which has a totally different implmentation behind it than GITINT * [cli] Improve chat warnning message Since there is no Gunicorn on windows, it doesn't make sense to tell the users to run it * [cli] Bug fix - CLI couldn't open server routes on Windows The CLI used the url `http://0.0.0.0:8000` by default, which is not supported on windows. I replaced it with `http://localhost:8000`. * Don't use tenerary if, so mypy will be happy What can you do... * [cli] Bug fix - still using os.path.join() for url Caught in PR #170 review. * linter
1 parent 3461429 commit 020a3de

File tree

2 files changed

+44
-33
lines changed

2 files changed

+44
-33
lines changed

src/canopy_cli/cli.py

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ def cli(ctx):
171171

172172

173173
@cli.command(help="Check if canopy server is running and healthy.")
174-
@click.option("--url", default="http://0.0.0.0:8000",
175-
help="Canopy's server url. Defaults to http://0.0.0.0:8000")
174+
@click.option("--url", default="http://localhost:8000",
175+
help="Canopy's server url. Defaults to http://localhost:8000")
176176
def health(url):
177177
check_server_health(url)
178178
click.echo(click.style("Canopy server is healthy!", fg="green"))
@@ -432,8 +432,8 @@ def _chat(
432432
help="Print additional debugging information")
433433
@click.option("--rag/--no-rag", default=True,
434434
help="Compare RAG-infused Chatbot with vanilla LLM",)
435-
@click.option("--chat-server-url", default="http://0.0.0.0:8000",
436-
help="URL of the Canopy server to use. Defaults to http://0.0.0.0:8000")
435+
@click.option("--chat-server-url", default="http://localhost:8000",
436+
help="URL of the Canopy server to use. Defaults to http://localhost:8000")
437437
def chat(chat_server_url, rag, debug, stream):
438438
check_server_health(chat_server_url)
439439
note_msg = (
@@ -488,7 +488,7 @@ def chat(chat_server_url, rag, debug, stream):
488488
history=history_with_pinecone,
489489
message=message,
490490
stream=stream,
491-
api_base=os.path.join(chat_server_url, "context"),
491+
api_base=urljoin(chat_server_url, "/context"),
492492
print_debug_info=debug,
493493
)
494494

@@ -541,12 +541,18 @@ def start(host: str, port: str, reload: bool,
541541
config: Optional[str], index_name: Optional[str]):
542542
note_msg = (
543543
"🚨 Note 🚨\n"
544-
"For debugging only. To run the Canopy server in production, run the command:"
544+
"For debugging only. To run the Canopy server in production "
545+
)
546+
msg_suffix = (
547+
"run the command:"
545548
"\n"
546549
"gunicorn canopy_server.app:app --worker-class uvicorn.workers.UvicornWorker "
547550
f"--bind {host}:{port} --workers <num_workers>"
551+
) if os.name != "nt" else (
552+
# TODO: Replace with proper instructions once we have a Dockerfile
553+
"please use Docker with a Gunicorn server."
548554
)
549-
for c in note_msg:
555+
for c in note_msg + msg_suffix:
550556
click.echo(click.style(c, fg="red"), nl=False)
551557
time.sleep(0.01)
552558
click.echo()
@@ -574,31 +580,32 @@ def start(host: str, port: str, reload: bool,
574580
"""
575581
)
576582
)
577-
@click.option("url", "--url", default="http://0.0.0.0:8000",
578-
help="URL of the Canopy server to use. Defaults to http://0.0.0.0:8000")
583+
@click.option("url", "--url", default="http://localhost:8000",
584+
help="URL of the Canopy server to use. Defaults to http://localhost:8000")
579585
def stop(url):
580-
# Check if the server was started using Gunicorn
581-
res = subprocess.run(["pgrep", "-f", "gunicorn canopy_server.app:app"],
582-
capture_output=True)
583-
output = res.stdout.decode("utf-8").split()
584-
585-
# If Gunicorn was used, kill all Gunicorn processes
586-
if output:
587-
msg = ("It seems that Canopy server was launched using Gunicorn.\n"
588-
"Do you want to kill all Gunicorn processes?")
589-
click.confirm(click.style(msg, fg="red"), abort=True)
590-
try:
591-
subprocess.run(["pkill", "-f", "gunicorn canopy_server.app:app"],
592-
check=True)
593-
except subprocess.CalledProcessError:
586+
if os.name != "nt":
587+
# Check if the server was started using Gunicorn
588+
res = subprocess.run(["pgrep", "-f", "gunicorn canopy_server.app:app"],
589+
capture_output=True)
590+
output = res.stdout.decode("utf-8").split()
591+
592+
# If Gunicorn was used, kill all Gunicorn processes
593+
if output:
594+
msg = ("It seems that Canopy server was launched using Gunicorn.\n"
595+
"Do you want to kill all Gunicorn processes?")
596+
click.confirm(click.style(msg, fg="red"), abort=True)
594597
try:
595-
[os.kill(int(pid), signal.SIGINT) for pid in output]
596-
except OSError:
597-
msg = (
598-
"Could not kill Gunicorn processes. Please kill them manually."
599-
f"Found process ids: {output}"
600-
)
601-
raise CLIError(msg)
598+
subprocess.run(["pkill", "-f", "gunicorn canopy_server.app:app"],
599+
check=True)
600+
except subprocess.CalledProcessError:
601+
try:
602+
[os.kill(int(pid), signal.SIGINT) for pid in output]
603+
except OSError:
604+
msg = (
605+
"Could not kill Gunicorn processes. Please kill them manually."
606+
f"Found process ids: {output}"
607+
)
608+
raise CLIError(msg)
602609

603610
try:
604611
res = requests.get(urljoin(url, "/shutdown"))
@@ -619,8 +626,8 @@ def stop(url):
619626
"""
620627
)
621628
)
622-
@click.option("--url", default="http://0.0.0.0:8000",
623-
help="Canopy's server url. Defaults to http://0.0.0.0:8000")
629+
@click.option("--url", default="http://localhost:8000",
630+
help="Canopy's server url. Defaults to http://localhost:8000")
624631
def api_docs(url):
625632
import webbrowser
626633

src/canopy_server/app.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,11 @@ async def shutdown() -> ShutdownResponse:
255255
status_code=500,
256256
detail="Failed to locate parent process. Cannot shutdown server.",
257257
)
258-
os.kill(pid, signal.SIGINT)
258+
if sys.platform == 'win32':
259+
kill_signal = signal.CTRL_C_EVENT
260+
else:
261+
kill_signal = signal.SIGINT
262+
os.kill(pid, kill_signal)
259263
return ShutdownResponse()
260264

261265

0 commit comments

Comments
 (0)