-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SQL cleanup thread or per requests (#211)
Also use retry decorator to storage exceptions
- Loading branch information
Showing
15 changed files
with
246 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
name: Ruff | ||
on: [push, pull_request] | ||
jobs: | ||
ruff: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: chartboost/ruff-action@v1 | ||
ruff: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: chartboost/ruff-action@v1 | ||
with: | ||
version: 0.1.11 | ||
args: --select B |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
name: Run unittests | ||
on: [push, pull_request] | ||
jobs: | ||
unittests: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: supercharge/[email protected] | ||
- uses: niden/actions-memcached@v7 | ||
- uses: actions/setup-python@v4 | ||
with: | ||
python-version: 'pypy3.9' | ||
- name: Install testing requirements | ||
run: pip3 install -r requirements/pytest.txt | ||
- name: Run tests | ||
run: pytest tests | ||
unittests: | ||
runs-on: ubuntu-latest | ||
services: | ||
mongodb: | ||
image: mongo | ||
ports: | ||
- 27017:27017 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: supercharge/[email protected] | ||
- uses: niden/actions-memcached@v7 | ||
- uses: actions/setup-python@v4 | ||
with: | ||
python-version: "pypy3.9" | ||
- name: Install testing requirements | ||
run: pip3 install -r requirements/pytest.txt | ||
- name: Run tests | ||
run: pytest tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
""" | ||
MIT License | ||
Copyright (c) 2023 giuppep | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
""" | ||
import time | ||
from functools import wraps | ||
from typing import Any, Callable | ||
|
||
from flask import current_app | ||
|
||
|
||
def retry_query( | ||
*, max_attempts: int = 3, delay: float = 0.3, backoff: int = 2 | ||
) -> Callable[..., Any]: | ||
"""Decorator to retry a query when an OperationalError is raised. | ||
Args: | ||
max_attempts: Maximum number of attempts. Defaults to 3. | ||
delay: Delay between attempts in seconds. Defaults to 0.3. | ||
backoff: Backoff factor. Defaults to 2. | ||
""" | ||
|
||
def decorator(func: Callable[..., Any]) -> Callable[..., Any]: | ||
@wraps(func) | ||
def wrapper(*args: Any, **kwargs: Any) -> Any: | ||
for attempt in range(max_attempts): | ||
try: | ||
return func(*args, **kwargs) | ||
# TODO: use proper exception type | ||
except Exception as e: | ||
if attempt == max_attempts - 1: | ||
raise e | ||
|
||
sleep_time = delay * backoff**attempt | ||
current_app.logger.exception( | ||
f"Exception when querying database ({e})." | ||
f"Retrying ({attempt + 1}/{max_attempts}) in {sleep_time:.2f}s." | ||
) | ||
time.sleep(sleep_time) | ||
|
||
return wrapper | ||
|
||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.