[REFACTOR] DRY Redis client creation in worker cache modules#1857
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Greptile SummaryThis PR consolidates duplicated Redis connection branching in Key changes:
Issue found:
Confidence Score: 3/5
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["create_redis_client(env_prefix)"] --> B{Sentinel mode?}
B -- yes --> C["_create_sentinel_client()"]
B -- no --> D["_create_standalone_client()"]
D --> E["_resolve_redis_env()\nreads host, port, auth,\ndb, tls settings"]
E --> F["_build_connection_kwargs()\nnormal path\ntls params included"]
F --> G{max_connections?}
G -- yes --> H["ConnectionPool + redis.Redis"]
G -- no --> I["redis.Redis(kwargs)"]
C --> J["_resolve_redis_env()\nreads host, port, auth,\ndb, tls settings"]
J --> K["_build_connection_kwargs()\nauth-only path\ntls params SKIPPED\nsentinel_kwargs"]
J --> L["_build_connection_kwargs()\nnormal path\ntls params included\nmaster_kwargs"]
K --> M["Sentinel(sentinels, sentinel_kwargs)"]
L --> M
M --> N["sentinel.master_for\nmaster_kwargs"]
|
cdd8d66 to
e8cd5c7
Compare
e8cd5c7 to
323d05a
Compare
Worker cache modules (cache_backends.py, cache_utils.py) duplicated
sentinel/standalone branching logic for Redis client creation. Both
now use create_redis_client() from unstract-core as the single entry
point, which already handles mode detection internally.
- Add SSL support (ssl, ssl_cert_reqs) to create_redis_client() via
env vars ({prefix}SSL, {prefix}SSL_CERT_REQS), disabled by default
- Remove if-sentinel-else-standalone branching in cache_backends.py
and cache_utils.py — replaced with single create_redis_client() call
- Remove unused os and redis imports from simplified modules
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
323d05a to
67676a0
Compare
|
9486aea
into
fix/redis-sentinel-metrics-mixin
* [FIX] Fix Redis Sentinel crash in MetricsMixin (prompt-service) MetricsMixin was creating a raw StrictRedis client using REDIS_HOST/PORT directly, which in Sentinel HA mode points to the Sentinel process (port 26379). Sentinel doesn't support the SELECT command, causing ResponseError on every /answer-prompt call. - Replace StrictRedis with create_redis_client() from unstract-core which handles Sentinel discovery via master_for() - Add unstract-core as sdk1 dependency (all services using sdk1 already depend on core, so zero new transitive deps) - Add error handling around set_start_time() and collect_metrics() so Redis failures degrade gracefully instead of crashing requests - Add missing COPY for unstract/core in tool Dockerfiles (classifier, structure, text_extractor) since sdk1 now depends on core Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Commit uv.lock changes * [FIX] Add unstract-core to tool requirements.txt for pip resolution Tool Dockerfiles (classifier, text_extractor, structure) use pip, not uv. Since sdk1 now depends on unstract-core (which isn't on PyPI), pip can't resolve it via [tool.uv.sources]. Adding explicit editable install of core before sdk1 ensures pip finds it locally. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * [REFACTOR] DRY Redis client creation in worker cache modules (#1857) Worker cache modules (cache_backends.py, cache_utils.py) duplicated sentinel/standalone branching logic for Redis client creation. Both now use create_redis_client() from unstract-core as the single entry point, which already handles mode detection internally. - Add SSL support (ssl, ssl_cert_reqs) to create_redis_client() via env vars ({prefix}SSL, {prefix}SSL_CERT_REQS), disabled by default - Remove if-sentinel-else-standalone branching in cache_backends.py and cache_utils.py — replaced with single create_redis_client() call - Remove unused os and redis imports from simplified modules Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>



What
create_redis_client()in unstract-core via env varsWhy
cache_backends.pyandcache_utils.pyboth had identical if-sentinel-else-standalone branching to create Redis clientscreate_redis_client()already handles both modes internally — the branching was redundantcache_utils.pystandalone path but not in the factory, so it would be lost when consolidatingHow
{prefix}SSLand{prefix}SSL_CERT_REQSenv var reading to_resolve_redis_env()in core'sredis_client.py_build_connection_kwargs()and_create_standalone_client()cache_backends.pyandcache_utils.pywith a singlecreate_redis_client(env_prefix="CACHE_REDIS_", ...)callosandredisimportsCan this PR break any existing features. If yes, please list possible items. If no, please explain why. (PS: Admins do not merge the PR without this section filled)
create_redis_client()reads the same env vars (CACHE_REDIS_HOST,CACHE_REDIS_PORT, etc.) that the removed standalone code was reading. SSL is off by default (CACHE_REDIS_SSL=false), preserving current behavior.Database Migrations
Env Config
{prefix}SSLand{prefix}SSL_CERT_REQSare now read by the core factory (previously only read by workers locally). Default behavior unchanged.Relevant Docs
Related Issues or PRs
Dependencies Versions
Notes on Testing
CACHE_REDIS_SSL=false(default) behavior is unchangedRedisCacheBackendandWorkerCacheManagerboth connect successfullyScreenshots
N/A
Checklist
I have read and understood the Contribution Guidelines.