summaryrefslogtreecommitdiff
path: root/gear/worker.py
blob: 35281ca3c0bc0600d5272d14cb2cfd239db5c567 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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)