smart-chatbot-api / app /utils /sync_control.py
GitHub Actions
Deploy from GitHub Actions (2026-05-31 09:04 UTC)
55c0d78
Raw
History Blame Contribute Delete
1.41 kB
"""Redis-backed cooperative signal helpers for WP sync job control.
Each running sync task polls these flags between post entries. Cancel and
pause are cooperative — the task exits cleanly rather than being forcibly
terminated, so already-synced posts are never lost.
"""
import redis
from app.utils.config import config_manager
CANCEL_FLAG_TTL_SECONDS = 300 # 5 min — outlasts any single task execution window
PAUSE_FLAG_TTL_SECONDS = 172_800 # 48 h — matches the job expiry window
def _redis_client() -> redis.Redis:
return redis.from_url(config_manager.get_config().redis_url, decode_responses=True)
def _cancel_key(job_id: str) -> str:
return f"wp_sync_cancel:{job_id}"
def _pause_key(job_id: str) -> str:
return f"wp_sync_pause:{job_id}"
def set_cancel_flag(job_id: str) -> None:
_redis_client().set(_cancel_key(job_id), "1", ex=CANCEL_FLAG_TTL_SECONDS)
def check_cancel_flag(job_id: str) -> bool:
return bool(_redis_client().get(_cancel_key(job_id)))
def clear_cancel_flag(job_id: str) -> None:
_redis_client().delete(_cancel_key(job_id))
def set_pause_flag(job_id: str) -> None:
_redis_client().set(_pause_key(job_id), "1", ex=PAUSE_FLAG_TTL_SECONDS)
def check_pause_flag(job_id: str) -> bool:
return bool(_redis_client().get(_pause_key(job_id)))
def clear_pause_flag(job_id: str) -> None:
_redis_client().delete(_pause_key(job_id))