Spaces:
Running
Running
Commit ·
2cfd8b7
1
Parent(s): ff674de
ok
Browse files
app/services/scheduler_service.py
CHANGED
|
@@ -6,6 +6,7 @@ import json
|
|
| 6 |
import re
|
| 7 |
import secrets
|
| 8 |
import uuid
|
|
|
|
| 9 |
from datetime import UTC, datetime, timedelta
|
| 10 |
from enum import Enum
|
| 11 |
from typing import Any
|
|
@@ -502,27 +503,60 @@ class SchedulerService:
|
|
| 502 |
# Redis connection management
|
| 503 |
# -----------------------------------------------------------------------
|
| 504 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 505 |
async def _connect_async_redis(self) -> Any:
|
| 506 |
ra = _get_redis_asyncio()
|
| 507 |
if ra is None:
|
| 508 |
return None
|
| 509 |
try:
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
port=settings.redis_port,
|
| 513 |
-
password=settings.redis_password or None,
|
| 514 |
-
db=settings.redis_db,
|
| 515 |
-
ssl=settings.redis_ssl,
|
| 516 |
-
socket_timeout=settings.redis_socket_timeout,
|
| 517 |
-
socket_connect_timeout=settings.redis_socket_connect_timeout,
|
| 518 |
-
retry_on_timeout=settings.redis_retry_on_timeout,
|
| 519 |
-
health_check_interval=settings.redis_health_check_interval,
|
| 520 |
-
decode_responses=True,
|
| 521 |
-
)
|
| 522 |
await client.ping()
|
| 523 |
logger.info(
|
| 524 |
-
"Connected to Redis
|
| 525 |
-
settings.redis_host
|
| 526 |
)
|
| 527 |
return client
|
| 528 |
except Exception as exc:
|
|
@@ -533,17 +567,7 @@ class SchedulerService:
|
|
| 533 |
cls = _get_redis_jobstore_cls()
|
| 534 |
if cls is None:
|
| 535 |
raise RuntimeError("apscheduler[jobstores_redis] not installed")
|
| 536 |
-
return cls(
|
| 537 |
-
db=settings.redis_db,
|
| 538 |
-
host=settings.redis_host,
|
| 539 |
-
port=settings.redis_port,
|
| 540 |
-
password=settings.redis_password or None,
|
| 541 |
-
ssl=settings.redis_ssl,
|
| 542 |
-
socket_timeout=settings.redis_socket_timeout,
|
| 543 |
-
socket_connect_timeout=settings.redis_socket_connect_timeout,
|
| 544 |
-
retry_on_timeout=settings.redis_retry_on_timeout,
|
| 545 |
-
health_check_interval=settings.redis_health_check_interval,
|
| 546 |
-
)
|
| 547 |
|
| 548 |
# -----------------------------------------------------------------------
|
| 549 |
# Distributed execution lock (Redis SETNX)
|
|
|
|
| 6 |
import re
|
| 7 |
import secrets
|
| 8 |
import uuid
|
| 9 |
+
from urllib.parse import urlparse
|
| 10 |
from datetime import UTC, datetime, timedelta
|
| 11 |
from enum import Enum
|
| 12 |
from typing import Any
|
|
|
|
| 503 |
# Redis connection management
|
| 504 |
# -----------------------------------------------------------------------
|
| 505 |
|
| 506 |
+
def _redis_conn_params(self) -> dict:
|
| 507 |
+
"""Connection parameters for redis.Redis()."""
|
| 508 |
+
if settings.redis_url:
|
| 509 |
+
return {"url": settings.redis_url}
|
| 510 |
+
return {
|
| 511 |
+
"host": settings.redis_host,
|
| 512 |
+
"port": settings.redis_port,
|
| 513 |
+
"password": settings.redis_password or None,
|
| 514 |
+
"db": settings.redis_db,
|
| 515 |
+
"ssl": settings.redis_ssl,
|
| 516 |
+
"socket_timeout": settings.redis_socket_timeout,
|
| 517 |
+
"socket_connect_timeout": settings.redis_socket_connect_timeout,
|
| 518 |
+
"retry_on_timeout": settings.redis_retry_on_timeout,
|
| 519 |
+
"health_check_interval": settings.redis_health_check_interval,
|
| 520 |
+
}
|
| 521 |
+
|
| 522 |
+
def _redis_jobstore_params(self) -> dict:
|
| 523 |
+
"""Connection parameters for RedisJobStore (does not accept url)."""
|
| 524 |
+
if settings.redis_url:
|
| 525 |
+
parsed = urlparse(settings.redis_url)
|
| 526 |
+
return {
|
| 527 |
+
"host": parsed.hostname or settings.redis_host,
|
| 528 |
+
"port": parsed.port or settings.redis_port,
|
| 529 |
+
"password": parsed.password or settings.redis_password or None,
|
| 530 |
+
"db": int(parsed.path.lstrip("/")) if parsed.path and parsed.path != "/" else settings.redis_db,
|
| 531 |
+
"ssl": parsed.scheme in ("rediss", "redis+ssl"),
|
| 532 |
+
"socket_timeout": settings.redis_socket_timeout,
|
| 533 |
+
"socket_connect_timeout": settings.redis_socket_connect_timeout,
|
| 534 |
+
"retry_on_timeout": settings.redis_retry_on_timeout,
|
| 535 |
+
"health_check_interval": settings.redis_health_check_interval,
|
| 536 |
+
}
|
| 537 |
+
return {
|
| 538 |
+
"host": settings.redis_host,
|
| 539 |
+
"port": settings.redis_port,
|
| 540 |
+
"password": settings.redis_password or None,
|
| 541 |
+
"db": settings.redis_db,
|
| 542 |
+
"ssl": settings.redis_ssl,
|
| 543 |
+
"socket_timeout": settings.redis_socket_timeout,
|
| 544 |
+
"socket_connect_timeout": settings.redis_socket_connect_timeout,
|
| 545 |
+
"retry_on_timeout": settings.redis_retry_on_timeout,
|
| 546 |
+
"health_check_interval": settings.redis_health_check_interval,
|
| 547 |
+
}
|
| 548 |
+
|
| 549 |
async def _connect_async_redis(self) -> Any:
|
| 550 |
ra = _get_redis_asyncio()
|
| 551 |
if ra is None:
|
| 552 |
return None
|
| 553 |
try:
|
| 554 |
+
params = self._redis_conn_params()
|
| 555 |
+
client = ra.Redis(**params, decode_responses=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 556 |
await client.ping()
|
| 557 |
logger.info(
|
| 558 |
+
"Connected to Redis (url=%s)",
|
| 559 |
+
params.get("url") or f"{settings.redis_host}:{settings.redis_port}",
|
| 560 |
)
|
| 561 |
return client
|
| 562 |
except Exception as exc:
|
|
|
|
| 567 |
cls = _get_redis_jobstore_cls()
|
| 568 |
if cls is None:
|
| 569 |
raise RuntimeError("apscheduler[jobstores_redis] not installed")
|
| 570 |
+
return cls(**self._redis_jobstore_params())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 571 |
|
| 572 |
# -----------------------------------------------------------------------
|
| 573 |
# Distributed execution lock (Redis SETNX)
|