Skip to content
This repository has been archived by the owner on Feb 5, 2022. It is now read-only.

[PATCH] DEV-2614: @t_db_retry: don't retry when attempting write operations on read-only t.db #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions mediaire_toolbox/transaction_db/t_db_retry.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

from tenacity.retry import retry_if_exception_type
from tenacity.retry import (retry_if_exception_type,
retry_if_not_exception_message)
from tenacity import retry, stop_after_attempt, wait_fixed

from sqlite3 import OperationalError as Sqlite3OperationalError
Expand All @@ -18,21 +19,28 @@
Database retry logic for the Transactions DB.
Retry up to a maximum amount of times, with a fixed wait period inbetween.
Retry only for certain exceptions that we know are problematic.
Don't retry when attempting write operations on read-only databases.
"""


def before_sleep_log(retry_state):
default_logger.warn(
default_logger.warning(
'Retrying {}: attempt {} ended with: {}'
.format(retry_state.fn, retry_state.attempt_number,
retry_state.outcome))


def t_db_retry(f):
"""Retries only when specific sqlite3 Exceptions are raised and when
they don't correspond failed write operations on read-only databases
(message will be something like 'attempt to write a readonly database')
"""
return retry(
retry=(
retry_if_exception_type(OperationalError)
| retry_if_exception_type(Sqlite3OperationalError)),
(retry_if_exception_type(OperationalError)
| retry_if_exception_type(Sqlite3OperationalError))
& retry_if_not_exception_message(match='.*read.?only.*')
),
stop=stop_after_attempt(RETRY_DATABASE_OP_TIMES),
wait=wait_fixed(RETRY_DATABASE_OP_SECONDS),
before_sleep=before_sleep_log)(f)