Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError accessing stats page #632

Closed
bennylope opened this issue Nov 27, 2023 · 2 comments · Fixed by #633
Closed

KeyError accessing stats page #632

bennylope opened this issue Nov 27, 2023 · 2 comments · Fixed by #633

Comments

@bennylope
Copy link
Contributor

bennylope commented Nov 27, 2023

The Redis database does not need to be declared in the Redis connection string, allowing the default 0 db to be used. This is what many providers do when providing a connection string, e.g. Heroku, Render, etc. If using an existing Redis cache connection as described in the readme, no DB will ever be included.

The connection uses db 0 but the connection kwargs do not reflect this, and the get_scheduler_statistics() function depends on the connection kwargs.

def get_scheduler_statistics():
schedulers = {}
for index, config in enumerate(QUEUES_LIST):
# there is only one scheduler per redis connection, so we use the connection as key
# to handle the possibility of a configuration with multiple redis connections and scheduled
# jobs in more than one of them
queue = get_queue_by_index(index)
connection_kwargs = queue.connection.connection_pool.connection_kwargs
conn_key = f"{connection_kwargs['host']}:{connection_kwargs['port']}/{connection_kwargs['db']}"

When the db is not included in the connection kwargs (line 105 above), a KeyError is raised.

bennylope added a commit to bennylope/django-rq that referenced this issue Nov 27, 2023
When the `db` value is not included explicitly in the connection kwargs
the default `0` db is used but this is not reflected in the connection
kwargs.

Closes rqgh-632
@csilcock
Copy link

csilcock commented Dec 8, 2023

This is blocking our team from upgrading to django-rq 2.9.x - any idea when the fix will be merged / released?

@bennylope
Copy link
Contributor Author

@csilcock there is a workaround and I feel badly for forgetting to share it.

Add this function somewhere - I chose to add it to a separate module to import into settings:

from urllib.parse import urlparse


def redis_with_default_db(redis_url):
    """Return a redis connection with the specified db.

    Necessary to ensure that the DB is in the connection kwargs.
    """
    parsed = urlparse(redis_url)
    db = parsed.path.strip("/") or "0"
    return parsed._replace(path=f"/{db}").geturl()

Then in your settings.py file (having either imported redis_with_default_db or defined it locally):

REDIS_URL = redis_with_default_db(os.environ.get("REDIS_URL", "redis://"))

This is not guaranteed to work universally, but the general concept could be reapplied if the connection string differs.

selwin pushed a commit that referenced this issue Dec 16, 2023
When the `db` value is not included explicitly in the connection kwargs
the default `0` db is used but this is not reflected in the connection
kwargs.

Closes gh-632
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 a pull request may close this issue.

2 participants