def run(project): task = json.loads(request.form['task']) project_info = { 'name': project, 'status': 'DEBUG', 'script': request.form['script'], } fetch_result = {} start_time = time.time() try: fetch_result = app.config['fetch'](task) response = rebuild_response(fetch_result) module = build_module(project_info, {'debugger': True}) ret = module['instance'].run(module['module'], task, response) except Exception, e: type, value, tb = sys.exc_info() tb = hide_me(tb, globals()) logs = ''.join(traceback.format_exception(type, value, tb)) result = { 'fetch_result': fetch_result, 'logs': logs, 'follows': [], 'messages': [], 'result': None, 'time': time.time() - start_time, }
def _run(self, task, response): self._reset() if isinstance(response, dict): response = rebuild_response(response) process = task.get('process', {}) callback = process.get('callback', '__call__') if not hasattr(self, callback): raise NotImplementedError("self.%s() not implemented!" % callback) function = getattr(self, callback) if not getattr(function, '_catch_status_code_error', False): response.raise_for_status() args, varargs, keywords, defaults = inspect.getargspec(function) if len(args) == 1: # foo(self) return function() elif len(args) == 2: # foo(self, response) return function(response) elif len(args) == 3: # foo(self, response, save) return function(response, process.get('save')) elif len(args) == 4: # foo(self, response, save, task) return function(response, process.get('save'), task) else: raise TypeError( "self.%s() need at least 1 argument and lesser 4 arguments: %s(self, [response], [save], [task])" % (function.__name__, function.__name__))
def run(project): task = json.loads(request.form['task']) project_info = { 'name': project, 'status': 'DEBUG', 'script': request.form['script'], } fetch_result = "" start_time = time.time() try: fetch_result = app.config['fetch'](task) response = rebuild_response(fetch_result) module = build_module(project_info, {'debugger': True}) ret = module['instance'].run(module['module'], task, response) except Exception, e: type, value, tb = sys.exc_info() tb = hide_me(tb, globals()) logs = ''.join(traceback.format_exception(type, value, tb)) result = { 'fetch_result': fetch_result, 'logs': logs, 'follows': [], 'messages': [], 'result': None, 'time': time.time() - start_time, }
def _run(self, task, response): self._reset() if isinstance(response, dict): response = rebuild_response(response) process = task.get('process', {}) callback = process.get('callback', '__call__') if not hasattr(self, callback): raise NotImplementedError("self.%s() not implemented!" % callback) function = getattr(self, callback) if not getattr(function, '_catch_status_code_error', False): response.raise_for_status() return self._run_func(function, response, task)
def on_task(self, task, response): start_time = time.time() try: response = rebuild_response(response) assert 'taskid' in task, 'need taskid in task' project = task['project'] if project not in self.projects: raise LookupError("not such project: %s" % project) project_data = self.projects[project] ret = project_data['instance'].run(project_data['module'], task, response) except Exception, e: logger.exception(e) return False
def run_task(self, module, task, response): """ Processing the task, catching exceptions and logs, return a `ProcessorResult` object """ logger = module.logger result = None exception = None stdout = sys.stdout self.task = task if isinstance(response, dict): response = rebuild_response(response) self.response = response self.save = (task.get('track') or {}).get('save', {}) try: if self.__env__.get('enable_stdout_capture', True): sys.stdout = ListO(module.log_buffer) self._reset() result = self._run_task(task, response) if inspect.isgenerator(result): for r in result: self._run_func(self.on_result, r, response, task) else: self._run_func(self.on_result, result, response, task) except Exception as e: logger.exception(e) exception = e finally: follows = self._follows messages = self._messages logs = list(module.log_buffer) extinfo = self._extinfo save = self.save sys.stdout = stdout self.task = None self.response = None self.save = None module.log_buffer[:] = [] return ProcessorResult(result, follows, messages, logs, exception, extinfo, save)
def _run(self, task, response): self._reset() if isinstance(response, dict): response = rebuild_response(response) process = task.get('process', {}) callback = process.get('callback', '__call__') if not hasattr(self, callback): raise NotImplementedError("self.%s() not implemented!" % callback) function = getattr(self, callback) if not getattr(function, '_catch_status_code_error', False): response.raise_for_status() args, varargs, keywords, defaults = inspect.getargspec(function) if len(args) == 1: # foo(self) return function() elif len(args) == 2: # foo(self, response) return function(response) elif len(args) == 3: # foo(self, response, save) return function(response, process.get('save')) elif len(args) == 4: # foo(self, response, save, task) return function(response, process.get('save'), task) else: raise TypeError("self.%s() need at least 1 argument and lesser 4 arguments: %s(self, [response], [save], [task])" % (function.__name__, function.__name__))