1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
23 from bqueue import B_Queue
24 from config import config, init_conf
28 def alarmalarm(signum, frame):
29 raise IOError, 'TCP connection hung'
31 def has_new(control_url):
33 cnt_f = open(path.last_req_no_file)
35 last_count = int(string.strip(cnt_f.readline()))
41 socket.setdefaulttimeout(240)
42 signal.signal(signal.SIGALRM, alarmalarm)
45 headers = { 'Cache-Control': 'no-cache', 'Pragma': 'no-cache' }
46 req = urllib2.Request(url=control_url + "/max_req_no", headers=headers)
47 f = urllib2.urlopen(req)
48 count = int(string.strip(f.readline()))
52 log.error("can't fetch %s: %s" % (control_url + "/max_req_no", e))
55 if count != last_count:
60 def fetch_queue(control_url):
61 signal.signal(signal.SIGALRM, alarmalarm)
62 socket.setdefaulttimeout(240)
65 headers = { 'Cache-Control': 'no-cache', 'Pragma': 'no-cache' }
66 req = urllib2.Request(url=control_url + "/queue.gz", headers=headers)
67 f = urllib2.urlopen(req)
71 log.error("can't fetch %s: %s" % (control_url + "/queue.gz", e))
73 sio = StringIO.StringIO()
77 f = gzip.GzipFile(fileobj = sio)
80 except struct.error, e:
81 log.alert("corrupted fetched queue.gz file")
83 (signers, body) = gpg.verify_sig(fdata)
84 u = acl.user_by_email(signers)
86 log.alert("queue.gz not signed with signature of valid user: %s" % signers)
88 if not u.can_do("sign_queue", "all"):
89 log.alert("user %s is not allowed to sign my queue" % u.login)
91 return request.parse_requests(body)
93 def handle_reqs(builder, reqs):
94 qpath = path.queue_file + "-" + builder
95 if not os.access(qpath, os.F_OK):
96 util.append_to(qpath, "<queue/>\n")
101 if r.kind != 'group':
102 raise Exception, 'handle_reqs: fatal: huh? %s' % r.kind
105 if builder in b.builders:
108 log.notice("queued %s (%d) for %s" % (r.id, r.no, builder))
114 lck = lock.lock("request_fetcher", non_block = True)
120 status.push("fetching requests")
121 if has_new(config.control_url):
122 q = fetch_queue(config.control_url)
128 if r.no > last_count:
130 for b in config.binary_builders:
131 handle_reqs(b, q_new)
132 f = open(path.last_req_no_file, "w")
133 f.write("%d\n" % max_no)
138 if __name__ == '__main__':
139 # http connection is established (and few bytes transferred through it)
140 # each $secs seconds.
141 loop.run_loop(main, secs = 10)