Fix high load (#84)

* chore: testing

* fix: resubscribe when merchant added/removed

* chore: code clean-up

* fix: temp subscribe when new merchant is added

* fix: new customer profile
This commit is contained in:
Vlad Stan 2023-09-20 09:17:01 +03:00 committed by GitHub
parent c0226fe85b
commit c073bf55c9
5 changed files with 202 additions and 148 deletions

View file

@ -40,7 +40,6 @@ from .crud import (
get_last_direct_messages_time,
get_merchant_by_pubkey,
get_merchant_for_user,
get_merchants_ids_with_pubkeys,
get_order,
get_order_by_event_id,
get_orders,
@ -86,7 +85,9 @@ from .services import (
reply_to_structured_dm,
build_order_with_payment,
create_or_update_order_from_dm,
resubscribe_to_all_merchants,
sign_and_send_to_nostr,
subscribe_to_all_merchants,
update_merchant_to_nostr,
)
@ -119,7 +120,9 @@ async def api_create_merchant(
),
)
await nostr_client.subscribe_merchant(data.public_key, 0)
await resubscribe_to_all_merchants()
await nostr_client.merchant_temp_subscription(data.public_key)
return merchant
except AssertionError as ex:
@ -170,8 +173,7 @@ async def api_delete_merchant(
assert merchant, "Merchant cannot be found"
assert merchant.id == merchant_id, "Wrong merchant ID"
# first unsubscribe so new events are not created during the clean-up
await nostr_client.unsubscribe_merchant(merchant.public_key)
await nostr_client.unsubscribe_merchants()
await delete_merchant_orders(merchant.id)
await delete_merchant_products(merchant.id)
@ -180,6 +182,7 @@ async def api_delete_merchant(
await delete_merchant_zones(merchant.id)
await delete_merchant(merchant.id)
except AssertionError as ex:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
@ -191,7 +194,8 @@ async def api_delete_merchant(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
detail="Cannot get merchant",
)
finally:
await subscribe_to_all_merchants()
@nostrmarket_ext.put("/api/v1/merchant/{merchant_id}/nostr")
async def api_republish_merchant(
@ -1057,7 +1061,8 @@ async def api_create_customer(
customer = await create_customer(
merchant.id, Customer(merchant_id=merchant.id, public_key=pubkey)
)
await nostr_client.subscribe_to_user_profile(pubkey, 0)
await nostr_client.user_profile_temp_subscribe(pubkey)
return customer
except (ValueError, AssertionError) as ex:
@ -1084,9 +1089,7 @@ async def api_list_currencies_available():
@nostrmarket_ext.put("/api/v1/restart")
async def restart_nostr_client(wallet: WalletTypeInfo = Depends(require_admin_key)):
try:
ids = await get_merchants_ids_with_pubkeys()
merchant_public_keys = [id[0] for id in ids]
await nostr_client.restart(merchant_public_keys)
await nostr_client.restart()
except Exception as ex:
logger.warning(ex)
@ -1100,9 +1103,7 @@ async def api_stop(wallet: WalletTypeInfo = Depends(check_admin)):
logger.warning(ex)
try:
ids = await get_merchants_ids_with_pubkeys()
merchant_public_keys = [id[0] for id in ids]
await nostr_client.stop(merchant_public_keys)
await nostr_client.stop()
except Exception as ex:
logger.warning(ex)