def _await_nailgun_server(self, workunit): nailgun_timeout_seconds = 5 max_socket_connect_attempts = 10 start = time.time() endpoint = self._get_nailgun_endpoint() while endpoint is None: if time.time() - start > nailgun_timeout_seconds: raise NailgunError('Failed to read ng output after %s seconds' % nailgun_timeout_seconds) time.sleep(0.1) endpoint = self._get_nailgun_endpoint() port = endpoint[1] nailgun = self._create_ngclient(port, workunit) self.context.log.debug('Detected ng server up on port %d' % port) attempt = 0 while attempt < max_socket_connect_attempts: sock = nailgun.try_connect() if sock: sock.close() self.context.log.debug('Connected to ng server pid: %d @ port: %d' % endpoint) return nailgun attempt += 1 self.context.log.debug('Failed to connect on attempt %d' % attempt) time.sleep(0.1) raise NailgunError('Failed to connect to ng after %d connect attempts' % max_socket_connect_attempts)
def _await_nailgun_server(self, workunit): nailgun_timeout_seconds = 5 max_socket_connect_attempts = 10 nailgun = None port_parse_start = time.time() with _safe_open(self._ng_out, 'r') as ng_out: while not nailgun: started = ng_out.readline() if started: port = self._parse_nailgun_port(started) with open(self._pidfile, 'a') as pidfile: pidfile.write(':%d\n' % port) nailgun = self._create_ngclient(port, workunit) log.debug('Detected ng server up on port %d' % port) elif time.time() - port_parse_start > nailgun_timeout_seconds: raise NailgunError( 'Failed to read ng output after %s seconds' % nailgun_timeout_seconds) attempt = 0 while nailgun: sock = nailgun.try_connect() if sock: sock.close() log.info('Connected to ng server pid: %d @ port: %d' % self._get_nailgun_endpoint()) return nailgun elif attempt > max_socket_connect_attempts: raise NailgunError( 'Failed to connect to ng output after %d connect attempts' % max_socket_connect_attempts) attempt += 1 log.debug('Failed to connect on attempt %d' % attempt) time.sleep(0.1)
def _run_cmd(cmd): runcmd = cmd + ' && echo "\n${PIPESTATUS[*]}"' popen = subprocess.Popen(runcmd, shell=True, executable='/bin/bash', bufsize=-1, close_fds=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) (stdout_data, _) = popen.communicate() stdout_data_lines = [ line for line in stdout_data.strip().split('\n') if line ] if not stdout_data_lines: raise NailgunError('No output for command (%s)' % runcmd) try: # Get the return codes of each piped cmd. piped_return_codes = [ int(x) for x in stdout_data_lines[-1].split(' ') if x ] except ValueError: raise NailgunError('Failed to parse result (%s) for command (%s)' % (stdout_data_lines, cmd)) # Drop the echoing of PIPESTATUS, which our caller doesn't care about. stdout_data_lines = stdout_data_lines[:-1] failed = any(piped_return_codes) if failed: raise NailgunError('Failed to execute cmd: "%s". Exit codes: %s. Output: "%s"' %\ (cmd, piped_return_codes, ''.join(stdout_data_lines))) return stdout_data_lines
def _parse_nailgun_port(self, line): match = NailgunTask._PARSE_NG_PORT.match(line) if not match: raise NailgunError( 'Failed to determine spawned ng port from response line: %s' % line) return int(match.group(1))