mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-02-03 06:36:39 +00:00
Autostart a redis instance in nix shell.
As a part of test environment, an unprivileged redis server is started on localhost:6379. Redis connection pool detects when it is run in a development nix shell and uses this port instead of a production unix socket. This way, redis tests pass even on computers without redis installed.
This commit is contained in:
parent
3ce71b0993
commit
2e9cdf15ab
|
@ -3,6 +3,7 @@ Redis pool module for selfprivacy_api
|
|||
"""
|
||||
import redis
|
||||
from selfprivacy_api.utils.singleton_metaclass import SingletonMetaclass
|
||||
from os import environ
|
||||
|
||||
REDIS_SOCKET = "/run/redis-sp-api/redis.sock"
|
||||
|
||||
|
@ -13,10 +14,18 @@ class RedisPool(metaclass=SingletonMetaclass):
|
|||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._pool = redis.ConnectionPool.from_url(
|
||||
f"unix://{REDIS_SOCKET}",
|
||||
decode_responses=True,
|
||||
)
|
||||
if "USE_REDIS_PORT" in environ.keys():
|
||||
self._pool = redis.ConnectionPool(
|
||||
host="127.0.0.1",
|
||||
port=int(environ["USE_REDIS_PORT"]),
|
||||
decode_responses=True,
|
||||
)
|
||||
|
||||
else:
|
||||
self._pool = redis.ConnectionPool.from_url(
|
||||
f"unix://{REDIS_SOCKET}",
|
||||
decode_responses=True,
|
||||
)
|
||||
self._pubsub_connection = self.get_connection()
|
||||
|
||||
def get_connection(self):
|
||||
|
|
|
@ -64,9 +64,14 @@ pkgs.mkShell {
|
|||
buildInputs = [
|
||||
sp-python
|
||||
pkgs.black
|
||||
pkgs.redis
|
||||
];
|
||||
shellHook = ''
|
||||
PYTHONPATH=${sp-python}/${sp-python.sitePackages}
|
||||
# envs set with export and as attributes are treated differently.
|
||||
# for example. printenv <Name> will not fetch the value of an attribute.
|
||||
export USE_REDIS_PORT=6379
|
||||
redis-server --port $USE_REDIS_PORT >/dev/null &
|
||||
# maybe set more env-vars
|
||||
'';
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue