-
Notifications
You must be signed in to change notification settings - Fork 1
/
dispatcher.py
executable file
·146 lines (128 loc) · 5.28 KB
/
dispatcher.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import argparse
import os
import re
import socket
import socketserver
import time
import threading
import helpers
def serve():
parser = argparse.ArgumentParser()
parser.add_argument("--host",
help="dispatcher's host, by default it uses localhost",
default="localhost",
action="store")
parser.add_argument("--port",
help="dispatcher's port, by default it uses 8888",
default=8888,
action="store")
args = parser.parse_args()
server = ThreadingTCPServer((args.host, int(args.port)), DispatcherHandler)
print('serving on {}:{}'.format(args.host, int(args.port)))
def runner_checker(server):
def manage_commit_lists(runner):
for commit, assigned_runner in server.dispatched_commits.items():
if assigned_runner == runner:
del server.dispatched_commits[commit]
server.pending_commits.append(commit)
break
server.runners.remove(runner)
while not server.dead:
time.sleep(1)
for runner in server.runners:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
response = helpers.communicate(runner["host"],
int(runner["port"]),
"ping")
if response != "pong":
print("removing runner {}".format(runner))
manage_commit_lists(runner)
except socket.error as e:
manage_commit_lists(runner)
def redistribute(server):
while not server.dead:
for commit in server.pending_commits:
print("running redistribute")
print(server.pending_commits)
dispatch_tests(server, commit)
time.sleep(5)
runner_heartbeat = threading.Thread(target=runner_checker, args=(server,))
redistributor = threading.Thread(target=redistribute, args=(server,))
try:
runner_heartbeat.start()
redistributor.start()
server.serve_forever()
except (KeyboardInterrupt, Exception):
server.dead = True
runner_heartbeat.join()
redistributor.join()
def dispatch_tests(server, commit_id):
while True:
print("trying to dispatch to runners")
for runner in server.runners:
response = helpers.communicate(runner["host"],
int(runner["port"]),
"runtest:{}".format(commit_id))
if response == "OK":
print("adding id {}".format(commit_id))
server.dispatched_commits[commit_id] = runner
if commit_id in server.pending_commits:
server.pending_commits.remove(commit_id)
return
time.sleep(2)
class ThreadingTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
runners = []
dead = False
dispatched_commits = {}
pending_commits = []
class DispatcherHandler(socketserver.BaseRequestHandler):
command_re = re.compile(r"(\w+)(:.+)*")
BUF_SIZE = 1024
def handle(self):
self.data = self.request.recv(self.BUF_SIZE).strip()
command_groups = self.command_re.match(self.data.decode())
if not command_groups:
self.request.sendall(b"Invalid command")
return
command = command_groups.group(1)
if command == "status":
print("in status")
self.request.sendall(b"OK")
elif command == "register":
print("register")
address = command_groups.group(2)
host, port = re.findall(r":(\w*)", address)
runner = {"host": host, "port":port}
self.server.runners.append(runner)
self.request.sendall(b"OK")
elif command == "dispatch":
print("going to dispatch")
commit_id = command_groups.group(2)[1:]
if not self.server.runners:
self.request.sendall(b"No runners are registered")
else:
self.request.sendall(b"OK")
dispatch_tests(self.server, commit_id)
elif command == "results":
print("got test results")
results = command_groups.group(2)[1:]
results = results.split(":")
commit_id = results[0]
length_msg = int(results[1])
remaining_buffer = self.BUF_SIZE - \
(len(command) + len(commit_id) + len(results[1]) + 3)
if length_msg > remaining_buffer:
self.data += self.request.recv(length_msg - remaining_buffer).strip()
del self.server.dispatched_commits[commit_id]
if not os.path.exists("test_results"):
os.makedirs("test_results")
with open("test_results/{}".format(commit_id), "w") as f:
data = self.data.decode().split(":")[3:]
data = "\n".join(data)
f.write(data)
self.request.sendall(b"OK")
if __name__ == "__main__":
serve()