Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/checkhealth endpoint with hazelcast #13

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@ jobs:
run: |
poetry run flake8 .

- name: Start cache service
run: docker compose up -d --build cache

- name: Test with pytest
run: poetry run pytest
9 changes: 9 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@
from pyaml_env import parse_config


class CacheSettings(BaseModel):
class CacheParams(BaseModel):
host: str
port: int

hazelcast: CacheParams


class Settings(BaseModel):
class PyrisSettings(BaseModel):
api_key: str
cache: CacheSettings
llm: dict

pyris: PyrisSettings
Expand Down
7 changes: 7 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@

from app.routes.messages import router as messages_router
from app.routes.health import router as health_router
from app.services.hazelcast_client import hazelcast_client

app = FastAPI(default_response_class=ORJSONResponse)


@app.on_event("shutdown")
async def shutdown():
hazelcast_client.shutdown()


app.include_router(messages_router)
app.include_router(health_router)
38 changes: 37 additions & 1 deletion app/services/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import datetime, timedelta
from typing import Union, Any
from pydantic import BaseModel
from app.services.hazelcast_client import hazelcast_client


class CacheStoreInterface(ABC):
Expand Down Expand Up @@ -115,4 +116,39 @@ def delete(self, name: str):
del self._cache[name]


cache_store = InMemoryCacheStore()
class HazelcastCacheStore(CacheStoreInterface):
def __init__(self):
self._cache = hazelcast_client.get_map("cache_store").blocking()

def get(self, name: str) -> Union[Any, None]:
return self._cache.get(name)

def set(self, name: str, value, ex: Union[int, None] = None):
self._cache.put(name, value, ex)

def expire(self, name: str, ex: int):
self._cache.set_ttl(name, ex)

def incr(self, name: str) -> int:
flag = False
value = 0
while not flag:
value = self._cache.get(name)
if value is None:
self._cache.set(name, 1)
return 1
if not isinstance(value, int):
raise TypeError("value is not an integer")

flag = self._cache.replace_if_same(name, value, value + 1)

return value + 1

def flushdb(self):
self._cache.clear()

def delete(self, name: str):
self._cache.remove(name)


cache_store = HazelcastCacheStore()
9 changes: 9 additions & 0 deletions app/services/hazelcast_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import hazelcast
from app.config import settings

hazelcast_client = hazelcast.HazelcastClient(
cluster_members=[
f"{settings.pyris.cache.hazelcast.host}:\
{settings.pyris.cache.hazelcast.port}"
],
)
4 changes: 4 additions & 0 deletions application-docker.test.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
pyris:
api_key: secret
cache:
hazelcast:
host: cache
port: 5701
llm:
GPT35_TURBO:
model:
Expand Down
4 changes: 4 additions & 0 deletions application.example.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
pyris:
api_key: secret
cache:
hazelcast:
host: localhost
port: 5701
llm:
GPT35_TURBO:
model:
Expand Down
4 changes: 4 additions & 0 deletions application.test.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
pyris:
api_key: secret
cache:
hazelcast:
host: localhost
port: 5701
llm:
GPT35_TURBO:
model:
Expand Down
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ services:
dockerfile: Dockerfile.development
environment:
- DOCKER=1
depends_on:
- cache
ports:
- "8000:8000"
volumes:
Expand All @@ -18,5 +20,13 @@ services:
stdin_open: true
tty: true

cache:
container_name: pyris-cache
image: hazelcast/hazelcast:5.3.1
networks:
- pyris
ports:
- '5701:5701'

networks:
pyris:
16 changes: 15 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ black = "^23.3.0"
flake8 = "^6.0.0"
pyaml-env = "^1.2.1"
gunicorn = "^20.1.0"
hazelcast-python-client = "^5.3.0"


[build-system]
Expand Down
Loading
Loading