optimize license service
This commit is contained in:
parent
62c93b7730
commit
2609495b91
|
|
@ -35,20 +35,22 @@ async def license_index_loop(memory, platform_found: bool, seqno: int) -> [bool,
|
||||||
continue
|
continue
|
||||||
|
|
||||||
last_updated_licenses = user.meta.get('last_updated_licenses')
|
last_updated_licenses = user.meta.get('last_updated_licenses')
|
||||||
make_log("LicenseIndex", f"User: {user.id}, last_updated_licenses: {last_updated_licenses}", level="info")
|
must_skip = last_updated_licenses and (datetime.now() - datetime.fromisoformat(last_updated_licenses)) < timedelta(minutes=1)
|
||||||
if 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")
|
||||||
|
if must_skip:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await user.scan_owned_user_content(session)
|
await user.scan_owned_user_content(session)
|
||||||
user.meta['last_updated_licenses'] = datetime.now().isoformat()
|
user.meta = {**user.meta, 'last_updated_licenses': datetime.now().isoformat()}
|
||||||
|
session.commit()
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
make_log("LicenseIndex", f"Error: {e}" + '\n' + traceback.format_exc(), level="error")
|
make_log("LicenseIndex", f"Error: {e}" + '\n' + traceback.format_exc(), level="error")
|
||||||
|
|
||||||
process_content = session.query(UserContent).filter(
|
process_content = session.query(UserContent).filter(
|
||||||
and_(
|
and_(
|
||||||
UserContent.type.startswith('nft/'),
|
UserContent.type.startswith('nft/'),
|
||||||
UserContent.updated < (datetime.now() - timedelta(minutes=5)),
|
UserContent.updated < (datetime.now() - timedelta(minutes=60)),
|
||||||
)
|
)
|
||||||
).first()
|
).first()
|
||||||
if process_content:
|
if process_content:
|
||||||
|
|
@ -61,40 +63,6 @@ async def license_index_loop(memory, platform_found: bool, seqno: int) -> [bool,
|
||||||
process_content.updated = datetime.now()
|
process_content.updated = datetime.now()
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
for action in session.query(UserAction).filter(
|
|
||||||
and_(
|
|
||||||
UserAction.type == 'purchase',
|
|
||||||
# UserAction.updated < (datetime.now() - timedelta(minutes=5)),
|
|
||||||
UserAction.status == 'requested',
|
|
||||||
)
|
|
||||||
):
|
|
||||||
try:
|
|
||||||
user = session.query(User).filter_by(id=action.user_id).first()
|
|
||||||
chat_wrap = Wrapped_CBotChat(CLIENT_TELEGRAM_API_KEY, chat_id=user.telegram_id, db_session=session, user=user)
|
|
||||||
content = session.query(StoredContent).filter_by(id=action.content_id).first()
|
|
||||||
|
|
||||||
edited_ = False
|
|
||||||
if (datetime.now() - action.created) > timedelta(minutes=5):
|
|
||||||
if action.telegram_message_id:
|
|
||||||
await chat_wrap.delete_message(action.telegram_message_id)
|
|
||||||
|
|
||||||
make_log("LicenseIndex", f"Action timeout: {action.id}", level="info")
|
|
||||||
action.status = 'canceled'
|
|
||||||
edited_ = True
|
|
||||||
else:
|
|
||||||
user_wallet_address = user.wallet_address(session)
|
|
||||||
user_content = session.query(UserContent).filter_by(content_id=action.content_id, status='active', owner_address=user_wallet_address).first()
|
|
||||||
if user_content:
|
|
||||||
make_log("LicenseIndex", f"User already has content: {user_content.content_id}", level="info")
|
|
||||||
action.status = 'success'
|
|
||||||
edited_ = True
|
|
||||||
|
|
||||||
if edited_:
|
|
||||||
session.commit()
|
|
||||||
await chat_wrap.send_content(session, content)
|
|
||||||
except BaseException as e:
|
|
||||||
make_log("LicenseIndex", f"Error: {e}" + '\n' + traceback.format_exc(), level="error")
|
|
||||||
|
|
||||||
return platform_found, seqno
|
return platform_found, seqno
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -110,7 +78,7 @@ async def main_fn(memory, ):
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
make_log("LicenseIndex", f"Error: {e}" + '\n' + traceback.format_exc(), level="error")
|
make_log("LicenseIndex", f"Error: {e}" + '\n' + traceback.format_exc(), level="error")
|
||||||
|
|
||||||
await asyncio.sleep(5)
|
await asyncio.sleep(1)
|
||||||
seqno += 1
|
seqno += 1
|
||||||
|
|
||||||
# if __name__ == '__main__':
|
# if __name__ == '__main__':
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue