Skip to content

Commit

Permalink
kraken: Connect jobs module with API V2
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoMario109 authored and patrickelectric committed Jun 12, 2024
1 parent a5480ab commit eae2970
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
2 changes: 2 additions & 0 deletions core/services/kraken/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
container_router_v2,
extension_router_v2,
index_router_v2,
jobs_router_v2,
manifest_router_v2,
)

Expand All @@ -29,6 +30,7 @@
application.include_router(index_router_v2)
application.include_router(container_router_v2)
application.include_router(extension_router_v2)
application.include_router(jobs_router_v2)
application.include_router(manifest_router_v2)

application = VersionedFastAPI(application, prefix_format="/v{major}.{minor}", enable_latest=True)
Expand Down
3 changes: 2 additions & 1 deletion core/services/kraken/api/v2/routers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .container import container_router_v2
from .extension import extension_router_v2
from .index import index_router_v2
from .jobs import jobs_router_v2
from .manifest import manifest_router_v2

__all__ = ["container_router_v2", "extension_router_v2", "index_router_v2", "manifest_router_v2"]
__all__ = ["container_router_v2", "extension_router_v2", "index_router_v2", "jobs_router_v2", "manifest_router_v2"]
58 changes: 58 additions & 0 deletions core/services/kraken/api/v2/routers/jobs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import uuid
from functools import wraps
from typing import Any, Callable, List, Tuple

from fastapi import APIRouter, Body, HTTPException, status
from fastapi_versioning import versioned_api_route

from jobs import JobsManager
from jobs.exceptions import JobNotFound
from jobs.models import Job, JobMethod

jobs_router_v2 = APIRouter(
prefix="/jobs",
tags=["jobs_v2"],
route_class=versioned_api_route(2, 0),
responses={status.HTTP_404_NOT_FOUND: {"description": "Not found"}},
)


def jobs_to_http_exception(endpoint: Callable[..., Any]) -> Callable[..., Any]:
@wraps(endpoint)
async def wrapper(*args: Tuple[Any], **kwargs: dict[str, Any]) -> Any:
try:
return await endpoint(*args, **kwargs)
except JobNotFound as error:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(error)) from error
except Exception as error:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(error)) from error

return wrapper


@jobs_router_v2.post("/{route:path}", status_code=status.HTTP_202_ACCEPTED)
@jobs_to_http_exception
async def create(
route: str, body: dict[str, Any] = Body(...), method: JobMethod = JobMethod.POST, retries: int = 5
) -> Job:
job = Job(id=str(uuid.uuid4()), route=route, method=method, body=body, retries=retries)
JobsManager.add(job)
return job


@jobs_router_v2.get("/", status_code=status.HTTP_200_OK)
@jobs_to_http_exception
async def fetch() -> List[Job]:
return JobsManager.get()


@jobs_router_v2.get("/{identifier}", status_code=status.HTTP_200_OK)
@jobs_to_http_exception
async def fetch_by_identifier(identifier: str) -> Job:
return JobsManager.get_by_identifier(identifier)


@jobs_router_v2.delete("/{identifier}", status_code=status.HTTP_204_NO_CONTENT)
@jobs_to_http_exception
async def delete(identifier: str) -> None:
JobsManager.delete(identifier)

0 comments on commit eae2970

Please sign in to comment.