This commit is contained in:
Andreas Knuth 2025-06-13 15:59:28 -05:00
parent fc6fa76bc0
commit 26adce6ecf
2 changed files with 12 additions and 1 deletions

View File

@ -52,6 +52,8 @@ def mark_email_as_processed(bucket, key):
@app.route('/process/<domain>', methods=['POST']) @app.route('/process/<domain>', methods=['POST'])
def process_email(domain): def process_email(domain):
logger.info(f"[{request_id}] INCOMING POST /process/{domain}: "
f"payload_keys={list(data.keys())}")
auth = request.headers.get('Authorization') auth = request.headers.get('Authorization')
if auth != f'Bearer {API_TOKEN}': if auth != f'Bearer {API_TOKEN}':
return jsonify({'error': 'Unauthorized'}), 401 return jsonify({'error': 'Unauthorized'}), 401
@ -67,6 +69,7 @@ def process_email(domain):
msg = BytesParser(policy=default).parsebytes(email_bytes) msg = BytesParser(policy=default).parsebytes(email_bytes)
from_addr = getaddresses(msg.get_all('from', []))[0][1] if msg.get_all('from') else f'lambda@{domain}' from_addr = getaddresses(msg.get_all('from', []))[0][1] if msg.get_all('from') else f'lambda@{domain}'
logger.debug(f"[{request_id}] Parsed email: from={from_addr}, recipients={recipients}")
recipients = [] recipients = []
for hdr in ('to', 'cc', 'bcc'): for hdr in ('to', 'cc', 'bcc'):
recipients += [addr for _n, addr in getaddresses(msg.get_all(hdr, []))] recipients += [addr for _n, addr in getaddresses(msg.get_all(hdr, []))]
@ -94,7 +97,9 @@ def retry_domain_emails(domain):
head = s3_client.head_object(Bucket=bucket, Key=obj['Key']) head = s3_client.head_object(Bucket=bucket, Key=obj['Key'])
if head.get('Metadata', {}).get('processed') != 'true': if head.get('Metadata', {}).get('processed') != 'true':
unprocessed.append(obj['Key']) unprocessed.append(obj['Key'])
logger.info(f"[{request_id}] RETRY processing for domain={domain}, keys={unprocessed}")
results = {'processed': [], 'failed': []} results = {'processed': [], 'failed': []}
for key in unprocessed: for key in unprocessed:
try: try:

View File

@ -55,6 +55,7 @@ def call_api_with_retry(payload, domain, request_id, max_retries=3):
payload_json = json.dumps(payload).encode('utf-8') payload_json = json.dumps(payload).encode('utf-8')
for attempt in range(max_retries): for attempt in range(max_retries):
try: try:
logger.info(f"[{request_id}] Attempt {attempt+1}: POST payload to {sync_url}")
req = urllib.request.Request(sync_url, data=payload_json, method="POST") req = urllib.request.Request(sync_url, data=payload_json, method="POST")
req.add_header('Authorization', f'Bearer {API_TOKEN}') req.add_header('Authorization', f'Bearer {API_TOKEN}')
req.add_header('Content-Type', 'application/json') req.add_header('Content-Type', 'application/json')
@ -65,6 +66,7 @@ def call_api_with_retry(payload, domain, request_id, max_retries=3):
with urllib.request.urlopen(req, timeout=timeout, context=context) as response: with urllib.request.urlopen(req, timeout=timeout, context=context) as response:
code = response.getcode() code = response.getcode()
if code == 200: if code == 200:
logger.info(f"[{request_id}] API response: {response_code}")
return True return True
except urllib.error.HTTPError as e: except urllib.error.HTTPError as e:
if 400 <= e.code < 500: if 400 <= e.code < 500:
@ -113,6 +115,10 @@ def lambda_handler(event, context):
'original_size': len(body), 'original_size': len(body),
'compressed_size': len(compressed) 'compressed_size': len(compressed)
} }
logger.info(f"[{request_id}] OUTGOING payload to {API_BASE_URL}/process/{domain}: "
f"domain={domain}, key={key}, bucket={bucket}, "
f"orig_size={len(body)}, comp_size={len(compressed)}")
if call_api_with_retry(payload, domain, request_id): if call_api_with_retry(payload, domain, request_id):
# Inline metadata marking statt Löschung # Inline metadata marking statt Löschung