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

102 lines
2.4 KiB
Python
Raw Permalink Normal View History

2021-03-01 21:15:17 +01:00
# import
import time
2021-03-01 21:15:17 +01:00
import requests
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
from cic_eth.error import (
SignerError,
EthError,
)
from cic_eth.db.models.base import SessionBase
2021-03-07 14:51:59 +01:00
logg = logging.getLogger(__name__)
2021-03-01 21:15:17 +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
def create_session(self):
return BaseTask.session_func()
def log_banner(self):
logg.debug('task {} root uuid {}'.format(self.__class__.__name__, self.request.root_id))
return
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
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,
EthError,
2021-03-01 21:15:17 +01:00
)
safe_gas_threshold_amount = 2000000000 * 60000 * 3
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,
)
safe_gas_threshold_amount = 2000000000 * 60000 * 3
safe_gas_refill_amount = safe_gas_threshold_amount * 5
@celery_app.task(bind=True, base=BaseTask)
def hello(self):
time.sleep(0.1)
return id(SessionBase.create_session)
2021-04-21 14:43:30 +02:00
@celery_app.task()
2021-04-21 15:35:20 +02:00
def check_health(self):
2021-04-21 14:43:30 +02:00
celery.app.control.shutdown()