Skip to content

Commit

Permalink
Delete Obsidian plugin requests after timeout.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nurdok committed Feb 27, 2024
1 parent 07eecea commit 983f9b5
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/spanreed/apis/obsidian.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@ async def _send_request(
}
self._logger.info(f"Sending request: {request=}")

request_queue_name: str = f"obsidian-plugin-tasks:{self._user.id}"
try:
async with asyncio.timeout(
datetime.timedelta(seconds=30).total_seconds()
):
await redis_api.lpush(
f"obsidian-plugin-tasks:{self._user.id}",
json.dumps(request),
)
await redis_api.lpush(request_queue_name, json.dumps(request))
except Exception:
self._logger.error(f"Failed to send request: {request=}")
raise
Expand All @@ -58,18 +56,20 @@ async def _send_request(
async with asyncio.timeout(
datetime.timedelta(seconds=30).total_seconds()
):
queue_name: str = (
response_queue_name: str = (
f"obsidian-plugin-tasks:{self._user.id}:{request_id}"
)
self._logger.info(f"Waiting for response on {queue_name=}")
self._logger.info(f"Waiting for response on {response_queue_name=}")
response: dict = json.loads(
# Take the value from the tuple returned by `blpop`, we already know the key
(await redis_api.blpop(queue_name))[1]
(await redis_api.blpop(response_queue_name))[1]
)
self._logger.info(
f"Got response for {method=} on {queue_name=}: {response=}"
f"Got response for {method=} on {response_queue_name=}: {response=}"
)
except TimeoutError as e:
except TimeoutError:
# Delete the request from the queue
await redis_api.lrem(request_queue_name, 0, json.dumps(request))
raise TimeoutError(
f"Obsidian API request timed out ({request_id=})."
)
Expand Down

0 comments on commit 983f9b5

Please sign in to comment.