uploader-bot/app/core/background/indexator_service.py

50 lines
1.7 KiB
Python

from app.core._utils.send_status import send_status
from app.core._config import MY_PLATFORM_CONTRACT
from app.core._blockchain.ton.toncenter import toncenter
from app.core.models.node_storage import StoredContent
from app.core.storage import db_session
from app.core.logger import make_log
import asyncio
async def indexator_loop(platform_found: bool, seqno: int) -> [bool, int]:
if not platform_found:
platform_state = await toncenter.get_account(MY_PLATFORM_CONTRACT)
if not platform_state.get('code'):
make_log("TON", "Platform contract is not deployed, skipping loop", level="info")
await send_status("indexator", "not working: platform is not deployed")
return
else:
platform_found = True
make_log("Indexator", "Service running", level="debug")
with db_session() as session:
last_known_index = session.query(StoredContent).order_by(StoredContent.onchain_index.desc()).first()
last_known_index = last_known_index.onchain_index if last_known_index >= 0 else 0
make_log("Indexator", f"Last known index: {last_known_index}", level="debug")
await send_status("indexator", f"working (seqno={seqno})")
return platform_found, seqno
async def main_fn():
make_log("Indexator", "Service started", level="info")
platform_found = False
seqno = 0
while True:
try:
platform_found, seqno = await indexator_loop(platform_found, seqno)
except BaseException as e:
make_log("Indexator", f"Error: {e}", level="error")
await asyncio.sleep(5)
seqno += 1
# if __name__ == '__main__':
# loop = asyncio.get_event_loop()
# loop.run_until_complete(main())
# loop.close()