56 lines
2.1 KiB
Python
56 lines
2.1 KiB
Python
"""Исполняется внутри API через stdin; наружу возвращает только безопасные данные."""
|
|
import json
|
|
import sys
|
|
from uuid import UUID
|
|
|
|
from src import broker, jobs
|
|
from src.objects import BUCKET, client
|
|
from src.storage import storage
|
|
|
|
|
|
def run(args):
|
|
action = args['action']
|
|
if action == 'sql':
|
|
with storage.pool.connection() as conn:
|
|
cursor = conn.execute(args['query'], args.get('params', []))
|
|
return cursor.fetchall() if cursor.description else []
|
|
if action == 'publish':
|
|
with broker.channel() as (_, channel):
|
|
channel.confirm_delivery()
|
|
for message in args['messages']:
|
|
broker.publish(channel, message)
|
|
return True
|
|
if action == 'upgrade':
|
|
return jobs.upgrade(UUID(args['id']), args.get('version', 2))
|
|
if action == 'replay':
|
|
return jobs.replay(UUID(args['id']))
|
|
if action == 'cleanup':
|
|
from src.staging import cleanup
|
|
return cleanup(client(), 60)
|
|
if action == 'objects':
|
|
s3 = client()
|
|
result = []
|
|
for prefix in (f"originals/{args['id']}", f"variants/{args['id']}/", f"staging/{args['id']}"):
|
|
for page in s3.get_paginator('list_objects_v2').paginate(Bucket=BUCKET, Prefix=prefix):
|
|
result.extend({'key': row['Key'], 'etag': row['ETag'], 'size': row['Size']}
|
|
for row in page.get('Contents', []))
|
|
return result
|
|
if action == 'remove_staging':
|
|
client().delete_object(Bucket=BUCKET, Key=f"staging/{args['id']}")
|
|
return True
|
|
if action == 'corrupt_staging':
|
|
client().put_object(Bucket=BUCKET, Key=f"staging/{args['id']}", Body=b'broken')
|
|
return True
|
|
if action == 'restore_staging':
|
|
import base64
|
|
client().put_object(Bucket=BUCKET, Key=f"staging/{args['id']}", Body=base64.b64decode(args['data']))
|
|
return True
|
|
raise ValueError('unknown probe action')
|
|
|
|
|
|
storage.pool.open(wait=True)
|
|
try:
|
|
print(json.dumps(run(ARGS), default=str))
|
|
finally:
|
|
storage.pool.close()
|