You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kmftools/bookscraper/db/state_redis.py

80 lines
2.8 KiB

# ============================================================
# File: db/state_redis.py
# Purpose:
# Low-level Redis counters/state for BookScraper.
# Used ONLY by db.repository façade.
# ============================================================
import os
import time
import redis
from logbus.publisher import log
REDIS_URL = os.getenv("REDIS_BROKER", "redis://redis:6379/0")
r = redis.Redis.from_url(REDIS_URL, decode_responses=True)
# ------------------------------------------------------------
# STATUS
# ------------------------------------------------------------
def redis_set_status(book_id: str, status: str):
key = f"book:{book_id}:state"
r.hset(key, "status", status)
r.hset(key, "last_update", int(time.time()))
# ------------------------------------------------------------
# TOTAL CHAPTERS
# ------------------------------------------------------------
def redis_set_chapters_total(book_id: str, total: int):
key = f"book:{book_id}:state"
r.hset(key, "chapters_total", total)
r.hset(key, "last_update", int(time.time()))
# ------------------------------------------------------------
# DOWNLOAD COUNTERS
# ------------------------------------------------------------
def redis_inc_download_done(book_id: str, amount: int = 1):
key = f"book:{book_id}:state"
r.hincrby(key, "chapters_download_done", amount)
r.hset(key, "last_update", int(time.time()))
def redis_inc_download_skipped(book_id: str, amount: int = 1):
log(f"[DB-REDIS] Incrementing download skipped for {book_id} by {amount}")
key = f"book:{book_id}:state"
r.hincrby(key, "chapters_download_skipped", amount)
r.hset(key, "last_update", int(time.time()))
# ------------------------------------------------------------
# PARSE COUNTERS
# ------------------------------------------------------------
def redis_inc_parsed_done(book_id: str, amount: int = 1):
key = f"book:{book_id}:state"
r.hincrby(key, "chapters_parsed_done", amount)
r.hset(key, "last_update", int(time.time()))
# ------------------------------------------------------------
# AUDIO COUNTERS
# ------------------------------------------------------------
def redis_inc_audio_done(book_id: str, amount: int = 1):
log(f"[DB-REDIS] Incrementing audio done for {book_id} by {amount}")
key = f"book:{book_id}:state"
r.hincrby(key, "audio_done", amount)
r.hset(key, "last_update", int(time.time()))
def redis_inc_audio_skipped(book_id: str, amount: int = 1):
log(f"[DB-REDIS] Incrementing audio skipped for {book_id} by {amount}")
"""
New: Count skipped audio chapters (timeouts, pre-existing files, abort, etc.)
SQL does NOT track this; Redis-only metric.
"""
key = f"book:{book_id}:state"
r.hincrby(key, "audio_skipped", amount)
r.hset(key, "last_update", int(time.time()))