Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ BOT_EMOJI_ID=5417915203100613993
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_DB=0
REDIS_USERNAME=
REDIS_PASSWORD=
6 changes: 1 addition & 5 deletions app/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ async def main() -> None:
config = load_config()

# Initialize apscheduler
job_store = RedisJobStore(
host=config.redis.HOST,
port=config.redis.PORT,
db=config.redis.DB,
)
job_store = RedisJobStore(**config.redis.connection_dict)
apscheduler = AsyncIOScheduler(
jobstores={"default": job_store},
)
Expand Down
28 changes: 27 additions & 1 deletion app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,38 @@ class RedisConfig:
HOST: str
PORT: int
DB: int
USERNAME: str | None
PASSWORD: str | None

def dsn(self) -> str:
"""
Generates a Redis connection DSN (Data Source Name) using the provided host, port, and database.

:return: The generated DSN.
"""
return f"redis://{self.HOST}:{self.PORT}/{self.DB}"
if self.PASSWORD:
return f"redis://{self.USERNAME or ''}:{self.PASSWORD}@{self.HOST}:{self.PORT}/{self.DB}"
else:
return f"redis://{self.HOST}:{self.PORT}/{self.DB}"

@property
def connection_dict(self) -> dict[str, str | int]:
"""
Connection kwargs for Redis clients.
"""

args = dict(
host=self.HOST,
port=self.PORT,
db=self.DB,
)

if self.USERNAME:
args["username"] = self.USERNAME
if self.PASSWORD:
args["password"] = self.PASSWORD

return args


@dataclass
Expand Down Expand Up @@ -76,5 +100,7 @@ def load_config() -> Config:
HOST=env.str("REDIS_HOST"),
PORT=env.int("REDIS_PORT"),
DB=env.int("REDIS_DB"),
USERNAME=env.str("REDIS_USERNAME", None),
PASSWORD=env.str("REDIS_PASSWORD", None),
),
)