Add environment variables for connection (#1472)

* Add environment variables for connection

Use same args as in config file

#1342

* add test

* add suggestions

https://github.com/rq/rq/pull/1472#discussion_r640205865
https://github.com/rq/rq/pull/1472#discussion_r640206565

* remove unused import
This commit is contained in:
rpkak 2021-05-30 13:49:28 +02:00 committed by GitHub
parent ba915508a3
commit 73d0210d65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 2 deletions

View File

@ -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

View File

@ -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 <url>"""
runner = CliRunner()