Skip to content

Conversation

@reevebarreto
Copy link
Collaborator

✨ Feature / Enhancement PR

πŸ”— Epic / Issue

Closes #179
[Feature Request]: Configurable Connection Retries for DB and Redis


πŸš€ Summary (1–2 sentences)

Adds configurable connection retry logic for both Redis and the database on application startup. This ensures the services wait for dependencies to be ready, improving robustness during cold starts or in containerized environments.


πŸ§ͺ Checks

  • make lint passes
  • make test passes

πŸ““ Notes (optional)

  • Introduced redis_isready.py, modeled after db_isready.py, to handle Redis readiness.
  • Updated main.py to call both wait_for_db_ready() and wait_for_redis_ready() before starting the app.
  • Added unit tests for Redis readiness logic (tests/unit/mcpgateway/utils/test_redis_isready.py).
flowchart TD
    A["main.py"] --> B["wait_for_db_ready()"]
    B --> C["wait_for_redis_ready()"]
    B -->|ping| D[(Database)]
    C -->|ping| E[(Redis)]
Loading

- Introduced redis_isready.py to wait for Redis connection readiness.
- Updated main.py to wait for both database and Redis to be ready before proceeding.
- Added tests for Redis readiness functionality.

Signed-off-by: reevebarreto <reevemarcbarreto@gmail.com>
@crivetimihai
Copy link
Member

crivetimihai commented Jul 2, 2025

  1. Ran make autoflake isort black pre-commit to cleanup the code.
  2. Works on a clean install: make venv install install-dev serve - and works with sqlite and no redis.
  3. Fix issues flagged by pre-commit: mcpgateway/utils/redis_isready.py: has a shebang but is not marked executable:
git add --chmod=+x mcpgateway/utils/redis_isready.py
chmod +x mcpgateway/utils/redis_isready.py` 
  1. Ensure that make autoflake isort black pre-commit now passes clean
  2. Check lint, ex: make flake8 - this raises a few issues we need to sort in the PR: mcpgateway/utils/redis_isready.py:71:1: DAR401 Missing exception(s) in Raises section: -r RuntimeError (just a formatting error, resolved moving it to same line)
  3. Ensure that new code passes pylint -> 10/10, great job!
  4. Check static typing (mypy, ty) -> Success: no issues found in 1 source file
    FIXED 8. pyright: redis_isready.py:124:17 - error: "Redis" is possibly unbound (reportPossiblyUnboundVariable)
    FIXED 9. pyrefly: ERROR Argument () -> None is not assignable to parameter func with type (**tuple[*@_]) -> @_ in function asyncio.events.AbstractEventLoop.run_in_executor [bad-argument-type]
  5. make test passes
  6. make smoketest passes.
  7. Works in docker-compose (e.g. make compose-down compose-rm compose-up) and check docker logs mcp-context-forge-gateway-1
INFO:db_isready:Probing postgresql at postgres:5432/mcp (timeout=2s, interval=2.0s, max_tries=3)                                                       INFO:db_isready:Database ready after 0.01s (attempt 1)                                                                                                 INFO:redis_isready:Redis ready (attempt 1)         

Still missing:
FIXED 1. Update to helm chart (values.txt) with new .env.example values
FIXED 2. redis_isready.py is an executable script but is missing a CLI entrypoint. So when running this as a probe from the CLI it will return 0 even if redis is not ready. This needs to be fixed similar to db_isready.py. Fixed:

./mcpgateway/utils/redis_isready.py
2025-07-02T22:36:09 [INFO] Probing Redis at redis://localhost:6379/0 (interval=2000ms, max_retries=3)
2025-07-02T22:36:09 [INFO] Redis ready (attempt 1)

Not tested yet:

  1. Helm deployment.

Fixing lint issues:
Issue 1: "Redis" is possibly unbound (pyright)
If the redis library isn't installed, the import fails and Redis becomes undefined, but the code could still try to use it if someone calls wait_for_redis_ready() directly (bypassing the CLI check).

Issue 2: run_in_executor argument type (pyrefly)
_probe is defined as () -> None, but run_in_executor expects a callable that accepts *args. This is a type annotation mismatch.

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
@crivetimihai crivetimihai merged commit fc8cc48 into IBM:main Jul 2, 2025
21 of 22 checks passed
vk-playground pushed a commit to vk-playground/mcp-context-forge that referenced this pull request Sep 14, 2025
IBM#214)

* feat: add configurable connection retries for DB and Redis readiness
- Introduced redis_isready.py to wait for Redis connection readiness.
- Updated main.py to wait for both database and Redis to be ready before proceeding.
- Added tests for Redis readiness functionality.

Signed-off-by: reevebarreto <reevemarcbarreto@gmail.com>

* Linting fixes and autoflake etc.

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>

* cli entrypoint

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>

---------

Signed-off-by: reevebarreto <reevemarcbarreto@gmail.com>
Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
Co-authored-by: Mihai Criveti <crivetimihai@gmail.com>
vk-playground pushed a commit to vk-playground/mcp-context-forge that referenced this pull request Sep 14, 2025
IBM#214)

* feat: add configurable connection retries for DB and Redis readiness
- Introduced redis_isready.py to wait for Redis connection readiness.
- Updated main.py to wait for both database and Redis to be ready before proceeding.
- Added tests for Redis readiness functionality.

Signed-off-by: reevebarreto <reevemarcbarreto@gmail.com>

* Linting fixes and autoflake etc.

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>

* cli entrypoint

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>

---------

Signed-off-by: reevebarreto <reevemarcbarreto@gmail.com>
Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
Co-authored-by: Mihai Criveti <crivetimihai@gmail.com>
vk-playground pushed a commit to vk-playground/mcp-context-forge that referenced this pull request Sep 16, 2025
IBM#214)

* feat: add configurable connection retries for DB and Redis readiness
- Introduced redis_isready.py to wait for Redis connection readiness.
- Updated main.py to wait for both database and Redis to be ready before proceeding.
- Added tests for Redis readiness functionality.

Signed-off-by: reevebarreto <reevemarcbarreto@gmail.com>

* Linting fixes and autoflake etc.

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>

* cli entrypoint

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>

---------

Signed-off-by: reevebarreto <reevemarcbarreto@gmail.com>
Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
Co-authored-by: Mihai Criveti <crivetimihai@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request]: Configurable Connection Retries for DB and Redis

2 participants