From 6e4893f59dace3c84b727eefa6fb21e2a5a12d33 Mon Sep 17 00:00:00 2001 From: Doctor Delpy Date: Wed, 24 Dec 2025 14:22:26 +0300 Subject: [PATCH] try indexer fix --- app/core/background/indexer_service.py | 107 ++++++++++++++++--------- 1 file changed, 69 insertions(+), 38 deletions(-) diff --git a/app/core/background/indexer_service.py b/app/core/background/indexer_service.py index bca075c..d9370b7 100644 --- a/app/core/background/indexer_service.py +++ b/app/core/background/indexer_service.py @@ -61,48 +61,79 @@ async def indexer_loop(memory, platform_found: bool, seqno: int) -> [bool, int]: ) ))).scalars().all() for new_license in new_licenses: - licensed_content = (await session.execute(select(StoredContent).where( - StoredContent.id == new_license.content_id - ))).scalars().first() - if not licensed_content: - make_log("Indexer", f"Licensed content not found: {new_license.content_id}", level="error") + try: + licensed_content = (await session.execute(select(StoredContent).where( + StoredContent.id == new_license.content_id + ))).scalars().first() + if not licensed_content: + make_log("Indexer", f"Licensed content not found: {new_license.content_id}", level="error") + new_license.meta = {**(new_license.meta or {}), 'notification_sent': True, 'notification_error': 'content_not_found'} + await session.commit() + continue - content_metadata = await licensed_content.metadata_json_async(session) - assert content_metadata, "No content metadata found" - - if not (licensed_content.owner_address == new_license.owner_address): try: - user = await session.get(User, new_license.user_id) - if user.telegram_id and licensed_content: - await (Wrapped_CBotChat(memory._client_telegram_bot, chat_id=user.telegram_id, user=user, db_session=session)).send_content( - session, licensed_content - ) - - wallet_owner_connection = (await session.execute( - select(WalletConnection).where( - WalletConnection.wallet_address == licensed_content.owner_address, - WalletConnection.invalidated == False - ).order_by(desc(WalletConnection.id)) - )).scalars().first() - wallet_owner_user = await session.get(User, wallet_owner_connection.user_id) if wallet_owner_connection else None - if wallet_owner_user.telegram_id: - wallet_owner_bot = Wrapped_CBotChat(memory._telegram_bot, chat_id=wallet_owner_user.telegram_id, user=wallet_owner_user, db_session=session) - meta_title = content_metadata.get('title') or content_metadata.get('name') or 'Unknown' - meta_artist = content_metadata.get('artist') - formatted_title = f"{meta_artist} – {meta_title}" if meta_artist else meta_title - await wallet_owner_bot.send_message( - user.translated('p_licenseWasBought').format( - username=user.front_format(), - nft_address=f'"https://tonviewer.com/{new_license.onchain_address}"', - content_title=formatted_title, - ), - message_type='notification', - ) + content_metadata = await licensed_content.metadata_json_async(session) except BaseException as e: - make_log("IndexerSendNewLicense", f"Error: {e}" + '\n' + traceback.format_exc(), level="error") + make_log("Indexer", f"Metadata fetch failed for content_id={licensed_content.id}: {e}", level="warning") + content_metadata = None - new_license.meta = {**new_license.meta, 'notification_sent': True} - await session.commit() + # Metadata is best-effort here: it should never block indexer loop progress. + if not content_metadata: + content_metadata = { + 'name': licensed_content.meta.get('title') or licensed_content.filename or 'Unknown', + 'artist': licensed_content.meta.get('artist'), + 'title': licensed_content.meta.get('title'), + } + + if not (licensed_content.owner_address == new_license.owner_address): + try: + user = await session.get(User, new_license.user_id) + if user and user.telegram_id: + await ( + Wrapped_CBotChat( + memory._client_telegram_bot, + chat_id=user.telegram_id, + user=user, + db_session=session, + ) + ).send_content(session, licensed_content) + + wallet_owner_connection = (await session.execute( + select(WalletConnection).where( + WalletConnection.wallet_address == licensed_content.owner_address, + WalletConnection.invalidated == False + ).order_by(desc(WalletConnection.id)) + )).scalars().first() + wallet_owner_user = await session.get(User, wallet_owner_connection.user_id) if wallet_owner_connection else None + if wallet_owner_user and wallet_owner_user.telegram_id: + wallet_owner_bot = Wrapped_CBotChat( + memory._telegram_bot, + chat_id=wallet_owner_user.telegram_id, + user=wallet_owner_user, + db_session=session, + ) + meta_title = content_metadata.get('title') or content_metadata.get('name') or 'Unknown' + meta_artist = content_metadata.get('artist') + formatted_title = f"{meta_artist} – {meta_title}" if meta_artist else meta_title + await wallet_owner_bot.send_message( + user.translated('p_licenseWasBought').format( + username=user.front_format(), + nft_address=f'"https://tonviewer.com/{new_license.onchain_address}"', + content_title=formatted_title, + ), + message_type='notification', + ) + except BaseException as e: + make_log("IndexerSendNewLicense", f"Error: {e}" + '\n' + traceback.format_exc(), level="error") + + # Preserve current behavior: do not retry notifications indefinitely. + new_license.meta = {**(new_license.meta or {}), 'notification_sent': True} + await session.commit() + except BaseException as e: + # Never allow a single broken license/metadata record to block the whole indexer loop. + make_log("Indexer", f"Error processing new license {getattr(new_license, 'id', None)}: {e}" + '\n' + traceback.format_exc(), level="error") + new_license.meta = {**(new_license.meta or {}), 'notification_sent': True, 'notification_error': str(e)[:256]} + await session.commit() content_without_cid = (await session.execute(select(StoredContent).where(StoredContent.content_id == None))).scalars().all() for target_content in content_without_cid: