-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle cache health check with Django
- Loading branch information
Showing
2 changed files
with
33 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
Command to wait for the cache to be available before proceeding. | ||
Inspired from https://github.com/enzofrnt/django_wait_for_db | ||
""" | ||
|
||
import time | ||
|
||
import djclick as click | ||
from django.core.cache import caches | ||
from redis.exceptions import ConnectionError | ||
|
||
|
||
@click.command() | ||
def wait_for_cache(): | ||
click.secho("Waiting for the cache to be available...", fg="yellow") | ||
|
||
while True: | ||
try: | ||
cache = caches["default"] | ||
client = cache._cache.get_client() | ||
client.ping() | ||
|
||
click.secho("Cache available!", fg="green") | ||
|
||
break | ||
|
||
except ConnectionError as e: | ||
click.secho(f"Error with cache: {e}", fg="red") | ||
click.secho("Cache unavailable, waiting 1 second...", fg="yellow") | ||
|
||
time.sleep(1) |