From 2cf7d80e432e1ae527f1b810ecd3e2cd9b0c2dcb Mon Sep 17 00:00:00 2001 From: Pedro Alvarez Date: Mon, 7 Mar 2016 15:25:41 +0000 Subject: Create controller class, and use it Change-Id: I29ff2f937a20f385a07de956045f4b92c4caf7c2 --- gear/client.py | 80 +++++++++++++++++++++++++++++++++------------------------- gear/worker.py | 1 + 2 files changed, 47 insertions(+), 34 deletions(-) diff --git a/gear/client.py b/gear/client.py index 561cd9a7..17202cdd 100644 --- a/gear/client.py +++ b/gear/client.py @@ -10,18 +10,17 @@ UNBUILT = 'not-built' BUILDING = 'building' BUILT = 'built' +import logging +logging.basicConfig() + gear.Server() -class aGearmanClient(gear.Client): +class theController(): def __init__(self): - super(aGearmanClient, self).__init__() - print "init" - self.finished = False + self.graph_client = aGearmanClient(self) + self.artifact = None - def handleWorkComplete(self, packet): - job = super(aGearmanClient, self).handleWorkComplete(packet) - print "workcomplete" - self.finished = True + def _process_build_graph(self, packet): print "Decoding artifact received" self.artifact = distbuild.decode_artifact_reference(job.data[-1]) print "Decoding artifact received done" @@ -30,9 +29,45 @@ class aGearmanClient(gear.Client): def set_initial_state(artifact): artifact.state = UNBUILT print "Setting them as unbuilt" - map_build_graph(self.artifact, set_initial_state) + self._map_build_graph(self.artifact, set_initial_state) print "Setting them as unbuilt done" + def _map_build_graph(self, artifact, callback, components=[]): + """Run callback on each artifact in the build graph and return result. + + If components is given, then only look at the components given and + their dependencies. Also, return a list of the components after they + have had callback called on them. + + """ + result = [] + mapped_components = [] + done = set() + if components: + queue = list(components) + else: + queue = [artifact] + while queue: + a = queue.pop() + if a not in done: + result.append(callback(a)) + queue.extend(a.dependencies) + done.add(a) + if a in components: + mapped_components.append(a) + return result, mapped_components + + +class aGearmanClient(gear.Client): + def __init__(self, controller): + super(aGearmanClient, self).__init__() + self.controller = controller + self.finished = False + + def handleWorkComplete(self, packet): + job = super(aGearmanClient, self).handleWorkComplete(packet) + print "workcomplete" + self.controller._process_build_graph(packet) return job def handleWorkData(self, packet): @@ -57,33 +92,10 @@ class aGearmanClient(gear.Client): -def map_build_graph(artifact, callback, components=[]): - """Run callback on each artifact in the build graph and return result. - - If components is given, then only look at the components given and - their dependencies. Also, return a list of the components after they - have had callback called on them. - - """ - result = [] - mapped_components = [] - done = set() - if components: - queue = list(components) - else: - queue = [artifact] - while queue: - a = queue.pop() - if a not in done: - result.append(callback(a)) - queue.extend(a.dependencies) - done.add(a) - if a in components: - mapped_components.append(a) - return result, mapped_components -client = aGearmanClient() +controller = theController() +client = controller.graph_client client.addServer('localhost') client.waitForServer() # Wait for at least one server to be connected print "server connected" diff --git a/gear/worker.py b/gear/worker.py index d1e876d1..90f0e415 100644 --- a/gear/worker.py +++ b/gear/worker.py @@ -22,6 +22,7 @@ while True: "Ref: '%s' System: '%s'") % (bg_request['repo'], bg_request['ref'], bg_request['system']) + # TODO: There should be another way of doing this. cmd = ['morph', 'calculate-build-graph', '--quiet', bg_request['repo'], bg_request['ref'], bg_request['system']] p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) output = p.stdout.read() -- cgit v1.2.1