Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
 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
Ejemplo n.º 3
0
def before_request() -> Optional[wrappers.Response]:
    if not request.path.endswith('/'):
        return None  # Let flask do the redirect-routes-not-ending-in-slashes thing before we interfere with routing. Avoids #8277.
    if request.path.startswith('/seasons') and len(request.path) > len(
            '/seasons/') and get_season_id() >= seasons.current_season_num():
        return redirect(re.sub('/seasons/[^/]*', '', request.path))
    if request.path.startswith('/seasons/0'):
        return redirect(request.path.replace('/seasons/0', '/seasons/all'))
    g.p = perf.start()
    return None
Ejemplo n.º 4
0
 def __init__(self, **kwargs: Any) -> None:
     self.launch_time = perf.start()
     super().__init__(
         command_prefix='!',
         help_command=commands.DefaultHelpCommand(dm_help=True),
         case_insensitive=True,
         **kwargs)
     self.voice = None
     self.achievement_cache: Dict[str, Dict[str, str]] = {}
     for task in TASKS:
         asyncio.ensure_future(task(self), loop=self.loop)
Ejemplo n.º 5
0
    def __init__(self, **kwargs: Any) -> None:
        self.launch_time = perf.start()
        commit_id = subprocess.check_output(['git', 'rev-parse',
                                             'HEAD']).decode()
        redis.store('discordbot:commit_id', commit_id)

        super().__init__(
            command_prefix=commands.when_mentioned_or('!'),
            help_command=commands.DefaultHelpCommand(dm_help=True),
            case_insensitive=True,
            **kwargs)
        self.voice = None
        self.achievement_cache: Dict[str, Dict[str, str]] = {}
        for task in TASKS:
            asyncio.ensure_future(task(self), loop=self.loop)
        discordbot.commands.setup(self)
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)
Ejemplo n.º 7
0
    def __init__(self, **kwargs: Any) -> None:
        self.launch_time = perf.start()
        commit_id = subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode()
        redis.store('discordbot:commit_id', commit_id)

        help_command = commands.DefaultHelpCommand(dm_help=None, no_category='Commands')
        intents = discord.Intents.default()
        intents.members = True
        intents.presences = True
        intents.typing = False

        super().__init__(command_prefix=commands.when_mentioned_or('!'), help_command=help_command, case_insensitive=True, intents=intents, **kwargs)
        super().load_extension('jishaku')
        self.voice = None
        self.achievement_cache: Dict[str, Dict[str, str]] = {}
        for task in TASKS:
            asyncio.ensure_future(task(self), loop=self.loop)
        discordbot.commands.setup(self)
 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
Ejemplo n.º 9
0
def before_request() -> None:
    g.p = perf.start()
Ejemplo n.º 10
0
def before_request():
    g.p = perf.start()
Ejemplo n.º 11
0
 def __init__(self) -> None:
     self.launch_time = perf.start()
     super().__init__()
     self.voice = None
     self.achievement_cache: Dict[str, Dict[str, str]] = {}