diff --git a/rq/cli/helpers.py b/rq/cli/helpers.py index a0803f9a..01a63bfd 100644 --- a/rq/cli/helpers.py +++ b/rq/cli/helpers.py @@ -5,6 +5,7 @@ from __future__ import (absolute_import, division, print_function, import sys import importlib import time +import os from functools import partial import click @@ -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), } @@ -235,8 +245,11 @@ class CliConfig: 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 diff --git a/tests/test_cli.py b/tests/test_cli.py index 8c5aa629..ee4756aa 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4,6 +4,8 @@ from __future__ import (absolute_import, division, print_function, from datetime import datetime, timezone +import os + from click.testing import CliRunner from redis import Redis @@ -106,6 +108,16 @@ class TestRQCli(RQTestCase): '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 """ runner = CliRunner()