1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
22 from bqueue import B_Queue
23 from config import config, init_conf
27 def alarmalarm(signum, frame):
28 raise IOError, 'TCP connection hung'
30 def has_new(control_url):
32 cnt_f = open(path.last_req_no_file)
34 last_count = int(string.strip(cnt_f.readline()))
40 socket.setdefaulttimeout(240)
41 signal.signal(signal.SIGALRM, alarmalarm)
44 headers = { 'Cache-Control': 'no-cache', 'Pragma': 'no-cache' }
45 req = urllib2.Request(url=control_url + "/max_req_no", headers=headers)
46 f = urllib2.urlopen(req)
47 count = int(string.strip(f.readline()))
51 log.error("can't fetch %s: %s" % (control_url + "/max_req_no", e))
54 if count != last_count:
59 def fetch_queue(control_url):
60 signal.signal(signal.SIGALRM, alarmalarm)
61 socket.setdefaulttimeout(240)
64 headers = { 'Cache-Control': 'no-cache', 'Pragma': 'no-cache' }
65 req = urllib2.Request(url=control_url + "/queue.gz", headers=headers)
66 f = urllib2.urlopen(req)
70 log.error("can't fetch %s: %s" % (control_url + "/queue.gz", e))
72 sio = StringIO.StringIO()
76 f = gzip.GzipFile(fileobj = sio)
77 (signers, body) = gpg.verify_sig(f.read())
78 u = acl.user_by_email(signers)
80 log.alert("queue.gz not signed with signature of valid user: %s" % signers)
82 if not u.can_do("sign_queue", "all"):
83 log.alert("user %s is not allowed to sign my queue" % u.login)
85 return request.parse_requests(body)
87 def handle_reqs(builder, reqs):
88 qpath = path.queue_file + "-" + builder
89 if not os.access(qpath, os.F_OK):
90 util.append_to(qpath, "<queue/>\n")
96 raise Exception, 'handle_reqs: fatal: huh? %s' % r.kind
99 if builder in b.builders:
102 log.notice("queued %s (%d) for %s" % (r.id, r.no, builder))
108 lck = lock.lock("request_fetcher", non_block = True)
114 status.push("fetching requests")
115 if has_new(config.control_url):
116 q = fetch_queue(config.control_url)
122 if r.no > last_count:
124 for b in config.binary_builders:
125 handle_reqs(b, q_new)
126 f = open(path.last_req_no_file, "w")
127 f.write("%d\n" % max_no)
132 if __name__ == '__main__':
133 # http connection is established (and few bytes transferred through it)
134 # each $secs seconds.
135 loop.run_loop(main, secs = 10)