def stop_self(self): """ 线程自停逻辑,即10分钟内没有新任务,会停止 :return: """ class _StopSelfThread(threading.Thread): def __init__(self, service): super().__init__() self._event = threading.Event() self._event.set() self.service = service self.daemon = True def run(self) -> None: self._event.clear() self._event.wait(self.service.stop_self_timeout) if not self.service.has_unfinished_tasks(): self.service._working = False self.service._stopping_self = False def resume(self): self._event.set() if not self._stopping_self: self._stopping_self = True logger.debug('任务池自停') self._self_stop_thread = _StopSelfThread(self) self._self_stop_thread.start()
def inner(*args, **kwargs): threading.Thread(name=name, target=func, args=args, kwargs=kwargs, daemon=daemon).start() if name: logger.debug(f'开启线程:{name}')
def _sub_finish(self, sub): for listener in self._listeners: listener.on_sub_finished(task=self, sub=sub) self.__sub_finish_count += 1 if self.__sub_finish_count >= len( self.subs) and self._status == PAUSED: logger.debug(f'{self.__sub_finish_count}') self.task_handler.resume()
def wait_sub_finished(self): """ 等待子任务完成 :return: """ logger.debug('等待子任务完成') self.pause(include_subs=False) self.task_handler.wait()
def run_fn(self, *args, **kwargs): try: self.logger.debug(f'执行测试:{self.__test_name}') self.fn(*args, **kwargs) for listener in self._listeners: listener.on_test_passed(self, *args, **kwargs) except: logger.debug(traceback.format_exc()) for listener in self._listeners: listener.on_test_failed(self, *args, **kwargs)
def resume(self, *args, **kwargs): if self.status != PAUSED: return False if self.__subs_paused: for sub in self.subs: sub.resume() logger.debug(f'唤醒任务:{self.id}') self.task_handler.resume() for listener in self._listeners: listener.on_resumed(self, *args, **kwargs) return True
def status(self, value): self._status = value for listener in self._listeners: if not isinstance(listener, TaskListener): continue method = getattr(listener, f'on_status_changed') if method: method(task=self, status=value) if value == FINISHED and self.parent: self.parent._sub_finish(sub=self) logger.debug(f'task {value}: {self.id}')
def set_status(self, status, value, *args, **kwargs): """""" try: self.logger.debug( f'设置状态:{status.name or status.__class__.__name__} -> {value}') status.set_value(value, *args, **kwargs) except: logger.debug(traceback.format_exc()) for listener in self._listeners: listener.on_test_blocked(self, *args, **kwargs) # 检查之前的测试里面哪个没擦好屁股 for record in self._status_fail_records: if record.status == status: pass # TODO 记录擦屁股失败
def _request(self, request_fn, api_url, addl_keys, data, _headers=False): url = self._get_url(api_url, self._url_args) # print "%s %s, data=%s" % (request_fn.__name__.upper(), url, str(data)) self._session = self._session or requests.Session() if request_fn in ['get', 'head'] and data: url = url + '?' + urlencode(data, doseq=True) data = None url = url[:-1] if url.endswith('?') else url if not isinstance(self, GitLab): url = url + ('&' if '?' in url else '?') + 'ref=' + self._branch logger.debug(url) resp = self._session.request(method=request_fn, url=url, headers=self._headers, data=data, **self._requests_kwargs) resp.raise_for_status() return resp.json()
def wrapper(*args, **kwargs): try: return func(*args, **kwargs) except errors: logger.debug(traceback.format_exc())