2021-03-01 21:15:17 +01:00
|
|
|
# import
|
2021-03-29 15:27:53 +02:00
|
|
|
import time
|
2021-03-01 21:15:17 +01:00
|
|
|
import requests
|
2021-03-29 15:27:53 +02:00
|
|
|
import logging
|
|
|
|
import uuid
|
2021-03-01 21:15:17 +01:00
|
|
|
|
|
|
|
# external imports
|
|
|
|
import celery
|
|
|
|
import sqlalchemy
|
2021-03-29 15:27:53 +02:00
|
|
|
from chainlib.eth.constant import ZERO_ADDRESS
|
|
|
|
from chainlib.eth.nonce import RPCNonceOracle
|
|
|
|
from chainlib.eth.gas import RPCGasOracle
|
2021-04-24 19:53:45 +02:00
|
|
|
import liveness.linux
|
2021-03-01 21:15:17 +01:00
|
|
|
|
2021-03-07 14:51:59 +01:00
|
|
|
# local imports
|
2021-04-24 19:53:45 +02:00
|
|
|
from cic_eth.error import SeppukuError
|
2021-03-29 15:27:53 +02:00
|
|
|
from cic_eth.db.models.base import SessionBase
|
2021-03-07 14:51:59 +01:00
|
|
|
|
2021-04-24 19:53:45 +02:00
|
|
|
logg = logging.getLogger().getChild(__name__)
|
2021-03-01 21:15:17 +01:00
|
|
|
|
2021-03-29 15:27:53 +02: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-04-24 08:14:24 +02:00
|
|
|
default_token_address = None
|
|
|
|
default_token_symbol = None
|
2021-04-24 19:53:45 +02:00
|
|
|
run_dir = '/run'
|
2021-03-29 15:27:53 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-04-24 19:53:45 +02:00
|
|
|
|
|
|
|
def on_failure(self, exc, task_id, args, kwargs, einfo):
|
|
|
|
if isinstance(exc, SeppukuError):
|
|
|
|
liveness.linux.reset(rundir=self.run_dir)
|
|
|
|
logg.critical(einfo)
|
|
|
|
msg = 'received critical exception {}, calling shutdown'.format(str(exc))
|
|
|
|
s = celery.signature(
|
|
|
|
'cic_eth.admin.ctrl.shutdown',
|
|
|
|
[msg],
|
|
|
|
queue=self.request.delivery_info.get('routing_key'),
|
|
|
|
)
|
|
|
|
s.apply_async()
|
|
|
|
|
2021-03-29 15:27:53 +02: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,
|
2021-03-29 15:27:53 +02:00
|
|
|
sqlalchemy.exc.ResourceClosedError,
|
2021-03-01 21:15:17 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class CriticalWeb3Task(CriticalTask):
|
|
|
|
autoretry_for = (
|
|
|
|
requests.exceptions.ConnectionError,
|
|
|
|
)
|
2021-03-29 15:27:53 +02:00
|
|
|
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,
|
2021-03-29 15:27:53 +02:00
|
|
|
sqlalchemy.exc.ResourceClosedError,
|
2021-03-01 21:15:17 +01:00
|
|
|
)
|
2021-03-29 15:27:53 +02: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,
|
2021-03-29 15:27:53 +02:00
|
|
|
sqlalchemy.exc.ResourceClosedError,
|
2021-03-07 14:51:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
class CriticalWeb3AndSignerTask(CriticalTask):
|
|
|
|
autoretry_for = (
|
|
|
|
requests.exceptions.ConnectionError,
|
|
|
|
)
|
2021-03-29 15:27:53 +02:00
|
|
|
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-23 23:02:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
@celery_app.task()
|
|
|
|
def check_health(self):
|
2021-04-24 19:53:45 +02:00
|
|
|
pass
|