summaryrefslogtreecommitdiff
path: root/gear/worker.py
blob: d1e961aa8f498bd820a270eb4a68d1a6fa11218f (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
        cmd = ['morph', 'worker-build', '--build-log-on-stdout', artifact.name]
        p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
        output = p.communicate(input=job.arguments)[0]
        job.sendWorkData(output)

        kind = artifact.kind

        if kind == 'chunk':
            artifact_names = artifact.source_artifact_names

            suffixes = ['%s.%s' % (kind, name) for name in artifact_names]
            suffixes.append('build-log')
        else:
            filename = '%s.%s' % (kind, job.artifact.name)
            suffixes = [filename]

            if kind == 'stratum':
                suffixes.append(filename + '.meta')

        job.sendWorkComplete(artifact.cache_key)