Skip to content

Commit

Permalink
some error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
masci committed Dec 28, 2023
1 parent a7cb3a9 commit 7abed2f
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 25 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/hayhooks/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -16,5 +16,5 @@ def hayhooks():


hayhooks.add_command(run)
hayhooks.add_command(serve)
hayhooks.add_command(deploy)
hayhooks.add_command(status)
18 changes: 18 additions & 0 deletions src/hayhooks/cli/deploy/__init__.py
Original file line number Diff line number Diff line change
@@ -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')}")
14 changes: 0 additions & 14 deletions src/hayhooks/cli/serve/__init__.py

This file was deleted.

20 changes: 18 additions & 2 deletions src/hayhooks/cli/status/__init__.py
Original file line number Diff line number Diff line change
@@ -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}")
4 changes: 2 additions & 2 deletions src/hayhooks/server/handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -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"]
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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}
5 changes: 4 additions & 1 deletion src/hayhooks/server/handlers/status.py
Original file line number Diff line number Diff line change
@@ -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}

0 comments on commit 7abed2f

Please sign in to comment.