41 lines
1.7 KiB
Python
41 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Refresh the bound-scoped essentialConsts KV snapshot for the suite region.
|
|
|
|
Root cause it works around: the user app queries essentialConsts with
|
|
(regionId, boundId), but only admin const-saves refresh bound-scoped KV keys
|
|
— product/tag/slot creation and backend boot refresh just the region-level
|
|
key. Without this, the app keeps reading stale cache versions (old
|
|
products/slots/availability files) and suite-created entities stay invisible.
|
|
|
|
Calls admin.const.updateConstants with an empty change set for bound 1
|
|
(region 1/Mbnr-1): a no-op write that still mirrors fresh versions into KV.
|
|
"""
|
|
import json
|
|
import os
|
|
import sys
|
|
import urllib.request
|
|
|
|
BASE = os.environ.get('BACKEND_URL', 'http://127.0.0.1:8787')
|
|
ADMIN_NAME = os.environ['ADMIN_NAME']
|
|
ADMIN_PASSWORD = os.environ['ADMIN_PASSWORD']
|
|
BOUND_ID = int(os.environ.get('BOUND_ID', '1'))
|
|
|
|
|
|
def post(token, path, data):
|
|
url = f'{BASE}/api/trpc/{path}?batch=1'
|
|
headers = {'content-type': 'application/json'}
|
|
if token:
|
|
headers['Authorization'] = f'Bearer {token}'
|
|
body = json.dumps({0: data}).encode()
|
|
req = urllib.request.Request(url, data=body, headers=headers, method='POST')
|
|
with urllib.request.urlopen(req, timeout=30) as r:
|
|
out = json.loads(r.read().decode())
|
|
if isinstance(out, list) and out and out[0].get('error'):
|
|
raise RuntimeError(f'{path}: {out[0]["error"].get("message")}')
|
|
return (out[0] if isinstance(out, list) else out)['result']['data']
|
|
|
|
|
|
token = post(None, 'admin.staffUser.login',
|
|
{'name': ADMIN_NAME, 'password': ADMIN_PASSWORD})['token']
|
|
post(token, 'admin.const.updateConstants', {'constants': [], 'boundId': BOUND_ID})
|
|
print(f'bound {BOUND_ID} consts snapshot refreshed', file=sys.stderr)
|