Commit 553fee62 authored by BAIRE Anthony's avatar BAIRE Anthony
Browse files

add a redis client in the controller

parent 52a9e3ec
......@@ -3,7 +3,8 @@ FROM allgo/base-debian
RUN apt-getq install python3-websocket python3-six python3-requests \
python3-mysqldb python3-sqlalchemy python3-fasteners \
python3-nose python3-coverage libjs-jquery python3-yaml \
python3-termcolor python3-iso8601 python3-docker
python3-termcolor python3-iso8601 python3-docker \
python3-aioredis
COPY files/. /
......@@ -25,6 +26,7 @@ ENV PORT="4567" \
SANDBOX_NETWORK="allgo_sandboxes" \
DOCKER_HOST="unix:///run/docker.sock" \
SWARM_HOST="unix:///run/docker.sock" \
MYSQL_HOST="{ENV}-mysql"
MYSQL_HOST="{ENV}-mysql" \
ALLGO_REDIS_HOST="{ENV}-redis"
LABEL dk.migrate_always=1
......@@ -20,6 +20,7 @@ import time
import threading
import traceback
import aioredis
import docker
import iso8601
import MySQLdb
......@@ -56,6 +57,24 @@ NB_JOB_TASKS = 4
default_executor = ThreadPoolExecutor(10)
##################################################
# redis keys
# job log
REDIS_KEY_JOB_LOG = "log:job:%d"
REDIS_KEY_JOB_STATE = "state:job:%d"
# pubsub channels for waking up allgo.aio (frontend) and the controller
# (ourselves)
REDIS_CHANNEL_AIO = "notify:aio"
REDIS_CHANNEL_CONTROLLER = "notify:controller"
# pubsub messages
REDIS_MESSAGE_JOB_UPDATED = "job:%d"
REDIS_MESSAGE_WEBAPP_UPDATED = "webapp:%d"
##################################################
log = logging.getLogger("controller")
......@@ -1490,7 +1509,7 @@ class ImageManager:
class DockerController:
def __init__(self, sandbox_host, swarm_host, mysql_host,
port, registry, env, datastore_path, sandbox_path,
toolbox_path, sandbox_network,
toolbox_path, sandbox_network, redis_host,
config_file="/vol/ro/config.yml",
):
......@@ -1528,6 +1547,8 @@ class DockerController:
connect_db(mysql_host))
self._thread_local = threading.local()
self.redis_host = redis_host
self.redis_client = None
self.sock = socket.socket()
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
......@@ -1732,6 +1753,10 @@ class DockerController:
yield from self.db_startup()
try:
# note: this call never fails (even if the redis server is down)
self.redis_client = yield from aioredis.create_reconnecting_redis(
(self.redis_host, 6379))
yield from self._shutdown_requested
finally:
# graceful shutdown
......
......@@ -136,6 +136,10 @@ def main():
mysql_host = val.format(ENV=env)
log.info("mysql host %s", mysql_host)
with get_envvar("ALLGO_REDIS_HOST") as val:
redis_host = val.format(ENV=env)
log.info("redis host %s", redis_host)
os.makedirs("/vol/cache", exist_ok=True)
lockfile = "/vol/cache/controller.lock"
lock = fasteners.InterProcessLock(lockfile)
......@@ -147,7 +151,7 @@ def main():
return controller.DockerController(docker_host, swarm_host, mysql_host,
port, registry, env, datastore_path, sandbox_path,
toolbox_path, sandbox_network).run()
toolbox_path, sandbox_network, redis_host).run()
except config_reader.ConfigError:
log.critical("bad config")
sys.exit(1)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment