From 1c91cf11c2fa28325059ee1e0bbd20f24051ce3e Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Tue, 5 Jun 2018 15:45:50 +0200 Subject: [PATCH] [3.2-linux] geiles non-java zeug! --- ass3-worker/Dockerfile | 13 ++++++++- ass3-worker/worker.py | 62 +++++++++++++++++++++++++++++++++++++++++- vm/docker-configure.sh | 7 +++++ 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/ass3-worker/Dockerfile b/ass3-worker/Dockerfile index f87f5c1..61afffa 100644 --- a/ass3-worker/Dockerfile +++ b/ass3-worker/Dockerfile @@ -1 +1,12 @@ -# TODO \ No newline at end of file +FROM python:3-slim + +# install stuff +RUN pip install pika + +# Setup worker +RUN mkdir -p /deploy +COPY worker.py /deploy/worker.py + +# Conf worker +USER 1337 +ENTRYPOINT ["python3", "/deploy/worker.py"] diff --git a/ass3-worker/worker.py b/ass3-worker/worker.py index f87f5c1..655efeb 100644 --- a/ass3-worker/worker.py +++ b/ass3-worker/worker.py @@ -1 +1,61 @@ -# TODO \ No newline at end of file +#!/usr/bin/env python3 +# + +import sys +import signal +import pika +import time +import json +import uuid +import random + +workertype = None + +def signal_handler(signum, frame): + sig = dict((k, v) for v, k in reversed(sorted(signal.__dict__.items())) if v.startswith('SIG') and not v.startswith('SIG_')) + print('\n*** '+str(sig[signum])+' received. Graceful shutdown.') + sys.exit(0) + + +def on_message(channel, method_frame, header_frame, body): + print('*** on_message(): '+workertype+'-'+str(method_frame.delivery_tag)) + print(body) + ret = dict() + startmillis = int(round(time.time() * 1000)) + + ret['requestId'] = str(uuid.uuid1()) + + # pseudo-work + if workertype == 'document': + time.sleep(random.randint(3,5)) + if workertype == 'quiz': + time.sleep(random.randint(1,2)) + if workertype == 'video': + time.sleep(random.randint(8,11)) + + ret['processingTime'] = str(int(round(time.time() * 1000))-startmillis) + channel.basic_ack(delivery_tag=method_frame.delivery_tag) + channel.basic_publish('dst.workers', 'requests.'+workertype, json.dumps(ret), pika.BasicProperties()) + + +def mainloop(): + connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.99.99',credentials=pika.PlainCredentials('dst', 'dst'))) + channel = connection.channel() + channel.queue_declare(queue='dst.'+workertype, durable=True, exclusive=False, auto_delete=False) + channel.basic_qos(prefetch_count=1) + channel.basic_consume(on_message, queue='dst.'+workertype) + + print('*** Starting consuming.') + channel.start_consuming() + + +if __name__ == '__main__': + if len(sys.argv) != 2: + print('Takes exactly one parameter: WORKERTYPE') + sys.exit(0) + + workertype = sys.argv[1] + print('*** Starting worker of type: '+workertype) + signal.signal(signal.SIGTERM, signal_handler) + signal.signal(signal.SIGINT, signal_handler) + mainloop() diff --git a/vm/docker-configure.sh b/vm/docker-configure.sh index 2e75a13..44453f7 100644 --- a/vm/docker-configure.sh +++ b/vm/docker-configure.sh @@ -20,3 +20,10 @@ systemctl daemon-reload echo "[docker-configure] Restarting docker" systemctl restart docker + + +# Added docker image handling. +echo "[docker] cleanup and image rebuild" +docker ps -aq | xargs docker rm -f &>/dev/null +docker build -t dst/ass3-worker /dst/ass3-worker/ +docker image prune -f -- 2.43.0