summaryrefslogtreecommitdiff
path: root/gear/worker.py
blob: b11f78bf558d91a75f025bd14bf891e359e5d3e4 (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
37
38
39
40
41
import gear
worker = gear.Worker('reverser')
worker.addServer('localhost')
worker.registerFunction("reverse")
worker.registerFunction("build-graph")
worker.registerFunction("build-artifact")
import time
import json
from subprocess import Popen, PIPE, STDOUT

import distbuild

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)
    elif job.name == "build-artifact":
        artifact = distbuild.decode_artifact_reference(job.arguments)
        print "building %s" % artifact.name
        job.sendWorkComplete(artifact.cache_key)