def serve(): parser = argparse.ArgumentParser() parser.add_argument("--host", help="test runner's host, by default it uses localhost", default="localhost", action="store") parser.add_argument("--port", help="test runner's port, by default it uses 8999", default=8999, action="store") parser.add_argument("--dispatcher-server", help="dispatcher host:port, " \ "by default it uses localhost:8888", default="localhost:8888", action="store") parser.add_argument("repo", metavar="REPO", type=str, help="path to the repository this will run test") args = parser.parse_args() server = ThreadingTCPServer((args.host, int(args.port)), TestHandler) dispatcher_host, dispatcher_port = args.dispatcher_server.split(":") server.dispatcher_server = {"host": dispatcher_host, "port": dispatcher_port} server.repo_folder = args.repo print("serving on % s: % s" % (args.host, int(args.port))) helpers.communicate(dispatcher_host, int(dispatcher_port), "register:%s:%s" % (args.host, int(args.port))) print("registered to % s: % s" % (dispatcher_host, dispatcher_port)) dispatcher_check = threading.Thread(target=dispatcher_checker, args=(server,)) try: dispatcher_check.start() # Activate the server; this will keep running until you # interrupt the program with Ctrl+C or Cmd+C server.serve_forever() except (KeyboardInterrupt, Exception): # if any exception occurs, kill the thread server.dead = True dispatcher_check.join()
def poll(): parser = argparse.ArgumentParser() parser.add_argument("--dispatcher-server", help="dispatcher host:port, by default it uses localhost:8888", default="localhost:8888" action="store") parser.add_argument("repo", metavar="REPO" type=str, help="Path to the repository this woll observe") args = parser.parse_args() dispatcher_host, dispatcher_port = args.dispatcher_server.split(":") while True: try: # call the bash script that will update the repo and check # for changes. If there's a change, it will drop a .commit_id file # with the latest commit in the current working directory subprocess.check_output(["./update_repo.sh", args.repo]) except subprocess.CalledProcessError as e: raise Exception("Could not update and check repository. Reason: %s" % e.output) if os.path.isfile(".commit-id"): # great, we have a change! let's execute the tests # First, check the status of the dispatcher server to see # if we can send the tests try: response = helpers.communicate(dispatcher_host, int(dispatcher_port), "status") except socket.error as e: raise Exception("Could not communicate with dispatcher server: %s" % e) if "OK" in response: commit = "" with open(".commit_id", "r") as f: commit = f.readline() reponse = helpers.communicate(dispatcher_host, int(dispatcher_port), "dispatch:%s" % commit) if "OK" not in reponse: raise Exception("Could not dispatch the test: %s" % reponse) print "Tests dispatched!" else: raise Exception("Could not dispatch the test: %s" % reponse) time.sleep(5)
def run_tests(self, commit_id, test_runner_clone_repo): # Update repo subprocess.check_output( ['test_runner_script.sh', test_runner_clone_repo, commit_id], shell=True) result_file = open('results.txt', 'a') result_file.write('=' * 70 + '\n') # Run the tests test_folder = os.path.join(test_runner_clone_repo, 'tests') # repo_folder + tests if os.path.exists(test_folder): suite = unittest.TestLoader().discover(test_folder) unittest.TextTestRunner(result_file).run(suite) else: result_file.write('No test folder directory\n') result_file.write('=' * 70 + '\n') result_file.close() # Give the dispatcher the results result_file = open('results.txt', 'r') output = result_file.read() helpers.communicate(self.server.dispatcher_host, self.server.dispatcher_port, f'results:{commit_id}:{len(output)}:{output}') result_file.close() os.remove('results.txt')
def poll(): parser = argparse.ArgumentParser() parser.add_argument("--dispatcher-server", help="dispatcher host:port, " \ "by default it uses localhost:8888", default="localhost:8888", action="store") parser.add_argument("repo", metavar="REPO", type=str, help="path to the repository this will observe") args = parser.parse_args() dispatcher_host, dispatcher_port = args.dispatcher_server.split(":") while True: # 更新repo,获取repo的哈希值,保存到.conmmit_hash try: # call the bash script that will update the repo and check # for changes. If there's a change, it will drop a .commit_hash file # with the latest commit in the current working directory subprocess.check_output(["./update_repo.sh", args.repo]) except subprocess.CalledProcessError as e: raise Exception("Could not update and check repository. Reason: %s" % e.output) # 检查.commit_hash文件,如存在,说明repo更新了 if os.path.isfile(".commit_hash"): # great, we have a change! let's execute the tests # First, check the status of the dispatcher server to see # if we can send the tests try: response = helpers.communicate(dispatcher_host, int(dispatcher_port), "status") except socket.error as e: raise Exception("Could not communicate with dispatcher server: %s" % e) if response == "OK": # Dispatcher is present, let's send it a test commit = "" with open(".commit_hash", "r") as f: commit = f.readline() # 将hash值分发到指定IP的指定端口 response = helpers.communicate(dispatcher_host, int(dispatcher_port), "dispatch:%s" % commit) if response != "OK": raise Exception("Could not dispatch the test: %s" % response) print "dispatched!" else: # Something wrong happened to the dispatcher raise Exception("Could not dispatch the test: %s" % response) time.sleep(5)
def run_tests(self, commit_id, repo_folder): output = subprocess.check_output(["./test_runner_script.sh", repo_folder, commit_id]) print(output) test_folder = os.path.join(repo_folder, "tests") suite = unittest.TestLoader().discover(test_folder) result_file = open("results", "w") unittest.TextTestRunner(result_file).run(suite) result_file.close() result_file = open("results", "r") output = result_file.read() helpers.communicate(self.server.dispatcher_server["host"], int(self.server.dispatcher_server["port"]), "results:{}:{}:{}".format(commit_id, len(output), output))
def commit_observe(): '''parse command line arguments, checking repo's changes''' parser = argparse.ArgumentParser() parser.add_argument("--forwarder-server", help="forwarder ussage host:port, " \ "default is: localhost:8878", default="localhost:8878", action="store" ) parser.add_argument("repo", metavar="REPO", type=str, help="path to the repo this will monitor") args = parser.parse_args() forwarder_host, forwarder_port = args.forwarder_server.split(":") '''infinite non-blocking loop .sh script compares commits and return commit id ''' while True: try: subprocess.check_output(["./update_repo.sh", args.repo]) except subprocess.CalledProcessError as e: raise Exception("not able to update and check the repo. " + "Reason: %s" % e.output) if os.path.isfile(".commit_hash"): try: response = helpers.communicate(forwarder_host, int(forwarder_port), "status") except socket.error as e: raise Exception("can't talk to forwarder server: %s" % e) if response == "OK": commit = "" with open(".commit_hash", "r") as f: commit = f.readline() response = helpers.communicate(forwarder_host, int(forwarder_port), "forwarder:%s" % commit) if response != "OK": raise Exception("can't forward the test: %s" % response) print("forwarded") else: raise Exception("can't forward the test: %s" % response) time.sleep(5)
def poll(): parser = argparse.ArgumentParser() parser.add_argument("--dispatcher-server", help="dispatcher host:port, " \ "by default it uses localhost:8888", default="localhost:8888", action="store") parser.add_argument("repo", metavar="REPO", type=str, help="path to the repository this will observe") args = parser.parse_args() dispatcher_host, dispatcher_port = args.dispatcher_server.split(":") while True: try: # call the bash script that will update the repo and check # for changes. If there's a change, it will drop a .commit_hash file # with the latest commit in the current working directory subprocess.check_output(["./update_repo.sh", args.repo]) except subprocess.CalledProcessError as e: raise Exception( "Could not update and check repository. Reason: %s" % e.output) if os.path.isfile(".commit_hash"): # great, we have a change! let's execute the tests # First, check the status of the dispatcher server to see # if we can send the tests try: response = helpers.communicate(dispatcher_host, int(dispatcher_port), "status") except socket.error as e: raise Exception( "Could not communicate with dispatcher server: %s" % e) if response == "OK": # Dispatcher is present, let's send it a test commit = "" with open(".commit_hash", "r") as f: commit = f.readline() response = helpers.communicate(dispatcher_host, int(dispatcher_port), "dispatch:%s" % commit) if response != "OK": raise Exception("Could not dispatch the test: %s" % response) print("dispatched!") else: # Something wrong happened to the dispatcher raise Exception("Could not dispatch the test: %s" % response) time.sleep(5)
def run_tests(self, commit_id, repo_folder): # update repo self.update_repo(repo_folder, commit_id) # run the tests test_folder = os.path.join(repo_folder, "tests") suite = unittest.TestLoader().discover(test_folder) result_file = open("results", "w") unittest.TextTestRunner(result_file).run(suite) result_file.close() result_file = open("results", "r") # give the dispatcher the results output = result_file.read() helpers.communicate(self.server.dispatcher_server["host"], int(self.server.dispatcher_server["port"]), "results:%s:%s:%s" % (commit_id, len(output), output))
def test_checker(server): def namage_commit_lists(runner): for commit, assigned_runner in \ server.forwarded_commits.interitems(): if assigned_runner == runner: del server.dorwarded_commits[commit] server.waiting_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(f"removing runner {runner}") manage_commit_lists(runner) except: pass
def serve(): parser = argparse.ArgumentParser() parser.add_argument("--host", help="dispathcer's host, by default it uses localhost", default="localhost", action="store") parser.add_argument("--port", heml="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 %s:%s" % (args.host, int(args.port)) def runner_checker(server): def manage_commit_lists(runner): for commit, assigned_runner in server.dispatched_commut.iteritems(): 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 "pong" not in response: print "removing runner %s" % runner mange_commit_lists(runner) except socket.error as e: manage_commit_losts(runner)
def commit_observe(): parser = argparse.ArgumentParser() parser.add_argument( '--forwarder-server', help='forwarder ussage host:port, default is: localhost: 8878', default='localhost:8878', action='store') parser.add_argument('repo', metavar='REPO', type=str, help='path to the repo this will monitor') args = parser.parse_args() forwarder_host, forwarder_port = args.forwarder_server.split(':') while True: try: subprocess.check_output(['./update_repo.sh', args.repo]) except subprocess.CalledProcessError as e: raise Exception('not able to update and check the repo. Reason: ' + e.output) if os.path.isfile('.commit_hash'): try: response = helpers.communicate(forwarder_host, int(forwarder_port), 'status') except socket.error as e: raise Exception("can't talk to forwarder server: %s" % e) if response == 'OK': commit = '' with open('.commit_hash', 'r') as f: commit = f.readline() response = helpers.communicate(forwarder_host, int(forwarder_port), "forward: %s" % commit) if response != 'OK': raise Exception("can't forward the test: %s" % response) print('forwarded') else: raise Exception("can't forward the test: %s" % response) time.sleep(5)
def run_tests(self, commit_id, repo_folder): # update repo output = subprocess.check_output(["./test_runner_script.sh", repo_folder, commit_id]) print output # run the tests test_folder = os.path.join(repo_folder, "tests") suite = unittest.TestLoader().discover(test_folder) result_file = open("results", "w") unittest.TextTestRunner(result_file).run(suite) result_file.close() result_file = open("results", "r") # give the dispatcher the results output = result_file.read() helpers.communicate(self.server.dispatcher_server["host"], int(self.server.dispatcher_server["port"]), "results:%s:%s:%s" % (commit_id, len(output), output))
def poll(): parser = argparse.ArgumentParser() parser.add_argument( "--dispatcher-server", help="dispatcher host:port by default use localhost:8888", default="localhost:8888", action="store") parser.add_argument("repo", metavar="REPO", type=str, help="path to the repository this will observe") args = parser.parse_args() dispatcher_host, dispatcher_port = args.dispatcher_server.split(":") while True: try: subprocess.check_output(["./update_repo.sh", args.repo]) except subprocess.CalledProcessError as e: raise Exception( "Could not update and check repository, Reason: {}".format( e.output)) if os.path.isfile(".commit_id"): try: response = helpers.communicate(dispatcher_host, dispatcher_port, "status") except socket.error as e: raise Exception( "Could not communicate with dispatcher server: {}".format( e)) if response == "OK": with open(".commit_id", "r") as f: commit = f.readline() response = helpers.communicate(dispatcher_host, dispatcher_port, "dispatcher:{}".format(commit)) if response != "OK": raise Exception( "Could not dispatch the test: {}".format(response)) print("dispatched") else: raise Exception( "Could not dispatch the test: {}".format(response)) time.sleep(5)
def push_commit_to_dispatcher(dispatcher_host:str, dispatcher_port:int, commit:str): ''' Check the status of the dispatcher server to see if we can send the commit and if everything is ok send it. ''' try: response = helpers.communicate(dispatcher_host, dispatcher_port, 'status') except error: raise Exception(f'Could not communicate with dispatcher server: {error}') if response == 'OK': response = helpers.communicate(dispatcher_host, dispatcher_port, 'dispatch:'+commit) if response == 'OK': print('commit dispatched') else: raise Exception(f'Could not dispatch the test: {response}') else: raise Exception(f'Could not dispatch the test: {response}')
def poll(): args = parse_args() dispatcher_host, dispatcher_port = args.dispatcher_server.split(':') while True: # delegates finding latest commit to a bash script try: subprocess.check_output(['./update_repo.sh', args.repo]) except subprocess.CalledProcessError as e: raise Exception('Could not update and check repo. Reason: %s' % e.output) # if commit found ... if os.path.isfile('.commit_id'): with open('.commit_id', 'r') as f: commit = f.readline() print 'Current commit: %s' % commit # ping the dispatcher (handle errors) try: response = helpers.communicate(dispatcher_host, int(dispatcher_port), 'status') except socket.error as e: raise Exception( 'Could not communicate with dispatcher server %s' % e) if response == 'OK': commit = '' with open('.commit_id', 'r') as f: commit = f.readline().strip() # tell dispatcher about latest commit (handle errors) response = helpers.communicate(dispatcher_host, int(dispatcher_port), 'dispatch:%s' % commit) if response != "OK": raise Exception("Could not dispatch the test: %s" % response) print "dispatched!" else: raise Exception("Could not dispatch the test: %s" % response) # sleep for 5 and try again time.sleep(5)
def run_tests(self, commit_hash, repo_folder): # update repo output = subprocess.check_output( ["./test_runner_script.sh", repo_folder, commit_hash]) print output # run the tests test_folder = os.path.join(repo_folder, "tests") suite = unittest.TestLoader().discover(test_folder) result_file = open("results", "w") unittest.TextTestRunner(result_file).run(suite) result_file.close() result_file = open("results", "r") # give the dispatcher the results output = result_file.read() helpers.communicate( self.server.dispatcher_server["host"], int(self.server.dispatcher_server["port"]), "results:%s:%s:%s" % (commit_hash, len(output), output))
def server(): parser = argparse.ArgumentParser() parser.add_argument("--host", help="runners host, default it uses localhost", default="localhost", action="store") parser.add_argument("--port", help="runner port,default it uses 8900", default=8900, action="store") parser.add_argument("--dispatcher-server", help="dispatcher host:port, byd default it uses 'localhost:8000'", default="localhost:8888", action="store") parser.add_argument("repo", metavar="REPO", type=str, help="path to the repository this will observe") args = parser.parse_args() dispatcher_host, dispatcher_port = args.dispatcher_server.split(":") server = ThreadingTCPServer((args.host, int(args.port)), TestHandler) server.repo_folder = args.repo server.dispatcher_server = {"host": dispatcher_host, "port": dispatcher_port} response = helpers.communicate(server.dispatcher_server["host"], int(server.dispatcher_server["port"]), "register:%s:%s"%(args.host, args.port)) if response != "OK": raise Exception("can't register with dispatcher!") print 'test runner serving on %s:%s' % (args.host, int(args.port)) def dispatcher_checker(server): while not server.dead: time.sleep(5) if (time.time() - server.last_communication) > 10: try: response = helpers.communicate(server.dispatcher_server["host"], int(server.dispatcher_server["port"], "status")) if response != "OK": print "Dispatcher is no longer functional" server.shutdown() return except socket.error as e: print "can't communicate with dispatcher: %s" % e server.shutdown() return t = threading.Thread(target=dispatcher_checker, args=(server,)) try: t.start() server.serve_forever() except (KeyboardInterrupt, Exception): server.dead = True t.join()
def poll(self): parser = argparse.ArgumentParser() parser.add_argument("--dispatcher-server", help="dispatcher host:port, " \ "by default it uses localhost:8888", default="localhost:8888", action="store") parser.add_argument("repo", metavar="REPO", type=str, help="path to the repository this will observe") args = parser.parse_args() dispatcher_host, dispatcher_port = args.dispatcher_server.split(":") while True: try: # call the bash script that will update the repo and check # for changes. If there's a change, it will drop a .commit_id file # with the latest commit in the current working directory self.update_repo(args.repo) except Exception as e: raise Exception(e) if os.path.isfile(".commit_id"): try: response = helpers.communicate(dispatcher_host, int(dispatcher_port), "status") except socket.error as e: raise Exception( "Could not communicate with dispatcher server: %s" % e) if response == "OK": with open(".commit_id", "r") as f: commit = f.readline() response = helpers.communicate(dispatcher_host, int(dispatcher_port), "dispatch:%s" % commit) if response != "OK": raise Exception("Could not dispatch the test: %s" % response) print("dispatched!") else: raise Exception("Could not dispatch the test: %s" % response) time.sleep(5)
def run_tests(self, commit_id, repo_folder): output = subprocess.check_output( ['./test_runner_script.sh', repo_folder, commit_id]) print output test_folder = os.path.join(repo_folder, 'tests') suite = unittest.TestLoader().discover(test_folder) result_file = open('results', 'w') unittest.TextTestRunner(result_file).run(suite) result_file.close() result_file = open('results', 'r') output = result_file.read() helpers.communicate( self.server.dispatcher_server['host'], int(self.server.dispatcher_server['port']), 'results:%s:%s:%s' % (commit_id, len(output), output))
def commit_observe(): parser = argparse.ArgumentParser() parser.add_argument("--forwarder-server", help="forwareder usage host:port \ default is:localhost:8878", default="localhost:8878", action="store") parser.add_argument("repo", metavar="REPO", type=str, help="path to the repo which needs monitoring") args = parser.parse_args() forwarder_host, forwarder_port = args.forwarder_server.split(":") while True: try: subprocess.check_output(["./update_repo.sh", args.repo]) except subprocess.CalledProcessError as e: raise Exception("not able to update and check the repo." + "Reason %s" % e.output) if os.path.isfile(".commit_hash"): try: response = helpers.communicate(forwarder_host, int(forwarder_port), "status") except socket.error as e: raise Exception("can't talk to the forwarder server: %s" % e) if response == "OK": commit = "" with open(".commit_hash", "r") as f: commit = f.readline() response = helpers.communicate(forwarder_host, int(forwarder_port), "forward:%s" % commit) if response != "OK": raise Exception("can't forward the test %s" % response) print "forwarded" else: raise Exception("can't forward the test: %s" % response) time.sleep()
def run_tests(self, commit_hash, repo_folder): # update repo output = subprocess.check_output(["./test_runner_script.sh %s %s" % (repo_folder, commit_hash)], shell=True) print output # run the tests test_folder = os.path.sep.join([repo_folder, "tests"]) suite = unittest.TestLoader().discover(test_folder) result_file = open("results", "w") unittest.TextTestRunner(result_file).run(suite) result_file.close() result_file = open("results", "r") # give the dispatcher the location of the results # NOTE: typically, we upload results to the result server, # which will be used by a webinterface output = result_file.read() helpers.communicate(self.server.dispatcher_server["host"], int(self.server.dispatcher_server["port"]), "results:%s:%s" % (commit_hash, output))
def dispatch_tests(self, commit_hash): # NOTE: usually we don't run this forever while True: print "trying to dispatch to runners" for runner in self.server.runners: response = helpers.communicate(runner["host"], int(runner["port"]), "runtest:%s" % commit_hash) if response == "OK": return time.sleep(2)
def dispatch_tests(server, commit_id): while True: print 'trying to dispatch runners' for runner in server.runners: response = helpers.communicate(runner['host'], int(runner['port']), 'runtest:%s' % commit_id) if response == 'OK': print 'adding id %s' % 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)
def poll(dispatcher_server, poll_interval, repo): dispatcher_host, dispatcher_port = dispatcher_server.split(":") while True: try: # call the bash script that will update the repo and check # for changes. If there's a change, it will drop a .commit_id file # with the latest commit in the current working directory subprocess.check_output(["./update_repo.sh", repo]) except subprocess.CalledProcessError as e: raise Exception( "Could not update and check repository. Reason: %s" % e.output) if os.path.isfile(".commit_id"): # great, we have a change! let's execute the tests # First, check the status of the dispatcher server to see # if we can send the tests try: response = helpers.communicate(dispatcher_host, int(dispatcher_port), "status:heartbeat") except socket.error as e: raise Exception( "Could not communicate with dispatcher server: %s" % e) if response == "OK": # Dispatcher is present, let's send it a test commit = "" with open(".commit_id", "r") as f: commit = f.readline() response = helpers.communicate(dispatcher_host, int(dispatcher_port), "dispatch:%s" % commit) if response != "OK": raise Exception("Could not dispatch the test: %s" % response) print("dispatched!") else: # Something wrong happened to the dispatcher raise Exception("Could not dispatch the test: %s" % response) time.sleep(poll_interval)
def _runner_checker(self): while not self.server.dead: time.sleep(1) for runner in self.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 %s" % runner) self._manage_commit_lists(runner) except Exception: self._manage_commit_lists(runner)
def dispatch_into_tests(server, commit_hash): while True: print('trying to forward to executor...') for runner in server.runners: response = helpers.communicate(runner['host'], int(runner['port']), 'runtest: %s' % commit_hash) if response == 'OK': print('adding hash %s' % commit_hash) server.forwarded_commits[commit_hash] = runner if commit_hash in server.waiting_commits: server.waiting_commits.remove(commit_hash) return time.sleep(2)
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:%s" % commit_id) if response == "OK": print("adding id %s" % 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)
def dispatch_into_tests(server, commit_hash): while True: print "Trying to forward to executor..." for runner in server.runners: response = helpers.communicate( runner["port"], int(runner["port"], "runtest:%s" % commit_hash)) if response == "OK": print "adding hash %s" % commit_hash server.forwarded_commits[commit_hash] = runner if commit_hash in server.waiting_commits: server.waiting_commits.remove(commit_hash) return time.sleep(2)
def dispatch_tests(server, commit_id): # NOTE: usually we don't run these forever while True: print "Trying to dispatch to runners" for runner in server.runners: response = helpers.communicate(runner["host"], int(runner["port"]), "runtest:%s" % commit_id) if "OK" in response: print "adding id %s" % commit_id server.dispatcher_commits[commit_id] = runner if commit_id in server.pending_commits: server.pending_commits.remove(commit_id) return time.sleep(2)
def dispatch_tests(server, commit_hash): # NOTE: usually we don't run this forever while True: print("trying to dispatch to runners") for runner in server.runners: response = helpers.communicate(runner["host"], int(runner["port"]), "runtest:%s" % commit_hash) if response == "OK": print("adding hash %s" % commit_hash) server.dispatched_commits[commit_hash] = runner if commit_hash in server.pending_commits: server.pending_commits.remove(commit_hash) return time.sleep(2)
def dispatch_tests(server, commit_hash): # NOTE: usually we don't run this forever while True: print "trying to dispatch to runners" for runner in server.runners: response = helpers.communicate(runner["host"], int(runner["port"]), "runtest:%s" % commit_hash) if response == "OK": print "adding hash %s" % commit_hash server.dispatched_commits[commit_hash] = runner if commit_hash in server.pending_commits: server.pending_commits.remove(commit_hash) return time.sleep(2)
def serve(host, port, dispatcher_server, repo): range_start = 8900 runner_host = host runner_port = None tries = 0 if port < 0: runner_port = range_start while tries < 100: try: server = ThreadingTCPServer((runner_host, runner_port), TestHandler) print server print runner_port break except socket.error as e: if e.errno == errno.EADDRINUSE: tries += 1 runner_port = runner_port + tries continue else: raise e else: raise Exception("Could not bind to ports in range %s-%s" % (range_start, range_start+tries)) else: runner_port = int(port) server = ThreadingTCPServer((runner_host, runner_port), TestHandler) server.repo_folder = repo dispatcher_host, dispatcher_port = dispatcher_server.split(":") server.dispatcher_server = {"host":dispatcher_host, "port":dispatcher_port} response = helpers.communicate(server.dispatcher_server["host"], int(server.dispatcher_server["port"]), "register:%s:%s" % (runner_host, runner_port)) if response != "OK": raise Exception("Can't register with dispatcher!") t = threading.Thread(target=dispatcher_checker, args=(server,)) try: t.start() # Activate the server; this will keep running until you # interrupt the program with Ctrl-C server.serve_forever() except (KeyboardInterrupt, Exception): # if any exception occurs, kill the thread server.dead = True t.join()
def dispatcher_checker(server): while not server.dead: time.sleep(5) if (time.time() - server.last_communication) > 10: try: response = helpers.communicate(server.dispatcher_server["host"], int(server.dispatcher_server["port"], "status")) if response != "OK": print "Dispatcher is no longer functional" server.shutdown() return except socket.error as e: print "can't communicate with dispatcher: %s" % e server.shutdown() return
def dispatcher_checker(server): while not server.dead: time.sleep(5) if (time.time() - server.latest) > 10: try: response = helpers.communicate( server.dispatcher_server['host'], int(server.dispatcher_server['port']), 'status') if response != 'OK': print 'Dispatcher is no longer functional' server.shutdown() return except socket.error as e: print 'Cannot communicate with dispatcher: %s' % e server.shutdown() return
def dispatcher_checker(server): while not server.dead: time.sleep(5) if (time.time() - server.last_communication) > 5: try: response = helpers.communicate( server.dispatcher_server["host"], int(server.dispatcher_server["port"]), "status") if response != "OK": print "Dispatcher is no longer functional" server.shutdown() return except socket.error as e: print "Can't communicate with dispatcher: %s" % e server.shutdown() return
def runner_checker(server): #NOTE:mention that we can do timeout based kills (if BUSY for too long # etc) 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 %s" % runner server.runners.remove(runner) except socket.error as e: print "removing runner %s" % runner server.runners.remove(runner)
def dispatcher_checker(test_runner_server): # Checks if the dispatcher went down. # If it is down, we will shut down if since the dispatcher may not have the same host/port when it comes back up. while test_runner_server.is_serving: time.sleep(5) if (time.time() - test_runner_server.last_communication) > 10: try: response = helpers.communicate( test_runner_server.dispatcher_host, test_runner_server.dispatcher_port, 'status') if response != 'OK': print('Dispatcher is no longer functional') test_runner_server.shutdown() return except socket.error as e: print(f'Can\'t communicate with dispatcher: {e}') test_runner_server.shutdown() return
def dispatcher_checker(server): # Checks if the dispatcher went down. If it is down, we will shut down # if since the dispatcher may not have the same host/port # when it comes back up. while not server.dead: time.sleep(5) if (time.time() - server.last_communication) > 10: try: response = helpers.communicate( server.dispatcher_server["host"], int(server.dispatcher_server["port"]), "status") if response != "OK": print "Dispatcher is no longer functional" server.shutdown() return except socket.error as e: print "Can't communicate with dispatcher: %s" % e server.shutdown() return
def dispatcher_checker(server): # Checks if the dispatcher went down. If it is down, we will shut down # if since the dispatcher may not have the same host/port # when it comes back up. while not server.dead: time.sleep(5) if (time.time() - server.last_communication) > 10: try: response = helpers.communicate( server.dispatcher_server["host"], int(server.dispatcher_server["port"]), "status" ) if response != "OK": print "Dispatcher is no longer functional" server.shutdown() return except socket.error as e: print "Can't communicate with dispatcher: %s" % e server.shutdown() return
def dispatch_tests(dispatcher_server, commit_id): ''' Push commit to free test runner''' # This function should not run forever, because we should have enough free test runners while True: print('trying to push commit to free test runner') for runner in dispatcher_server.runners: response = helpers.communicate(runner['host'], runner['port'], 'runtest:'+commit_id) if response == 'OK': # Free test runner found -> # 1) add manage commit and its runner to dispatched commits dictionary # 2) remove commit from pending commits list print(f'adding commit id {commit_id}') dispatcher_server.dispatched_commits[commit_id] = runner if commit_id in dispatcher_server.pending_commits: dispatcher_server.pending_commits.remove(commit_id) return print('no free test runners') sleep(2)
def runner_checker(server): def manage_commit_lists(runner): for commit, assigned_runner in server.dispatched_commits.iteritems(): 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: try: response = helpers.communicate(runner["host"], int(runner["port"]), "ping") if response != "pong": print("removing runner %s" % runner) manage_commit_lists(runner) except socket.error as e: manage_commit_lists(runner)
def runner_checker(server): def manage_commit_lists(runner): for commit, assigned_runner in server.dispatched_commits.iteritems(): 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: try: response = helpers.communicate(runner["host"], int(runner["port"]), "ping") if response != "pong": print "removing runner %s" % runner manage_commit_lists(runner) except socket.error as e: manage_commit_lists(runner)
def serve(): range_start = 8900 parser = argparse.ArgumentParser() parser.add_argument("--host", help="runner's host, by default it uses localhost", default="localhost", action="store") parser.add_argument("--port", help="runner's port, by default it uses values >=%s" % range_start, action="store") parser.add_argument("--dispatcher-server", help="dispatcher host:port, by default it uses " \ "localhost:8888", default="localhost:8888", action="store") parser.add_argument("repo", metavar="REPO", type=str, help="path to the repository this will observe") args = parser.parse_args() runner_host = args.host runner_port = None tries = 0 if not args.port: runner_port = range_start while tries < 100: try: server = ThreadingTCPServer((runner_host, runner_port), TestHandler) print server print runner_port break except socket.error as e: if e.errno == errno.EADDRINUSE: tries += 1 runner_port = runner_port + tries continue else: raise e else: raise Exception("Could not bind to ports in range %s-%s" % (range_start, range_start+tries)) else: runner_port = int(args.port) server = ThreadingTCPServer((runner_host, runner_port), TestHandler) server.repo_folder = args.repo dispatcher_host, dispatcher_port = args.dispatcher_server.split(":") server.dispatcher_server = {"host":dispatcher_host, "port":dispatcher_port} response = helpers.communicate(server.dispatcher_server["host"], int(server.dispatcher_server["port"]), "register:%s:%s" % (runner_host, runner_port)) if response != "OK": raise Exception("Can't register with dispatcher!") def dispatcher_checker(server): # Checks if the dispatcher went down. If it is down, we will shut down # if since the dispatcher may not have the same host/port # when it comes back up. while not server.dead: time.sleep(5) if (time.time() - server.last_communication) > 10: try: response = helpers.communicate( server.dispatcher_server["host"], int(server.dispatcher_server["port"]), "status") if response != "OK": print "Dispatcher is no longer functional" server.shutdown() return except socket.error as e: print "Can't communicate with dispatcher: %s" % e server.shutdown() return t = threading.Thread(target=dispatcher_checker, args=(server,)) try: t.start() # Activate the server; this will keep running until you # interrupt the program with Ctrl-C server.serve_forever() except (KeyboardInterrupt, Exception): # if any exception occurs, kill the thread server.dead = True t.join()