def connect_to_master(self, master_url=None): """ Notify the master that this slave exists. :param master_url: The URL of the master service. If none specified, defaults to localhost:43000. :type master_url: str | None """ self.is_alive = True self._master_url = master_url or 'localhost:43000' self._master_api = UrlBuilder(self._master_url) connect_url = self._master_api.url('slave') data = { 'slave': '{}:{}'.format(self.host, self.port), 'num_executors': self._num_executors, 'session_id': SessionId.get() } response = self._network.post(connect_url, data=data) self._slave_id = int(response.json().get('slave_id')) self._logger.info('Slave {}:{} connected to master on {}.', self.host, self.port, self._master_url) # We disconnect from the master before build_teardown so that the master stops sending subjobs. (Teardown # callbacks are executed in the reverse order that they're added, so we add the build_teardown callback first.) UnhandledExceptionHandler.singleton().add_teardown_callback( self._do_build_teardown_and_reset, timeout=30) UnhandledExceptionHandler.singleton().add_teardown_callback( self._disconnect_from_master)
def set_default_headers(self): self.set_header('Content-Type', 'application/json') self.set_header(SessionId.SESSION_HEADER_KEY, SessionId.get()) request_origin = self.request.headers.get('Origin') # usually only set when making API request from a browser if request_origin and self._is_request_origin_allowed(request_origin): self.set_header('Access-Control-Allow-Origin', request_origin) self.set_header( 'Access-Control-Allow-Headers', 'Content-Type, Accept, X-Requested-With, Session, Session-Id', ) self.set_header('Access-Control-Allow-Methods', 'GET')
def prepare(self): """ If the request has specified the session id, which is optional, and the session id does not match the current instance's session id, then the client is asking for a resource that has expired and no longer exists. """ session_id = self.request.headers.get(SessionId.SESSION_HEADER_KEY) if session_id is not None and session_id != SessionId.get(): raise PreconditionFailedError('Specified session id: {} has expired and is invalid.'.format(session_id)) super().prepare()
def _check_expected_session_id(self): """ If the request has specified the session id, which is optional, and the session id does not match the current instance's session id, then the requester is asking for a resource that has expired and no longer exists. """ # An expected session header in a *request* should be declared using the "Expected-Session-Id" header # but for legacy support an expected header can also be specified with the "Session-Id" header. session_id = self.request.headers.get(SessionId.SESSION_HEADER_KEY) \ or self.request.headers.get(SessionId.EXPECTED_SESSION_HEADER_KEY) if session_id is not None and session_id != SessionId.get(): raise PreconditionFailedError('Specified session id: {} has expired and is invalid.'.format(session_id))
def prepare(self): """ If the request has specified the session id, which is optional, and the session id does not match the current instance's session id, then the client is asking for a resource that has expired and no longer exists. """ session_id = self.request.headers.get(SessionId.SESSION_HEADER_KEY) if session_id is not None and session_id != SessionId.get(): raise PreconditionFailedError( 'Specified session id: {} has expired and is invalid.'.format( session_id)) super().prepare()
def api_representation(self): """ Gets a dict representing this resource which can be returned in an API response. :rtype: dict [str, mixed] """ executors_representation = [executor.api_representation() for executor in self.executors_by_id.values()] return { 'is_alive': self.is_alive, 'master_url': self._master_url, 'current_build_id': self._current_build_id, 'slave_id': self._slave_id, 'executors': executors_representation, 'session_id': SessionId.get(), }
def api_representation(self): """ Gets a dict representing this resource which can be returned in an API response. :rtype: dict [str, mixed] """ executors_representation = [ executor.api_representation() for executor in self.executors_by_id.values() ] return { 'is_alive': self.is_alive, 'master_url': self._master_url, 'current_build_id': self._current_build_id, 'slave_id': self._slave_id, 'executors': executors_representation, 'session_id': SessionId.get(), }
def application_summary(logfile_count): # todo: move this method to app_info.py """ Return a string summarizing general info about the application. This will be output at the start of every logfile. :param logfile_count: The number of logfiles the application has created during its current execution :type logfile_count: int """ separator = '*' * 50 summary_lines = [ ' ClusterRunner', ' * Version: {}'.format(autoversioning.get_version()), ' * PID: {}'.format(os.getpid()), ' * Session id: {}'.format(SessionId.get()), ] if logfile_count > 1: summary_lines.append(' * Logfile count: {}'.format(logfile_count)) return '\n{0}\n{1}\n{0}\n'.format(separator, '\n'.join(summary_lines))
def application_summary( logfile_count): # todo: move this method to app_info.py """ Return a string summarizing general info about the application. This will be output at the start of every logfile. :param logfile_count: The number of logfiles the application has created during its current execution :type logfile_count: int """ separator = '*' * 50 summary_lines = [ ' ClusterRunner', ' * Version: {}'.format(autoversioning.get_version()), ' * PID: {}'.format(os.getpid()), ' * Session id: {}'.format(SessionId.get()), ] if logfile_count > 1: summary_lines.append(' * Logfile count: {}'.format(logfile_count)) return '\n{0}\n{1}\n{0}\n'.format(separator, '\n'.join(summary_lines))
def connect_to_master(self, master_url=None): """ Notify the master that this slave exists. :param master_url: The URL of the master service. If none specified, defaults to localhost:43000. :type master_url: str | None """ self.is_alive = True self._master_url = master_url or 'localhost:43000' self._master_api = UrlBuilder(self._master_url) connect_url = self._master_api.url('slave') data = { 'slave': '{}:{}'.format(self.host, self.port), 'num_executors': self._num_executors, 'session_id': SessionId.get() } response = self._network.post(connect_url, data=data) self._slave_id = int(response.json().get('slave_id')) self._logger.info('Slave {}:{} connected to master on {}.', self.host, self.port, self._master_url) # We disconnect from the master before build_teardown so that the master stops sending subjobs. (Teardown # callbacks are executed in the reverse order that they're added, so we add the build_teardown callback first.) UnhandledExceptionHandler.singleton().add_teardown_callback(self._do_build_teardown_and_reset, timeout=30) UnhandledExceptionHandler.singleton().add_teardown_callback(self._disconnect_from_master)
def test_get_should_return_same_string_on_repeated_calls(self): session_id = SessionId.get() self.assertEquals(session_id, SessionId.get())
def set_default_headers(self): """ Inject the session id in the header. """ self.set_header(SessionId.SESSION_HEADER_KEY, SessionId.get()) super().set_default_headers()