Skip to content

Commit

Permalink
Don't use database in app.ready
Browse files Browse the repository at this point in the history
  • Loading branch information
Uxio0 committed Mar 20, 2024
1 parent 19447f0 commit 52dae13
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
27 changes: 17 additions & 10 deletions safe_transaction_service/contracts/tx_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import operator
from functools import cache, cached_property
from logging import getLogger
from threading import Lock
from typing import (
Any,
Dict,
Expand Down Expand Up @@ -118,6 +119,9 @@ class MultisendDecoded(TypedDict):
data_decoded: Optional[DataDecoded]


mutex = Lock()


@cache
def get_db_tx_decoder() -> "DbTxDecoder":
"""
Expand All @@ -127,16 +131,19 @@ def get_db_tx_decoder() -> "DbTxDecoder":
the ``DbTxDecoder`` multiple times, and depending on the number of Contracts in the database it could
take a lot.
"""

def _get_db_tx_decoder() -> "DbTxDecoder":
return DbTxDecoder()

if running_on_gevent():
# It's a very intensive CPU task, so to prevent blocking
# http://www.gevent.org/api/gevent.threadpool.html
pool = gevent.get_hub().threadpool
return pool.spawn(_get_db_tx_decoder).get()
return _get_db_tx_decoder()
with mutex:
if is_db_tx_decoder_loaded():
return get_db_tx_decoder()

def _get_db_tx_decoder() -> "DbTxDecoder":
return DbTxDecoder()

if running_on_gevent():
# It's a very intensive CPU task, so to prevent blocking
# http://www.gevent.org/api/gevent.threadpool.html
pool = gevent.get_hub().threadpool
return pool.spawn(_get_db_tx_decoder).get()
return _get_db_tx_decoder()


def is_db_tx_decoder_loaded() -> bool:
Expand Down
13 changes: 0 additions & 13 deletions safe_transaction_service/history/apps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import sys

from django.apps import AppConfig


Expand All @@ -9,14 +7,3 @@ class HistoryConfig(AppConfig):

def ready(self):
from . import signals # noqa

for argument in sys.argv:
if "gunicorn" in argument: # pragma: no cover
# Just run this on production
# TODO Find a better way
from safe_transaction_service.contracts.tx_decoder import (
get_db_tx_decoder,
)

get_db_tx_decoder() # Build tx decoder cache
break

0 comments on commit 52dae13

Please sign in to comment.