def fetch(url: str, character_encoding: Optional[str] = None, force: bool = False, retry: bool = False) -> str: headers = {} if force: headers['Cache-Control'] = 'no-cache' print('Fetching {url} ({cache})'.format( url=url, cache='no cache' if force else 'cache ok')) try: p = perf.start() response = SESSION.get(url, headers=headers) perf.check(p, 'slow_fetch', (url, headers), 'fetch') if character_encoding is not None: response.encoding = character_encoding if response.status_code in [500, 502, 503]: raise FetchException(f'Server returned a {response.status_code}') p = perf.start() t = response.text took = round(perf.took(p), 2) if took > 1: print( 'Getting text from response was very slow. Setting an explicit character_encoding may help.' ) return t except (urllib.error.HTTPError, requests.exceptions.ConnectionError, TimeoutError) as e: # type: ignore # urllib isn't fully stubbed if retry: return fetch(url, character_encoding, force, retry=False) raise FetchException(e)
def execute_with_reconnect( self, sql: str, args: Optional[List[ValidSqlArgumentDescription]] = None, fetch_rows: Optional[bool] = False ) -> Tuple[int, List[ValidSqlArgumentDescription]]: result = None # Attempt to execute the query and reconnect 3 times, then give up for _ in range(3): try: p = perf.start() n = self.cursor.execute(sql, args) perf.check(p, 'slow_query', (f'```{sql}```', f'```{args}```'), 'mysql') if fetch_rows: rows = self.cursor.fetchall() result = (n, rows) else: result = (n, []) break except OperationalError as e: if 'MySQL server has gone away' in str(e): print('MySQL server has gone away: trying to reconnect') self.connect() else: # raise any other exception raise else: # all attempts failed raise DatabaseException( 'Failed to execute `{sql}` with `{args}`. MySQL has gone away and it was not possible to reconnect in 3 attemps' .format(sql=sql, args=args)) return result
async def on_ready(self) -> None: print('Logged in as {username} ({id})'.format(username=self.user.name, id=self.user.id)) print('Connected to {0}'.format(', '.join( [guild.name for guild in self.guilds]))) print('--------') perf.check(self.launch_time, 'slow_bot_start', '', 'discordbot')
async def on_ready(self) -> None: logging.info('Logged in as {username} ({id})', username=self.user.name, id=self.user.id) logging.info('Connected to {names}', names=', '.join( [guild.name or '' for guild in self.guilds])) logging.info('--------') perf.check(self.launch_time, 'slow_bot_start', '', 'discordbot')
def fetch(url: str, character_encoding: Optional[str] = None, force: bool = False) -> str: headers = {} if force: headers['Cache-Control'] = 'no-cache' print('Fetching {url} ({cache})'.format( url=url, cache='no cache' if force else 'cache ok')) try: p = perf.start() response = SESSION.get(url, headers=headers) perf.check(p, 'slow_fetch', (url, headers), 'fetch') if character_encoding is not None: response.encoding = character_encoding return response.text except (urllib.error.HTTPError, requests.exceptions.ConnectionError ) as e: # type: ignore # urllib isn't fully stubbed raise FetchException(e)
def execute_with_reconnect(self, sql, args): result = None # Attempt to excute the query and reconnect 3 times, then give up for _ in range(3): try: p = perf.start() self.cursor.execute(sql, args) perf.check(p, 'slow_query', (sql, args), 'mysql') result = self.cursor.fetchall() break except OperationalError as e: if 'MySQL server has gone away' in str(e): print("MySQL server has gone away: trying to reconnect") self.connect() else: # raise any other exception raise e else: # all attempts failed raise DatabaseException( 'Failed to execute `{sql}` with `{args}`. MySQL has gone away and it was not possible to reconnect in 3 attemps' .format(sql=sql, args=args)) return result
def teardown_request(response: Response) -> Response: if g.get('p') is not None: perf.check(g.p, 'slow_page', request.path, 'decksite') db().close() return response
def teardown_request(response): perf.check(g.p, 'slow_page', request.path, 'decksite') return response
def teardown_request(_: Optional[Exception]) -> None: if g.get('p') is not None: perf.check(g.p, 'slow_page', request.path, 'decksite') db().close()
async def on_ready(self) -> None: logging.info('Logged in as %s (%d)', self.user.name, self.user.id) names = ', '.join([guild.name or '' for guild in self.guilds]) logging.info('Connected to %s', names) logging.info('--------') perf.check(self.launch_time, 'slow_bot_start', '', 'discordbot')