diff --git a/procbuild/server.py b/procbuild/server.py index 163afe5..677c31a 100644 --- a/procbuild/server.py +++ b/procbuild/server.py @@ -15,7 +15,9 @@ app = Flask(__name__) print("Starting up build queue...") +#TODO add logging to these processes subprocess.Popen(['python', '-m', 'procbuild.message_proxy']) +subprocess.Popen(['python', '-m', 'procbuild.test_listen']) submitter = BuildRequestSubmitter() @app.route('/') @@ -30,10 +32,6 @@ def index(): allow_manual_build_trigger=ALLOW_MANUAL_BUILD_TRIGGER) -def monitor_queue(): - print("Launching queue monitoring...") - ## TODO: Add logging to this subprocess - subprocess.Popen(['python', '-m', 'procbuild.test_listen']) def dummy_build(nr): return jsonify({'status': 'fail', 'message': 'Not authorized'}) diff --git a/procbuild/test_listen.py b/procbuild/test_listen.py index a120f28..0425257 100644 --- a/procbuild/test_listen.py +++ b/procbuild/test_listen.py @@ -1,10 +1,11 @@ -import zmq import json import io import codecs import time from multiprocessing import Process +import zmq + from . import MASTER_BRANCH from .message_proxy import OUT from .utils import file_age, log @@ -12,26 +13,30 @@ from .builder import BuildManager -def handle_message(data): - print('Message received:', data) - - -def create_listener_socket(): - ctx = zmq.Context.instance() - socket = ctx.socket(zmq.SUB) - socket.connect(OUT) - - socket.setsockopt(zmq.SUBSCRIBE, 'build_queue'.encode('utf-8')) - return socket - - +class Listener: + def __init__(self): + self.ctx = zmq.Context() + self.socket = self.ctx.socket(zmq.SUB) + self.socket.connect(OUT) + self.socket.setsockopt(zmq.SUBSCRIBE, 'build_queue'.encode('utf-8')) + + def listen(self): + while True: + msg = self.socket.recv_multipart() + target, raw_payload = msg + payload = json.loads(raw_payload.decode('utf-8')) + print('received', payload) + paper_to_build = payload.get('build_paper', None) + _build_worker(paper_to_build) + + def _build_worker(nr): pr_info = get_pr_info() pr = pr_info[int(nr)] age = file_age(status_file(nr)) min_wait = 0.5 if not (age is None or age > min_wait): - log("Did not build paper %d--recently built." % nr) + log(f"Did not build paper {nr}--recently built.") return status_log = status_file(nr) @@ -73,11 +78,5 @@ def killer(process, timeout): if __name__ == "__main__": print('Listening for incoming messages...') - while True: - socket = create_listener_socket() - msg = socket.recv_multipart() - target, raw_payload = msg - payload = json.loads(raw_payload.decode('utf-8')) - print('received', payload) - paper_to_build = payload.get('build_paper', None) - _build_worker(paper_to_build) + listener = Listener() + listener.listen() diff --git a/runserver.py b/runserver.py index aaaf2be..1767625 100755 --- a/runserver.py +++ b/runserver.py @@ -2,7 +2,7 @@ # imports import os -from procbuild.server import app, monitor_queue +from procbuild.server import app from waitress import serve # -- SERVER CONFIGURATION -- (can be overridden from shell) @@ -19,9 +19,7 @@ print('Monitoring build queue...') # Iniitalize queue monitor -monitor_queue() serve(app, host='0.0.0.0', port=os.environ['PORT']) # Without waitress, this is the call: -# # app.run(debug=False, host='0.0.0.0', port=7001)