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

Old md-cloud fixes that should better go to master #97

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 9 additions & 2 deletions mediaire_toolbox/queue/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,15 @@ def __init__(self,
self.stopped = False
self.processing_t_id = None

signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)
try:
signal.signal(signal.SIGINT, self.exit_gracefully)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines can only be executed if you instantiate the daemons in the main execution thread, but that is not the case in md_cloud_compute

signal.signal(signal.SIGTERM, self.exit_gracefully)
except ValueError:
logger.warn('Catching ValueError from signal, are you using '
'daemons outside of the main thread, and if so, '
'do you know what you are doing?')
# (signal only works in main thread)
pass

@abstractmethod
def process_task(self, task):
Expand Down
14 changes: 9 additions & 5 deletions mediaire_toolbox/transaction_db/t_db_retry.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import logging

from tenacity.retry import retry_if_exception_type
from tenacity.retry import retry_if_exception
from tenacity import retry, stop_after_attempt, wait_fixed

from sqlite3 import OperationalError as Sqlite3OperationalError
from sqlalchemy.exc import OperationalError

from mediaire_toolbox.constants import (
RETRY_DATABASE_OP_SECONDS,
Expand All @@ -28,11 +26,17 @@ def before_sleep_log(retry_state):
retry_state.outcome))


def is_operational_error(e):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to fix this in order to properly catch Psql operational errors

return (
'OperationalError' in str(e) or
'OperationalError' in e.__class__.__name__
)


def t_db_retry(f):
return retry(
retry=(
retry_if_exception_type(OperationalError)
| retry_if_exception_type(Sqlite3OperationalError)),
retry_if_exception(is_operational_error)),
stop=stop_after_attempt(RETRY_DATABASE_OP_TIMES),
wait=wait_fixed(RETRY_DATABASE_OP_SECONDS),
before_sleep=before_sleep_log)(f)
13 changes: 7 additions & 6 deletions mediaire_toolbox/transaction_db/transaction_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,12 @@ def create_transaction(self,
@lock
def get_transaction(self, id_: int) -> Transaction:
try:
return self._get_transaction_or_raise_exception(id_)
finally:
# we should always complete the lifetime of the connection,
# otherwise we might run into timeout errors
# (see https://docs.sqlalchemy.org/en/latest/orm/session_transaction.html) # noqa: 501
t = self._get_transaction_or_raise_exception(id_)
self.session.commit()
return t
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here the lifecycle was not handled properly so I got errors (but in sqlite it doesn't matter because there are no connections)

except Exception:
self.session.rollback()
raise

def _get_transaction_or_raise_exception(self, id_: int):
t = self.session.query(Transaction).get(id_)
Expand Down Expand Up @@ -723,8 +723,9 @@ def get_study_metadata(self, study_id: str) -> StudiesMetadata:
try:
return self.session.query(StudiesMetadata)\
.filter_by(study_id=study_id).first()
finally:
self.session.commit()
except Exception:
self.session.rollback()

@t_db_retry
def get_user_sites(self, user_id: int): # TODO -> Query[UserSite]:
Expand Down