def _read_loop(self, resp): logger.debug("_read_loop init") # retry limit for http connecting http_count = 0 while self.running: if resp.isclosed(): break logger.debug("_readloop start(%s)" % str(resp.status)) # read length data = '' while True: try: c = resp.read(1) if c == '\n': http_count = 0 break data += c except SSLError, exception: logger.info("(%s retry:%d):%s" % ("_read_loop", http_count, str(exception))) http_count += 1 if http_count > self.http_retry: logger.error("over retry max") raise NoResponse("break loop") data = data.strip() # read data and pass into listener if self.listener.on_data(data) is False: self.running = False
def worker(self): while True: try: logger.debug("wait..") status = self.evqueue.get(block=True, timeout=None) logger.debug("get by %s" % (status.author.screen_name,)) self.dispatch(status) except TwisterError, err: logger.error(err) except Exception, err: logger.error(err)
def worker(self): while True: try: logger.debug("wait..") status = self.evqueue.get(block=True, timeout=None) logger.debug("get by %s" % (status.author.screen_name, )) self.dispatch(status) except TwisterError, err: logger.error(err) except Exception, err: logger.error(err)
def handle(self): logger.debug("connect by %s" % str(self.client_address)) self.queue_list[self.client_address] = Queue.Queue() while True: try: status = self.queue_list[self.client_address].get(block=True, timeout=None) self.request.send(to_utf8(status.text)) except Exception, err: # エラー発生で切断 logger.error(err) # can do on finish? del self.queue_list[self.client_address] break
def _run(self): # Authenticate url = "%s://%s%s" % (self.scheme, self.host, self.url) # Connect and process the stream error_counter = 0 conn = None exception = None logger.debug(u"run streaming server.") while self.running: # quit if error count greater than retry count if self.retry_count is not None and error_counter > self.retry_count: break try: if self.scheme == "http": conn = httplib.HTTPConnection(self.host) else: conn = httplib.HTTPSConnection(self.host) self.auth.apply_auth(url, 'POST', self.headers, self.parameters) conn.connect() conn.sock.settimeout(self.timeout) conn.request('POST', self.url, self.body, headers=self.headers) logger.debug(u"wait response.") resp = conn.getresponse() logger.debug(u"connect response: %s" % str(resp)) if resp.status != 200: if self.listener.on_error(resp.status) is False: break error_counter += 1 logger.error("response status: error(%s)", (str(resp.status), )) sleep(self.retry_time) else: error_counter = 0 logger.info("response status: ok(%s)", (str(resp.status), )) self._read_loop(resp) except socket.error, err: self.listener.on_socket_error(err) except socket.gaierror, err: self.listener.on_socket_error(err)
def _run(self): # Authenticate url = "%s://%s%s" % (self.scheme, self.host, self.url) # Connect and process the stream error_counter = 0 conn = None exception = None logger.debug(u"run streaming server.") while self.running: # quit if error count greater than retry count if self.retry_count is not None and error_counter > self.retry_count: break try: if self.scheme == "http": conn = httplib.HTTPConnection(self.host) else: conn = httplib.HTTPSConnection(self.host) self.auth.apply_auth(url, 'POST', self.headers, self.parameters) conn.connect() conn.sock.settimeout(self.timeout) conn.request('POST', self.url, self.body, headers=self.headers) logger.debug(u"wait response.") resp = conn.getresponse() logger.debug(u"connect response: %s" % str(resp)) if resp.status != 200: if self.listener.on_error(resp.status) is False: break error_counter += 1 logger.error("response status: error(%s)", (str(resp.status),)) sleep(self.retry_time) else: error_counter = 0 logger.info("response status: ok(%s)", (str(resp.status),)) self._read_loop(resp) except socket.error, err: self.listener.on_socket_error(err) except socket.gaierror, err: self.listener.on_socket_error(err)
logger.info("response status: timeout") if self.listener.on_timeout() == False: break if self.running is False: break conn.close() sleep(self.snooze_time) except Exception, exception: if self.listener.on_fatal_error(exception): continue raise # any other exception is fatal, so kill loop #break logger.error("(%s):%s" % ("_run", "false")) # cleanup self.running = False if conn: conn.close() # need to catch exception if exception: logger.error("(%s):%s" % ("_run", str(exception))) raise ''' @todo: @summary: read HTTPResponse @param resp: HTTPResponse
def on_socket_error(self, err): logger.error(u"[socket error] %s." % err) sleep(5)
def on_error(self, status_code): logger.error("[error] %d" % int(status_code)) return True
def on_status(self, status): try: status.created_at += timedelta(hours=9) self.handler.call(status) except Exception, exception: logger.error(str(exception))