Skip to content

Commit

Permalink
Feature/fix connection errors (#17)
Browse files Browse the repository at this point in the history
* fix redis and plex connection errors

* fix redis and plex connection errors

---------

Co-authored-by: Maksym Ivanchenko <--set>
  • Loading branch information
vanchaxy authored Apr 5, 2024
1 parent 7dd2f84 commit 39fc8a8
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ app = 'plexio'
primary_region = 'iad'

[build]
image = 'ghcr.io/vanchaxy/plexio:0.0.1-rc7'
image = 'ghcr.io/vanchaxy/plexio:0.0.1-rc8'

[env]
CACHE_TYPE = 'redis'
Expand Down
4 changes: 2 additions & 2 deletions frontend/package-lock.json

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

2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "frontend",
"version": "0.0.1-rc7",
"version": "0.0.1-rc8",
"dependencies": {
"base-64": "^1.0.0",
"react": "^18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion plexio/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.1-rc7'
__version__ = '0.0.1-rc8'
21 changes: 17 additions & 4 deletions plexio/cache.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import asyncio
from abc import ABC, abstractmethod
from enum import Enum

from redis.asyncio import Redis
from redis.exceptions import ConnectionError as RedisConnectionError


def init_cache(settings):
Expand Down Expand Up @@ -46,16 +48,27 @@ async def close(self):


class RedisCache(AbstractCache):
RETRY_TIMES = 3
RETRY_BACKOFF_SEC = 1

def __init__(self, redis_url):
self._redis = Redis.from_url(url=redis_url)

async def set(self, key, value):
await self._redis.set(key, value)
for _ in range(RedisCache.RETRY_TIMES):
try:
await self._redis.set(key, value)
except RedisConnectionError:
await asyncio.sleep(RedisCache.RETRY_BACKOFF_SEC)

async def get(self, key):
if value := await self._redis.get(key):
return value.decode()
return None
for _ in range(RedisCache.RETRY_TIMES):
try:
if value := await self._redis.get(key):
return value.decode()
return None
except RedisConnectionError:
await asyncio.sleep(RedisCache.RETRY_BACKOFF_SEC)

async def close(self):
await self._redis.close()
16 changes: 11 additions & 5 deletions plexio/plex/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import asyncio
import json
from json import JSONDecodeError

from aiohttp import ClientConnectorError, ContentTypeError
from aiohttp import ClientConnectorError
from fastapi import HTTPException
from sentry_sdk import configure_scope

Expand All @@ -16,11 +18,15 @@ async def get_json(client, url, params=None):
params=params,
timeout=settings.plex_requests_timeout,
) as response:
json = await response.json()
return json
except ContentTypeError as e:
with configure_scope() as scope:
if response.status >= 400:
raise HTTPException(
status_code=502,
detail='Received error from plex server',
)
response_bytes = await response.read()
return json.loads(response_bytes.decode())
except JSONDecodeError as e:
with configure_scope() as scope:
scope.add_attachment(bytes=response_bytes, filename='attachment.txt')
raise e
except ClientConnectorError as e:
Expand Down

0 comments on commit 39fc8a8

Please sign in to comment.