Skip to content

Commit

Permalink
format docstrings, remove prefix, create target_set, remove print
Browse files Browse the repository at this point in the history
remove unused imports
  • Loading branch information
mpacer committed Apr 8, 2018
1 parent c38889d commit 3005978
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 67 deletions.
18 changes: 12 additions & 6 deletions procbuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,18 @@ class BuildManager:
Parameters
----------
user: str; name of GitHub user
branch: str; name of git branch on user's PR
target: str; string representation of integer for which paper to build
cache: str; cache directory in which the build tools and final paper live
master_branch: str; git branch for build tools
log: function; logging function
user : str
name of GitHub user
branch : str
name of git branch on user's PR
target : str
string representation of integer for which paper to build
cache : str
cache directory in which the build tools and final paper live
master_branch : str, optional
git branch for build tools, defaults to "master"
log : function
logging function
"""

def __init__(self,
Expand Down
81 changes: 36 additions & 45 deletions procbuild/listener.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import json
import io
import codecs
import time
import asyncio

from multiprocessing import Process
from concurrent.futures import ThreadPoolExecutor

import zmq
Expand All @@ -20,58 +18,53 @@
class Listener:
""" Listener class for defining zmq sockets and maintaining a build queue.
Attributes:
Attributes
------------
ctx: zmq.asyncio.Context, the main context for the listener class
prefix: str, the prefix listened for by zmq sockets
socket: zmq.socket, the socket for listening to
queue: asyncio.Queue, the queue for holding the builds
dont_build: set, unique collection of PRs currently in self.queue
ctx : zmq.asyncio.Context
main context for the listener class
socket : zmq.socket
the socket for listening to
queue : asyncio.Queue
the queue for holding the builds
dont_build : set
unique collection of PRs currently in self.queue
Note: Only modify self.dont_build within synchronous blocks.
"""

def __init__(self, prefix='build_queue'):
"""
Parameters:
------------
build_queue: str, the prefix that will be checked for by the zmq socket
"""
def __init__(self):
self.ctx = Context.instance()
self.prefix = prefix
target_set = {'build_queue'}

self.socket = self.ctx.socket(zmq.SUB)
self.socket.connect(OUT)
self.socket.setsockopt(zmq.SUBSCRIBE, self.prefix.encode('utf-8'))
for target in target_set:
self.socket.setsockopt(zmq.SUBSCRIBE, target.encode('utf-8'))

self.queue = asyncio.Queue()
self.dont_build = set()

async def listen(self):
"""Listener method, containing while loop for checking socket
"""
while True:
msg = await 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)

if self.check_age_and_queue(paper_to_build):
if self.check_age(paper_to_build) or self.check_queue(paper_to_build):
continue
self.dont_build.add(paper_to_build)
await self.queue.put(paper_to_build)

def check_age(self, nr):
"""Check the age of a PR's status_file based on its number.
Parameters:
Parameters
------------
nr: int, the number of the PR in order of receipt
nr : int
the number of the PR in order of receipt
"""
age = file_age(status_file(nr))
min_wait = 0.5
Expand All @@ -84,31 +77,24 @@ def check_age(self, nr):
def check_queue(self, nr):
"""Check whether the queue currently contains a build request for a PR.
Parameters:
Parameters
------------
nr: int, the number of the PR to check
nr : int
the number of the PR to check
"""
in_queue = False
if nr in self.dont_build:
log(f"Did not queue paper {nr}--already in queue.")
in_queue = True
return in_queue

def check_age_and_queue(self, nr):
"""Check whether the PR is old enough or whether it is already in queue.
Parameters:
------------
nr: int, the number of the PR to check
"""
return self.check_age(nr) or self.check_queue(nr)

def report_status(self, nr):
"""prints status notification from status_file for paper `nr`
Parameters:
Parameters
------------
nr: int, the number of the PR to check
nr : int
the number of the PR to check
"""
with io.open(status_file(nr), 'r') as f:
status = json.load(f)['status']
Expand All @@ -122,7 +108,8 @@ def report_status(self, nr):
async def queue_builder(self, loop=None):
"""Manage queue and trigger builds, report results.
loop: asyncio.loop, the loop on which to be running these tasks
loop : asyncio.loop
the loop on which to be running these tasks
"""
while True:
# await an item from the queue
Expand All @@ -136,10 +123,12 @@ async def queue_builder(self, loop=None):
def paper_log(self, nr, record):
"""Writes status to PR's log file
Parameters:
------------
nr: int, the number of the PR to check
record: dict, the dictionary content to be written to the log
Parameters
------------
nr : int
the number of the PR to check
record : dict
the dictionary content to be written to the log
"""
status_log = status_file(nr)
with io.open(status_log, 'wb') as f:
Expand All @@ -148,9 +137,10 @@ def paper_log(self, nr, record):
def build_and_log(self, nr):
"""Builds paper for PR number and logs the resulting status
Parameters:
Parameters
------------
nr: int, the number of the PR to check
nr : int
the number of the PR to check
"""
pr_info = get_pr_info()
pr = pr_info[int(nr)]
Expand All @@ -171,6 +161,7 @@ def build_and_log(self, nr):
status = build_manager.build_paper()
self.paper_log(nr, status)


if __name__ == "__main__":
print('Listening for incoming messages...')

Expand Down
30 changes: 14 additions & 16 deletions procbuild/submitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,18 @@
class BuildRequestSubmitter:
"""Class for submitting build requests to zmq socket.
Attributes:
Parameters
------------
socket: zmq.socket, socket for pushing messages out
verbose: bool, whether to print a message when submitting with builder
verbose : bool, optional
whether to print a message when submitting with builder (default is False)
Attributes
------------
verbose
socket : zmq.socket, socket for pushing messages out
"""

def __init__(self, verbose=False):
"""
Parameters:
------------
verbose: bool, whether to print a message when submitting with builder
defaults to False
"""
ctx = zmq.Context()
self.socket = ctx.socket(zmq.PUSH)
self.socket.connect(IN)
Expand All @@ -31,18 +27,20 @@ def __init__(self, verbose=False):
def construct_message(self, nr):
"""Creates message to be sent on zmq socket.
Parameters:
Parameters
------------
nr: int, the number of the PR in order of receipt
nr : int
the number of the PR in order of receipt
"""
return ['build_queue', json.dumps({'build_paper': nr})]

def submit(self, nr):
"""Submits message to zmq socket
Parameters:
Parameters
------------
nr: int, the number of the PR in order of receipt
nr : int
the number of the PR in order of receipt
"""
message = self.construct_message(nr)
if self.verbose:
Expand Down

0 comments on commit 3005978

Please sign in to comment.