Skip to content

Commit

Permalink
cleanup listener, remove monitor_queue, directly open listen subprocess
Browse files Browse the repository at this point in the history
  • Loading branch information
mpacer committed Mar 20, 2018
1 parent 9e4836c commit 92e15f1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
6 changes: 2 additions & 4 deletions procbuild/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('/')
Expand All @@ -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'})
Expand Down
45 changes: 22 additions & 23 deletions procbuild/test_listen.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
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
from .pr_list import get_pr_info, status_file, cache
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)
Expand Down Expand Up @@ -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()
4 changes: 1 addition & 3 deletions runserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

0 comments on commit 92e15f1

Please sign in to comment.