def wait(self): try: # Communicate with process self.logger.debug('Communicating with %s (pid: %s)', self.interpreter, self.pid) while time.time() - self.start_time < self.exec_timeout: if self._proc_poll() is None: time.sleep(0.5) else: # Process terminated self.logger.debug('Process terminated') self.return_code = self._proc_complete() break else: # Process timeouted self.logger.debug( 'Timeouted: %s seconds. Killing process %s (pid: %s)', self.exec_timeout, self.interpreter, self.pid) self.return_code = self._proc_kill() if not os.path.exists(self.stdout_path): open(self.stdout_path, 'w+').close() if not os.path.exists(self.stderr_path): open(self.stderr_path, 'w+').close() elapsed_time = time.time() - self.start_time self.logger.debug( 'Finished %s' '\n %s' '\n 1: %s' '\n 2: %s' '\n return code: %s' '\n elapsed time: %s', self.interpreter, self.exec_path, format_size(os.path.getsize(self.stdout_path)), format_size(os.path.getsize(self.stderr_path)), self.return_code, elapsed_time) ret = dict(stdout=binascii.b2a_base64( get_truncated_log(self.stdout_path)), stderr=binascii.b2a_base64( get_truncated_log(self.stderr_path)), time_elapsed=elapsed_time, script_name=self.name, script_path=self.exec_path, event_name=self.event_name or '', return_code=self.return_code, event_server_id=self.event_server_id, event_id=self.event_id) return ret except: if threading.currentThread().name != 'MainThread': self.logger.exception('Exception in script execution routine') else: raise finally: f = os.path.dirname(self.exec_path) if os.path.exists(f): shutil.rmtree(f)
def wait(self): try: # Communicate with process self.logger.debug('Communicating with %s (pid: %s)', self.interpreter, self.pid) while time.time() - self.start_time < self.exec_timeout: if self._proc_poll() is None: time.sleep(0.5) else: # Process terminated self.logger.debug('Process terminated') self.return_code = self._proc_complete() break else: # Process timeouted self.logger.debug('Timeouted: %s seconds. Killing process %s (pid: %s)', self.exec_timeout, self.interpreter, self.pid) self.return_code = self._proc_kill() if not os.path.exists(self.stdout_path): open(self.stdout_path, 'w+').close() if not os.path.exists(self.stderr_path): open(self.stderr_path, 'w+').close() elapsed_time = time.time() - self.start_time self.logger.debug('Finished %s' '\n %s' '\n 1: %s' '\n 2: %s' '\n return code: %s' '\n elapsed time: %s', self.interpreter, self.exec_path, format_size(os.path.getsize(self.stdout_path)), format_size(os.path.getsize(self.stderr_path)), self.return_code, elapsed_time) ret = dict( stdout=binascii.b2a_base64(get_truncated_log(self.stdout_path)), stderr=binascii.b2a_base64(get_truncated_log(self.stderr_path)), time_elapsed=elapsed_time, script_name=self.name, script_path=self.exec_path, event_name=self.event_name or '', return_code=self.return_code, event_server_id=self.event_server_id, event_id=self.event_id ) return ret except: if threading.currentThread().name != 'MainThread': self.logger.exception('Exception in script execution routine') else: raise finally: f = os.path.dirname(self.exec_path) if os.path.exists(f): shutil.rmtree(f)
def wait(self): try: # Communicate with process self.logger.debug('Communicating with %s (pid: %s)', self.interpreter, self.pid) while time.time() - self.start_time < self.exec_timeout: if self._proc_poll() is None: if __node__['running']: time.sleep(0.5) else: raise KeyboardInterrupt() else: # Process terminated self.logger.debug('Process terminated') self.return_code = self._proc_complete() break else: # Process timed out self.return_code = self._proc_kill() if not os.path.exists(self.stdout_path): open(self.stdout_path, 'w+').close() if not os.path.exists(self.stderr_path): open(self.stderr_path, 'w+').close() self.elapsed_time = time.time() - self.start_time self.logger.debug( 'Finished %s' '\n %s' '\n 1: %s' '\n 2: %s' '\n return code: %s' '\n elapsed time: %s', self.interpreter, self.exec_path, format_size(os.path.getsize(self.stdout_path)), format_size(os.path.getsize(self.stderr_path)), self.return_code, self.elapsed_time) except KeyboardInterrupt: raise except: if threading.currentThread().name != 'MainThread': self.logger.exception('Exception in script execution routine') else: raise finally: # Remove finished scripts passed as code if not self.path and __node__['running']: f = os.path.dirname(self.exec_path) if os.path.exists(f): shutil.rmtree(f)
def wait(self): try: # Communicate with process self.logger.debug('Communicating with %s (pid: %s)', self.interpreter, self.pid) while time.time() - self.start_time < self.exec_timeout: time.sleep(5) if self._proc_poll() is None: time.sleep(0.5) else: # Process terminated self.logger.debug('Process terminated') self.return_code = self._proc_complete() break else: # Process timed out self.return_code = self._proc_kill() if not os.path.exists(self.stdout_path): open(self.stdout_path, 'w+').close() if not os.path.exists(self.stderr_path): open(self.stderr_path, 'w+').close() self.elapsed_time = time.time() - self.start_time self.logger.debug('Finished %s' '\n %s' '\n 1: %s' '\n 2: %s' '\n return code: %s' '\n elapsed time: %s', self.interpreter, self.exec_path, format_size(os.path.getsize(self.stdout_path)), format_size(os.path.getsize(self.stderr_path)), self.return_code, self.elapsed_time) except: if threading.currentThread().name != 'MainThread': self.logger.exception('Exception in script execution routine') else: raise finally: if not self.path: f = os.path.dirname(self.exec_path) if os.path.exists(f): shutil.rmtree(f)