Skip to content

Commit 3874df6

Browse files
committed
Put back requests and synchronous Matrix logging
1 parent fa25af8 commit 3874df6

File tree

5 files changed

+9
-18
lines changed

5 files changed

+9
-18
lines changed

app/core/utils/log.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from typing import Any
88

99
import uvicorn
10-
from fastapi import BackgroundTasks
1110

1211
from app.core.utils.config import Settings
1312

@@ -122,7 +121,6 @@ def _get_config_dict(self, settings: Settings):
122121
# Send error to a Matrix server. If credentials are not set in settings, the handler will be disabled
123122
"formatter": "matrix",
124123
"class": "app.utils.loggers_tools.matrix_handler.MatrixHandler",
125-
"background_tasks": BackgroundTasks,
126124
"room_id": settings.MATRIX_LOG_ERROR_ROOM_ID,
127125
"token": settings.MATRIX_TOKEN,
128126
"server_base_url": settings.MATRIX_SERVER_BASE_URL,
@@ -135,7 +133,6 @@ def _get_config_dict(self, settings: Settings):
135133
# Send error to a Matrix server. If credentials are not set in settings, the handler will be disabled
136134
"formatter": "matrix",
137135
"class": "app.utils.loggers_tools.matrix_handler.MatrixHandler",
138-
"background_tasks": BackgroundTasks,
139136
"room_id": settings.MATRIX_LOG_AMAP_ROOM_ID,
140137
"token": settings.MATRIX_TOKEN,
141138
"server_base_url": settings.MATRIX_SERVER_BASE_URL,

app/utils/communication/matrix.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Any
22

3-
import httpx
3+
import requests
44

55
from app.types.exceptions import MatrixRequestError, MatrixSendMessageError
66

@@ -24,7 +24,7 @@ def __init__(
2424

2525
self.access_token = token
2626

27-
async def post(
27+
def post(
2828
self,
2929
url: str,
3030
json: dict[str, Any],
@@ -43,18 +43,15 @@ async def post(
4343
if "Authorization" not in headers:
4444
headers["Authorization"] = "Bearer " + self.access_token
4545

46+
response = requests.post(url, json=json, headers=headers, timeout=10)
4647
try:
47-
async with httpx.AsyncClient() as client:
48-
response = await client.post(
49-
url, json=json, headers=headers, timeout=10
50-
)
5148
response.raise_for_status()
52-
except httpx.RequestError as err:
49+
except requests.exceptions.HTTPError as err:
5350
raise MatrixRequestError() from err
5451

5552
return response.json()
5653

57-
async def send_message(self, room_id: str, formatted_body: str) -> None:
54+
def send_message(self, room_id: str, formatted_body: str) -> None:
5855
"""
5956
Send a message to the room `room_id`.
6057
`formatted_body` can contain html formatted text
@@ -74,6 +71,6 @@ async def send_message(self, room_id: str, formatted_body: str) -> None:
7471
}
7572

7673
try:
77-
await self.post(url, json=data, headers=None)
74+
self.post(url, json=data, headers=None)
7875
except MatrixRequestError as error:
7976
raise MatrixSendMessageError(room_id=room_id) from error

app/utils/loggers_tools/matrix_handler.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import logging
22
from logging import StreamHandler
33

4-
from fastapi import BackgroundTasks
54
from typing_extensions import override
65

76
from app.utils.communication.matrix import Matrix
@@ -22,7 +21,6 @@ class MatrixHandler(StreamHandler):
2221

2322
def __init__(
2423
self,
25-
background_tasks: BackgroundTasks,
2624
room_id: str,
2725
token: str,
2826
server_base_url: str | None,
@@ -32,7 +30,6 @@ def __init__(
3230
super().__init__()
3331
self.setLevel(level)
3432

35-
self.background_tasks = background_tasks
3633
self.room_id = room_id
3734
self.enabled = enabled
3835
if self.enabled:
@@ -46,9 +43,7 @@ def emit(self, record):
4643
if self.enabled:
4744
msg = self.format(record)
4845
try:
49-
self.background_tasks.add_task(
50-
self.matrix.send_message, room_id=self.room_id, formatted_body=msg
51-
)
46+
self.matrix.send_message(self.room_id, msg)
5247
# We should catch and log any error, as Python may discarded them in production
5348
except Exception as err:
5449
# We use warning level so that the message is not sent to matrix again

requirements-common.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ PyMuPDF==1.24.9 # PDF processing, imported as `fitz`
2626
pypdf==4.3.1
2727
python-multipart==0.0.18 # a form data parser, as oauth flow requires form-data parameters
2828
redis==5.0.8
29+
requests==2.32.4
2930
sqlalchemy-utils == 0.41.2
3031
SQLAlchemy[asyncio]==2.0.32 # [asyncio] allows greenlet to be installed on Apple M1 devices.
3132
unidecode==1.3.8

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ types-Authlib==1.5.0.20250516
1414
types-fpdf2==2.8.3.20250516
1515
types-psutil==7.0.0.20250601
1616
types-redis==4.6.0.20241004
17+
types-requests==2.32.0.20250515

0 commit comments

Comments
 (0)