diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a23b5fc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,24 @@ +FROM python:3.8-alpine +WORKDIR /app + +# set env +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED 1 + +# install required packages +RUN pip install -U supervisor \ + && mkdir -p /var/log/supervisor \ + && apk add --no-cache ffmpeg + +# install dependencies +ADD ./requirements.txt /app/requirements.txt +RUN pip install -r requirements.txt + +# add user +RUN addgroup -S concierge && adduser -S concierge -G concierge + +# add code +ADD . /app + +# aaaaand go! +CMD ["/usr/local/bin/supervisord", "-c","/app/supervisord.conf"] diff --git a/main.py b/main.py index 5ac7977..eac86cc 100644 --- a/main.py +++ b/main.py @@ -1,17 +1,36 @@ -from celery import Celery +import os +import signal +import sys +import time + +# supervisord xml-rpc connection from xmlrpc.client import ServerProxy - -server = ServerProxy('http://localhost:9001/RPC2') -app = Celery('tasks', broker='redis://localhost') - -print(server.supervisor.getState()) +svd = ServerProxy('http://127.0.0.1:9001/RPC2') +identity = os.environ.get('CONCIERGE_IDENTITY', default="develop") -@app.task -def start_restream(name): - print(name) +def sigterm_handler(signum, frame): + print("concierge shutting down.") + # if concierge dies, all tasks need to die as well! + + sys.exit(0) -@app.task -def stop_restream(name): - print(name) +def loop(config): + while True: + # do stuff + print(svd.supervisor.getAllProcessInfo()) + + time.sleep(1) + + +def main(): + # program setup + signal.signal(signal.SIGTERM, sigterm_handler) + + # check connection to supervisord + print(svd.supervisor.getState()) + loop() + + +main() diff --git a/requirements.txt b/requirements.txt index d23fa5d..522f202 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ celery>=4.4 +redis diff --git a/setup.cfg b/setup.cfg index 1e30e03..f6d9df0 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,3 +1,3 @@ [flake8] -exclude = .git,*migrations* +exclude = .git max-line-length = 120 diff --git a/supervisord.conf b/supervisord.conf new file mode 100644 index 0000000..343d0d3 --- /dev/null +++ b/supervisord.conf @@ -0,0 +1,27 @@ +[supervisord] +logfile=/var/log/supervisord.log ; (main log file;default $CWD/supervisord.log) +logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB) +logfile_backups=10 ; (num of main logfile rotation backups;default 10) +loglevel=info ; (log level;default info; others: debug,warn,trace) +nodaemon=true ; (start in foreground if true;default false) +user=root + +[inet_http_server] +port=127.0.0.1:9001 + +[rpcinterface:supervisor] +supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface + +[program:concierge] +directory=/app +command=/usr/local/bin/python main.py +autostart=true +autorestart=true +priority=5 +stdout_events_enabled=true +stderr_events_enabled=true +stderr_logfile=/dev/stderr +stderr_logfile_maxbytes=0 +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +user=concierge