Added Ruff linting
This commit is contained in:
1 parent
c39513e4ff
commit
8a3630aca4
7 files changed
+59
-24
No files matched your search
@@ -0,0 +1,28 @@
|
|||||||
|
[tool.ruff]
|
||||||
|
line-length = 120
|
||||||
|
target-version = "py312"
|
||||||
|
[tool.ruff.lint]
|
||||||
|
select = [
|
||||||
|
"E", "F", # pycodestyle errors, pyflakes (unused imports/variables, undefined names)
|
||||||
|
"I", # import sorting (replaces isort)
|
||||||
|
"B", # bugbear: likely bugs
|
||||||
|
"ASYNC", # async mistakes
|
||||||
|
"RUF006", # un-referenced asyncio.create_task (tasks can be garbage-collected)
|
||||||
|
]
|
||||||
|
ignore = ["E501"]
|
||||||
|
|
||||||
|
[tool.ruff.lint.isort]
|
||||||
|
known-first-party = ["db_module", "metrics_server", "rabbitmq_handler", "secret_manager",
|
||||||
|
"simple_logger_handler", "uvicorn_logger_config", "validator"]
|
||||||
|
|
||||||
|
[tool.ruff.format]
|
||||||
|
indent-style = "tab"
|
||||||
|
|
||||||
|
[tool.ruff.lint.flake8-bugbear]
|
||||||
|
extend-immutable-calls = [
|
||||||
|
"fastapi.Depends",
|
||||||
|
"fastapi.Query",
|
||||||
|
"fastapi.Header",
|
||||||
|
"fastapi.Body",
|
||||||
|
"fastapi.Security",
|
||||||
|
]
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
ruff==0.17.0
|
||||||
@@ -1,11 +1,13 @@
|
|||||||
from contextlib import asynccontextmanager
|
|
||||||
import aiomysql
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from secret_manager import return_credentials
|
|
||||||
import os
|
import os
|
||||||
from simple_logger_handler import setup_logger
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
|
import aiomysql
|
||||||
import pymysql.err
|
import pymysql.err
|
||||||
|
|
||||||
|
from secret_manager import return_credentials
|
||||||
|
from simple_logger_handler import setup_logger
|
||||||
|
|
||||||
db_username = return_credentials("/etc/secrets/db_username")
|
db_username = return_credentials("/etc/secrets/db_username")
|
||||||
db_password = return_credentials("/etc/secrets/db_password")
|
db_password = return_credentials("/etc/secrets/db_password")
|
||||||
db_host = os.getenv("BACKEND_PN_DB_HOST","localhost")
|
db_host = os.getenv("BACKEND_PN_DB_HOST","localhost")
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ async def remove_inactive_push_token(uuid :str, db_manager):
|
|||||||
async with db_manager.acquire() as conn:
|
async with db_manager.acquire() as conn:
|
||||||
async with conn.cursor() as cur:
|
async with conn.cursor() as cur:
|
||||||
await cur.execute("UPDATE device_tokens SET status='expired' WHERE token_id=%s",
|
await cur.execute("UPDATE device_tokens SET status='expired' WHERE token_id=%s",
|
||||||
(uuid,))
|
(uuid,))
|
||||||
success = cur.rowcount > 0
|
success = cur.rowcount > 0
|
||||||
logger.debug(f"[DB] Token expiration for uuid={uuid} success={success}")
|
logger.debug(f"[DB] Token expiration for uuid={uuid} success={success}")
|
||||||
return success
|
return success
|
||||||
+4
-2
@@ -1,7 +1,9 @@
|
|||||||
from prometheus_client import Counter, Gauge, start_http_server
|
|
||||||
from simple_logger_handler import setup_logger
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
|
from prometheus_client import Counter, Gauge, start_http_server
|
||||||
|
|
||||||
|
from simple_logger_handler import setup_logger
|
||||||
|
|
||||||
logger = setup_logger(__name__)
|
logger = setup_logger(__name__)
|
||||||
|
|
||||||
MSG_PUBLISHED = Counter(
|
MSG_PUBLISHED = Counter(
|
||||||
|
|||||||
+12
-10
@@ -1,16 +1,18 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import signal
|
||||||
|
|
||||||
import aio_pika
|
import aio_pika
|
||||||
from aio_pika.exceptions import AMQPException
|
from aio_pika.exceptions import AMQPException
|
||||||
from secret_manager import return_credentials, cleanup_secret_files
|
|
||||||
from secret_manager.fernet_encryption import decrypt_token
|
|
||||||
from device_token_repository import database_lookup_by_user_id, remove_inactive_push_token, database_lookup_by_uuid
|
|
||||||
import os
|
|
||||||
from simple_logger_handler import setup_logger
|
|
||||||
import json
|
|
||||||
from db import db_manager
|
|
||||||
from send_notification import send_notification
|
|
||||||
import signal
|
|
||||||
import metrics
|
import metrics
|
||||||
|
from db import db_manager
|
||||||
|
from device_token_repository import database_lookup_by_user_id, database_lookup_by_uuid, remove_inactive_push_token
|
||||||
|
from secret_manager import cleanup_secret_files, return_credentials
|
||||||
|
from secret_manager.fernet_encryption import decrypt_token
|
||||||
|
from send_notification import send_notification
|
||||||
|
from simple_logger_handler import setup_logger
|
||||||
|
|
||||||
logger = setup_logger(__name__)
|
logger = setup_logger(__name__)
|
||||||
|
|
||||||
@@ -70,7 +72,7 @@ class RabbitMQConsumer:
|
|||||||
logger.debug(f"[RabbitMQ] DLQ queue '{self.dlq_queue_name}' declared.")
|
logger.debug(f"[RabbitMQ] DLQ queue '{self.dlq_queue_name}' declared.")
|
||||||
|
|
||||||
metrics.start_metrics_server(port=9000)
|
metrics.start_metrics_server(port=9000)
|
||||||
logger.debug(f"[Metrics] Prometheus metrics server started on port 9000.")
|
logger.debug("[Metrics] Prometheus metrics server started on port 9000.")
|
||||||
|
|
||||||
self._metric_tasks = [
|
self._metric_tasks = [
|
||||||
asyncio.create_task(metrics.update_queue_gauge(self.channel, self.queue_name)),
|
asyncio.create_task(metrics.update_queue_gauge(self.channel, self.queue_name)),
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import aiohttp
|
|
||||||
import aiodns
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
|
import aiohttp
|
||||||
|
|
||||||
from simple_logger_handler import setup_logger
|
from simple_logger_handler import setup_logger
|
||||||
|
|
||||||
API_ENDPOINT="https://exp.host/--/api/v2/push/send"
|
API_ENDPOINT="https://exp.host/--/api/v2/push/send"
|
||||||
@@ -15,15 +16,15 @@ retryable_exceptions = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def send_notification(message: dict, push_tokens, max_retries: int = 5, timeout: int = 5):
|
async def send_notification(message: dict, push_tokens, max_retries: int = 5):
|
||||||
logger.debug(f"[Notification] Sending to {len(push_tokens)} push tokens")
|
logger.debug(f"[Notification] Sending to {len(push_tokens)} push tokens")
|
||||||
results = {}
|
results = {}
|
||||||
for uuid, token in push_tokens.items():
|
for uuid, token in push_tokens.items():
|
||||||
logger.debug(f"[Notification] Preparing to send to uuid={uuid}")
|
logger.debug(f"[Notification] Preparing to send to uuid={uuid}")
|
||||||
results[uuid] = await _send_to_token(token, uuid, message, max_retries, timeout)
|
results[uuid] = await _send_to_token(token, uuid, message, max_retries)
|
||||||
return results
|
return results
|
||||||
|
|
||||||
async def _send_to_token(token: str, uuid:str , message: dict, max_retries: int, timeout: int):
|
async def _send_to_token(token: str, uuid:str , message: dict, max_retries: int):
|
||||||
payload = create_payload(token, message)
|
payload = create_payload(token, message)
|
||||||
logger.debug(f"[Notification] Payload for uuid={uuid}: {payload}")
|
logger.debug(f"[Notification] Payload for uuid={uuid}: {payload}")
|
||||||
|
|
||||||
@@ -36,8 +37,7 @@ async def _send_to_token(token: str, uuid:str , message: dict, max_retries: int,
|
|||||||
async with session.post(
|
async with session.post(
|
||||||
url=API_ENDPOINT,
|
url=API_ENDPOINT,
|
||||||
json=payload,
|
json=payload,
|
||||||
headers={"Content-Type": "application/json"},
|
headers={"Content-Type": "application/json"}
|
||||||
timeout=timeout
|
|
||||||
) as response:
|
) as response:
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
data = await response.json()
|
data = await response.json()
|
||||||
|
|||||||
Reference in new issue
Block a user