Skip to content

Commit

Permalink
Add environment variables for connection (rq#1472)
Browse files Browse the repository at this point in the history
* Add environment variables for connection

Use same args as in config file

rq#1342

* add test

* add suggestions

rq#1472 (comment)
rq#1472 (comment)

* remove unused import
  • Loading branch information
rpkak authored May 30, 2021
1 parent ba91550 commit 73d0210
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
17 changes: 15 additions & 2 deletions rq/cli/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
import importlib
import time
import os
from functools import partial

import click
Expand Down Expand Up @@ -48,12 +49,21 @@ def get_redis_from_config(settings, connection_class=Redis):
sn = Sentinel(instances, socket_timeout=socket_timeout, password=password, db=db)
return sn.master_for(master_name)

ssl = settings.get('REDIS_SSL', False)
if isinstance(ssl, str):
if ssl.lower() in ['y', 'yes', 't', 'true']:
ssl = True
elif ssl.lower() in ['n', 'no', 'f', 'false', '']:
ssl = False
else:
raise ValueError('REDIS_SSL is a boolean and must be "True" or "False".')

kwargs = {
'host': settings.get('REDIS_HOST', 'localhost'),
'port': settings.get('REDIS_PORT', 6379),
'db': settings.get('REDIS_DB', 0),
'password': settings.get('REDIS_PASSWORD', None),
'ssl': settings.get('REDIS_SSL', False),
'ssl': ssl,
'ssl_ca_certs': settings.get('REDIS_SSL_CA_CERTS', None),
}

Expand Down Expand Up @@ -235,8 +245,11 @@ def connection(self):
if self._connection is None:
if self.url:
self._connection = self.connection_class.from_url(self.url)
else:
elif self.config:
settings = read_config_file(self.config) if self.config else {}
self._connection = get_redis_from_config(settings,
self.connection_class)
else:
self._connection = get_redis_from_config(os.environ,
self.connection_class)
return self._connection
12 changes: 12 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from datetime import datetime, timezone

import os

from click.testing import CliRunner
from redis import Redis

Expand Down Expand Up @@ -106,6 +108,16 @@ def test_config_file_default_options_override(self):
'123'
)

def test_config_env_vars(self):
os.environ['REDIS_HOST'] = "testhost.example.com"

cli_config = CliConfig()

self.assertEqual(
cli_config.connection.connection_pool.connection_kwargs['host'],
'testhost.example.com',
)

def test_empty_nothing(self):
"""rq empty -u <url>"""
runner = CliRunner()
Expand Down

0 comments on commit 73d0210

Please sign in to comment.