From 66da3541e70c4303dc38bdcc9972896e59adde21 Mon Sep 17 00:00:00 2001 From: user Date: Fri, 14 Mar 2025 10:54:57 +0300 Subject: [PATCH] try fix licenses --- app/core/background/license_service.py | 12 +++++++----- app/core/background/ton_service.py | 6 +++--- app/core/models/user/__init__.py | 6 ++++-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/app/core/background/license_service.py b/app/core/background/license_service.py index 691d345..f999a47 100644 --- a/app/core/background/license_service.py +++ b/app/core/background/license_service.py @@ -3,7 +3,7 @@ from base64 import b64decode from datetime import datetime, timedelta from base58 import b58encode -from sqlalchemy import and_ +from sqlalchemy import and_, or_ from tonsdk.boc import Cell from tonsdk.utils import Address @@ -74,12 +74,14 @@ async def license_index_loop(memory, platform_found: bool, seqno: int) -> [bool, # Проверка кошельков пользователей на появление новых NFT, добавление их в базу как неопознанные for user in session.query(User).filter( - User.last_use > datetime.now() - timedelta(minutes=10) - ).all(): - if not user.wallet_address(session): + User.last_use > datetime.now() - timedelta(hours=4) + ).order_by(User.updated.asc()).all(): + user_wallet_address = user.wallet_address(session) + if not user_wallet_address: make_log("LicenseIndex", f"User {user.id} has no wallet address", level="info") continue + make_log("LicenseIndex", f"User {user.id} has wallet address {user_wallet_address}", level="info") last_updated_licenses = user.meta.get('last_updated_licenses') must_skip = last_updated_licenses and (datetime.now() - datetime.fromisoformat(last_updated_licenses)) < timedelta(minutes=1) make_log("LicenseIndex", f"User: {user.id}, last_updated_licenses: {last_updated_licenses}, must_skip: {must_skip}", level="info") @@ -99,7 +101,7 @@ async def license_index_loop(memory, platform_found: bool, seqno: int) -> [bool, UserContent.type.startswith('nft/'), UserContent.updated < (datetime.now() - timedelta(minutes=60)), ) - ).first() + ).order_by(UserContent.updated.asc()).first() if process_content: make_log("LicenseIndex", f"Syncing content with blockchain: {process_content.id}", level="info") try: diff --git a/app/core/background/ton_service.py b/app/core/background/ton_service.py index 5746c82..e9dba98 100644 --- a/app/core/background/ton_service.py +++ b/app/core/background/ton_service.py @@ -157,7 +157,7 @@ async def main_fn(memory): in_msg_blockchain_task.status = 'done' in_msg_blockchain_task.transaction_hash = transaction_hash in_msg_blockchain_task.transaction_lt = transaction_lt - await session.commit() + session.commit() for blockchain_message in [transaction['in_msg']]: try: @@ -210,7 +210,7 @@ async def main_fn(memory): # or sum([int("terminating vm with exit code 36" in e) for e in errors_list]) > 0: make_log("TON_Daemon", f"Task {blockchain_task.id} done", level="DEBUG") blockchain_task.status = 'done' - await session.commit() + session.commit() continue await asyncio.sleep(0.5) @@ -266,7 +266,7 @@ async def main_fn(memory): 'sign_created': sign_created, 'signed_message': query_boc.hex(), } - await session.commit() + session.commit() make_log("TON", f"Created signed message for task {blockchain_task.id}" + '\n' + traceback.format_exc(), level="info") except BaseException as e: make_log("TON", f"Error processing task {blockchain_task.id}: {e}" + '\n' + traceback.format_exc(), level="error") diff --git a/app/core/models/user/__init__.py b/app/core/models/user/__init__.py index 89d3493..35a2d67 100644 --- a/app/core/models/user/__init__.py +++ b/app/core/models/user/__init__.py @@ -1,3 +1,4 @@ +from datetime import datetime from sqlalchemy import Column, Integer, String, BigInteger, DateTime, JSON from sqlalchemy.orm import relationship @@ -19,8 +20,9 @@ class User(AlchemyBase, DisplayMixin, TranslationCore, AuthenticationMixin_V1, W lang_code = Column(String(8), nullable=False, default="en") meta = Column(JSON, nullable=False, default={}) - last_use = Column(DateTime, nullable=False, default=0) - created = Column(DateTime, nullable=False, default=0) + last_use = Column(DateTime, nullable=False, default=datetime.utcnow) + updated = Column(DateTime, nullable=False, default=datetime.utcnow) + created = Column(DateTime, nullable=False, default=datetime.utcnow) balances = relationship('UserBalance', back_populates='user') internal_transactions = relationship('InternalTransaction', back_populates='user')