| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """The registration server used to register tasks. | 5 """The registration server used to register tasks. |
| 6 | 6 |
| 7 The registration server is started by the test controller and allows the tasks | 7 The registration server is started by the test controller and allows the tasks |
| 8 to register themselves when they start. Authentication of the tasks controllers | 8 to register themselves when they start. Authentication of the tasks controllers |
| 9 is based on an OTP passed to the run_task binary on startup. | 9 is based on an OTP passed to the run_task binary on startup. |
| 10 """ | 10 """ |
| 11 | 11 |
| 12 import logging | 12 import logging |
| 13 import threading | 13 import threading |
| 14 | 14 |
| 15 from legion.lib import common_lib | 15 from legion.lib import common_lib |
| 16 from legion.lib.rpc import SimpleJSONRPCServer | 16 from legion.lib.rpc import SimpleJSONRPCServer |
| 17 | 17 |
| 18 | 18 |
| 19 class TaskRegistrationServer(object): | 19 class TaskRegistrationServer(object): |
| 20 """Discovery server run on the host.""" | 20 """Discovery server run on the host.""" |
| 21 | 21 |
| 22 def __init__(self): | 22 def __init__(self): |
| 23 self._expected_tasks = {} | 23 self._expected_tasks = {} |
| 24 self._rpc_server = None | 24 self._rpc_server = None |
| 25 self._thread = None | 25 self._thread = None |
| 26 self._port = common_lib.GetUnusedPort() | 26 self._port = common_lib.GetUnusedPort() |
| 27 # Register for the shutdown event | |
| 28 common_lib.OnShutdown += self.Shutdown | |
| 29 | 27 |
| 30 @property | 28 @property |
| 31 def port(self): | 29 def port(self): |
| 32 return self._port | 30 return self._port |
| 33 | 31 |
| 34 def _RegisterTaskRPC(self, otp, ip, port): | 32 def _RegisterTaskRPC(self, otp, ip, port): |
| 35 """The RPC used by a task to register with the registration server.""" | 33 """The RPC used by a task to register with the registration server.""" |
| 36 assert otp in self._expected_tasks | 34 assert otp in self._expected_tasks |
| 37 cb = self._expected_tasks.pop(otp) | 35 cb = self._expected_tasks.pop(otp) |
| 38 cb(ip, port) | 36 cb(ip, port) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 50 self._rpc_server.register_function( | 48 self._rpc_server.register_function( |
| 51 self._RegisterTaskRPC, 'RegisterTask') | 49 self._RegisterTaskRPC, 'RegisterTask') |
| 52 self._thread = threading.Thread(target=self._rpc_server.serve_forever) | 50 self._thread = threading.Thread(target=self._rpc_server.serve_forever) |
| 53 self._thread.start() | 51 self._thread.start() |
| 54 | 52 |
| 55 def Shutdown(self): | 53 def Shutdown(self): |
| 56 """Shuts the discovery server down.""" | 54 """Shuts the discovery server down.""" |
| 57 if self._thread and self._thread.is_alive(): | 55 if self._thread and self._thread.is_alive(): |
| 58 logging.info('Shutting down task registration server') | 56 logging.info('Shutting down task registration server') |
| 59 self._rpc_server.shutdown() | 57 self._rpc_server.shutdown() |
| OLD | NEW |