From 2003688c946509f2764ebb94897b34f94d9d1adf Mon Sep 17 00:00:00 2001 From: user Date: Wed, 6 Mar 2024 13:28:36 +0300 Subject: [PATCH] dev@locazia: make indexator --- app/core/background/indexator_service.py | 29 +++++++++++++++++++++++- app/core/background/ton_service.py | 10 -------- app/core/background/uploader_service.py | 2 +- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/app/core/background/indexator_service.py b/app/core/background/indexator_service.py index 705e4ce..f430a19 100644 --- a/app/core/background/indexator_service.py +++ b/app/core/background/indexator_service.py @@ -1,15 +1,42 @@ 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) - await send_status("indexator", f"working (seqno={seqno})") seqno += 1 # if __name__ == '__main__': diff --git a/app/core/background/ton_service.py b/app/core/background/ton_service.py index ba32371..6780c39 100644 --- a/app/core/background/ton_service.py +++ b/app/core/background/ton_service.py @@ -54,21 +54,11 @@ async def main_fn(): make_log("TON", "Withdraw command sent", level="info") await asyncio.sleep(10) - platform_state = await toncenter.get_account(MY_PLATFORM_CONTRACT) - if not platform_state.get('code'): - make_log("TON", "Platform contract is not deployed, shutdown service..", level="info") - await send_status("ton_daemon", "Platform contract is not deployed, not working") - while True: - await asyncio.sleep(100) - while True: try: sw_seqno_value = await get_sw_seqno() make_log("TON", f"Service running ({sw_seqno_value})", level="debug") - - - # with db_session() as session: # for stored_content in session.query(StoredContent).filter(StoredContent.uploaded == False).all(): # pass diff --git a/app/core/background/uploader_service.py b/app/core/background/uploader_service.py index e20b54a..19674a5 100644 --- a/app/core/background/uploader_service.py +++ b/app/core/background/uploader_service.py @@ -18,7 +18,7 @@ async def main_fn(): make_log("Uploader", "Service running", level="debug") await uploader_loop() await asyncio.sleep(5) - await send_status("uploader", f"working (seqno={seqno})") + await send_status("uploader_daemon", f"working (seqno={seqno})") seqno += 1 except BaseException as e: make_log("Uploader", f"Error: {e}", level="error")