From 7abed2f32daa569531f88034e4682431f4512961 Mon Sep 17 00:00:00 2001 From: Massimiliano Pippi Date: Thu, 28 Dec 2023 09:39:04 +0100 Subject: [PATCH] some error handling --- pyproject.toml | 2 +- src/hayhooks/cli/__init__.py | 4 ++-- src/hayhooks/cli/deploy/__init__.py | 18 +++++++++++++++++ src/hayhooks/cli/serve/__init__.py | 14 ------------- src/hayhooks/cli/status/__init__.py | 20 +++++++++++++++++-- src/hayhooks/server/handlers/__init__.py | 4 ++-- .../server/handlers/{serve.py => deploy.py} | 6 +++--- src/hayhooks/server/handlers/status.py | 5 ++++- 8 files changed, 48 insertions(+), 25 deletions(-) create mode 100644 src/hayhooks/cli/deploy/__init__.py delete mode 100644 src/hayhooks/cli/serve/__init__.py rename src/hayhooks/server/handlers/{serve.py => deploy.py} (95%) diff --git a/pyproject.toml b/pyproject.toml index e7d7d8e..e3d5cd0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "hatchling.build" [project] name = "hayhooks" dynamic = ["version"] -description = 'Grab and serve Haystack pipelines' +description = 'Grab and deploy Haystack pipelines' readme = "README.md" requires-python = ">=3.7" license = "Apache-2.0" diff --git a/src/hayhooks/cli/__init__.py b/src/hayhooks/cli/__init__.py index bd814ee..29d18a6 100644 --- a/src/hayhooks/cli/__init__.py +++ b/src/hayhooks/cli/__init__.py @@ -5,7 +5,7 @@ from hayhooks.__about__ import __version__ from hayhooks.cli.run import run -from hayhooks.cli.serve import serve +from hayhooks.cli.deploy import deploy from hayhooks.cli.status import status @@ -16,5 +16,5 @@ def hayhooks(): hayhooks.add_command(run) -hayhooks.add_command(serve) +hayhooks.add_command(deploy) hayhooks.add_command(status) diff --git a/src/hayhooks/cli/deploy/__init__.py b/src/hayhooks/cli/deploy/__init__.py new file mode 100644 index 0000000..7ce0fd5 --- /dev/null +++ b/src/hayhooks/cli/deploy/__init__.py @@ -0,0 +1,18 @@ +from pathlib import Path + +import click +import requests + + +@click.command() +@click.option('-n', '--name') +@click.argument('pipeline_file', type=click.File('r')) +def deploy(name, pipeline_file): + if name is None: + name = Path(pipeline_file.name).stem + resp = requests.post("http://localhost:1416/deploy", json={"name": name, "source_code": str(pipeline_file.read())}) + + if resp.status_code >= 400: + click.echo(f"Error deploying pipeline: {resp.json().get('detail')}") + else: + click.echo(f"Pipeline successfully deployed with name: {resp.json().get('name')}") diff --git a/src/hayhooks/cli/serve/__init__.py b/src/hayhooks/cli/serve/__init__.py deleted file mode 100644 index ba435ef..0000000 --- a/src/hayhooks/cli/serve/__init__.py +++ /dev/null @@ -1,14 +0,0 @@ -from pathlib import Path - -import click -import requests - - -@click.command() -@click.option('-n', '--name') -@click.argument('pipeline_file', type=click.File('r')) -def serve(name, pipeline_file): - if name is None: - name = Path(pipeline_file.name).stem - resp = requests.post("http://localhost:1416/serve", json={"name": name, "source_code": str(pipeline_file.read())}) - click.echo(resp.text) diff --git a/src/hayhooks/cli/status/__init__.py b/src/hayhooks/cli/status/__init__.py index 2983f4d..f5b95df 100644 --- a/src/hayhooks/cli/status/__init__.py +++ b/src/hayhooks/cli/status/__init__.py @@ -1,8 +1,24 @@ import click import requests +from requests.exceptions import ConnectionError @click.command() def status(): - r = requests.get("http://localhost:1416/status") - click.echo(r.status_code) + try: + r = requests.get("http://localhost:1416/status") + except ConnectionError: + click.echo("Hayhooks server is not responding. To start one, run `hayooks run`") + return + + if r.status_code >= 400: + body = r.json() + click.echo(f"Hayhooks server is unhealty: [{r.status_code}] {r. json().get('detail')}") + return + + click.echo("Hayhooks server is up and running.") + body = r.json() + if pipes := body.get("pipelines"): + click.echo("\nPipelines deployed:") + for p in pipes: + click.echo(f"- {p}") diff --git a/src/hayhooks/server/handlers/__init__.py b/src/hayhooks/server/handlers/__init__.py index 300bf9a..4821b88 100644 --- a/src/hayhooks/server/handlers/__init__.py +++ b/src/hayhooks/server/handlers/__init__.py @@ -1,5 +1,5 @@ -from hayhooks.server.handlers import serve +from hayhooks.server.handlers import deploy from hayhooks.server.handlers import status -__all__ = ["serve", "status"] +__all__ = ["deploy", "status"] diff --git a/src/hayhooks/server/handlers/serve.py b/src/hayhooks/server/handlers/deploy.py similarity index 95% rename from src/hayhooks/server/handlers/serve.py rename to src/hayhooks/server/handlers/deploy.py index b22092d..28be798 100644 --- a/src/hayhooks/server/handlers/serve.py +++ b/src/hayhooks/server/handlers/deploy.py @@ -11,8 +11,8 @@ class PipelineDefinition(BaseModel): source_code: str -@app.post("/serve") -async def serve(pipeline_def: PipelineDefinition): +@app.post("/deploy") +async def deploy(pipeline_def: PipelineDefinition): try: pipe = registry.add(pipeline_def.name, pipeline_def.source_code) except ValueError as e: @@ -67,4 +67,4 @@ async def pipeline_run(pipeline_run_req: PipelineRunRequest) -> JSONResponse: # app.openapi_schema = None app.setup() - return {"status": "ok"} + return {"name": pipeline_def.name} diff --git a/src/hayhooks/server/handlers/status.py b/src/hayhooks/server/handlers/status.py index 8797559..93f304e 100644 --- a/src/hayhooks/server/handlers/status.py +++ b/src/hayhooks/server/handlers/status.py @@ -1,6 +1,9 @@ +from fastapi import HTTPException from hayhooks.server import app +from hayhooks.server.pipelines import registry @app.get("/status") async def status(): - return {"status": "Up!"} + pipelines = registry.get_names() + return {"status": "Up!", "pipelines": pipelines}