cic-internal-integration/apps/cic-eth/cic_eth/task.py

97 lines
2.3 KiB
Python
Raw Normal View History

2021-03-01 21:15:17 +01:00
# import
2021-03-11 11:40:02 +01:00
import time
2021-03-01 21:15:17 +01:00
import requests
2021-03-11 11:40:02 +01:00
import logging
import uuid
2021-03-01 21:15:17 +01:00
# external imports
import celery
import sqlalchemy
from chainlib.eth.constant import ZERO_ADDRESS
from chainlib.eth.nonce import RPCNonceOracle
from chainlib.eth.gas import RPCGasOracle
2021-03-01 21:15:17 +01:00
2021-03-07 14:51:59 +01:00
# local imports
2021-03-07 19:01:16 +01:00
from cic_eth.error import (
SignerError,
EthError,
)
2021-03-11 11:40:02 +01:00
from cic_eth.db.models.base import SessionBase
2021-03-07 14:51:59 +01:00
2021-03-11 11:40:02 +01:00
logg = logging.getLogger(__name__)
2021-03-01 21:15:17 +01:00
2021-03-11 11:40:02 +01:00
celery_app = celery.current_app
class BaseTask(celery.Task):
session_func = SessionBase.create_session
call_address = ZERO_ADDRESS
create_nonce_oracle = RPCNonceOracle
create_gas_oracle = RPCGasOracle
2021-03-11 11:40:02 +01:00
def create_session(self):
return BaseTask.session_func()
2021-03-28 18:32:37 +02:00
def log_banner(self):
logg.debug('task {} root uuid {}'.format(self.__class__.__name__, self.request.root_id))
return
2021-03-11 11:40:02 +01:00
class CriticalTask(BaseTask):
2021-03-01 21:15:17 +01:00
retry_jitter = True
retry_backoff = True
retry_backoff_max = 8
class CriticalSQLAlchemyTask(CriticalTask):
autoretry_for = (
sqlalchemy.exc.DatabaseError,
sqlalchemy.exc.TimeoutError,
sqlalchemy.exc.ResourceClosedError,
2021-03-01 21:15:17 +01:00
)
class CriticalWeb3Task(CriticalTask):
autoretry_for = (
requests.exceptions.ConnectionError,
)
safe_gas_threshold_amount = 2000000000 * 60000 * 3
2021-03-18 19:36:52 +01:00
safe_gas_refill_amount = safe_gas_threshold_amount * 5
2021-03-01 21:15:17 +01:00
class CriticalSQLAlchemyAndWeb3Task(CriticalTask):
autoretry_for = (
sqlalchemy.exc.DatabaseError,
sqlalchemy.exc.TimeoutError,
requests.exceptions.ConnectionError,
sqlalchemy.exc.ResourceClosedError,
2021-03-07 19:01:16 +01:00
EthError,
2021-03-01 21:15:17 +01:00
)
safe_gas_threshold_amount = 2000000000 * 60000 * 3
2021-03-18 19:36:52 +01:00
safe_gas_refill_amount = safe_gas_threshold_amount * 5
2021-03-07 14:51:59 +01:00
class CriticalSQLAlchemyAndSignerTask(CriticalTask):
autoretry_for = (
sqlalchemy.exc.DatabaseError,
sqlalchemy.exc.TimeoutError,
sqlalchemy.exc.ResourceClosedError,
2021-03-07 14:51:59 +01:00
SignerError,
)
class CriticalWeb3AndSignerTask(CriticalTask):
autoretry_for = (
requests.exceptions.ConnectionError,
SignerError,
)
2021-03-18 19:36:52 +01:00
safe_gas_threshold_amount = 2000000000 * 60000 * 3
safe_gas_refill_amount = safe_gas_threshold_amount * 5
2021-03-11 11:40:02 +01:00
@celery_app.task(bind=True, base=BaseTask)
def hello(self):
time.sleep(0.1)
return id(SessionBase.create_session)