import gear worker = gear.Worker('reverser') worker.addServer('localhost') worker.registerFunction("reverse") worker.registerFunction("build-graph") import time import json from subprocess import Popen, PIPE, STDOUT while True: print "DEBUG: Waiting for job" job = worker.getJob() print "DEBUG: Received job '%s'" % job.name if job.name == "reverse": print "DEBUG: Starting job reverse with '%s'" % job.arguments for x in range(0, 100): job.sendWorkData("This is: %s" % x) job.sendWorkComplete("answer") elif job.name == "build-graph": bg_request=json.loads(job.arguments) print ("DEBUG: Starting build-graph calculation for Repo: '%s' " "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() # TODO: catch errors calculating build-graph here instead of sending the error as build-graph :) print "=====" print output print "=====" print "DEBUG: finished computing build graph" job.sendWorkComplete(output)