]> TLD Linux GIT Repositories - tld-builder.git/blob - TLD_Builder/load_balancer.py
- fix urllib imports
[tld-builder.git] / TLD_Builder / load_balancer.py
1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
2
3 import string
4 import os
5 import time
6
7 import path
8 import sys
9 import log
10 import status
11 import lock
12 import loop
13 import util
14
15 import rpm_builder
16
17 from config import config, init_conf
18
19 # return list of binary builders in fair-queue order
20 # it is determined based upon spool/got_lock file, which is also
21 # updated to be short
22 def builders_order():
23     bs = {}
24     bl = []
25     for b in config.binary_builders:
26         bs[b] = 0
27         bl.append(b)
28
29     lck = lock.lock("got-lock")
30     f = open(path.got_lock_file, "r+")
31     line_no = 0
32
33     for l in f:
34         line_no += 1
35         b = l.strip()
36         if b in bs:
37             bs[b] = line_no
38         else:
39             log.alert("found strange lock in got-lock: %s" % b)
40
41     def mycmp(b1, b2):
42         return util.cmp(bs[b1], bs[b2])
43
44     bl.sort(key=util.cmp_to_key(mycmp))
45
46     f.seek(0)
47     f.truncate(0)
48     for l in bl: f.write(l + "\n")
49     f.close()
50     lck.close()
51
52     return bl
53
54 def run_rpm_builder(b):
55     if os.fork() == 0:
56         return
57     else:
58         rpm_builder.main_for(b)
59         sys.exit(0)
60
61 def main():
62     init_conf()
63     for b in builders_order():
64         run_rpm_builder(b)
65         # give builder some time to aquire lock
66         time.sleep(1)
67     # wait for children to die out
68     try:
69         while 1: os.wait()
70     except:
71         pass
72
73 if __name__ == '__main__':
74     loop.run_loop(main)