-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
110 lines (91 loc) · 3.49 KB
/
lambda_function.py
File metadata and controls
110 lines (91 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import os
import requests
import time
import json
import base64
def lambda_handler(event, context):
print("DEBUG: Lambda invoked with event:", event)
# RapidAPI configuration
RAPIDAPI_ENDPOINT = "https://judge0-ce.p.rapidapi.com"
RAPIDAPI_HOST = "judge0-ce.p.rapidapi.com"
RAPIDAPI_KEY = os.environ.get("RAPIDAPI_KEY")
if not RAPIDAPI_KEY:
raise Exception("Missing RAPIDAPI_KEY in environment variables")
headers = {
"Content-Type": "application/json",
"X-RapidAPI-Host": RAPIDAPI_HOST,
"X-RapidAPI-Key": RAPIDAPI_KEY,
}
# Parse event body
body = event.get("body")
if body and isinstance(body, str):
try:
body = json.loads(body)
except json.JSONDecodeError:
body = {}
else:
body = {}
# Get code submission details from event
source_code = body.get("source_code", "print('Hello, World!')")
language_id = body.get("language_id", 71)
stdin = body.get("stdin", "")
# Check if stdin is empty and handle accordingly
if stdin == "":
stdin = None # Ensure we don't send empty stdin, as some languages fail with empty input.
submission_data = {
"language_id": language_id,
"source_code": source_code,
"stdin": stdin
}
print("DEBUG: Submitting code to RapidAPI:", submission_data)
# Submit code to Judge0 API
submit_response = requests.post(
f"{RAPIDAPI_ENDPOINT}/submissions?base64_encoded=true&wait=false",
headers=headers,
json=submission_data,
timeout=30,
)
print("DEBUG: Submission response:", submit_response.status_code, submit_response.text)
if submit_response.status_code != 201:
return {
"statusCode": submit_response.status_code,
"body": json.dumps({"error": submit_response.text})
}
token = submit_response.json().get("token")
if not token:
print("No token received")
return {
"statusCode": 500,
"body": json.dumps({"error": "No token received from submission"})
}
# Poll for result (up to 30 seconds)
for _ in range(30):
get_response = requests.get(
f"{RAPIDAPI_ENDPOINT}/submissions/{token}?base64_encoded=true",
headers=headers,
timeout=10,
)
result = get_response.json()
status_id = result.get("status", {}).get("id")
print("DEBUG: Current status:", result.get("status"))
if status_id not in [1, 2]: # 1: Pending, 2: Running
print("DEBUG: Execution finished with result:", result)
# Decode the base64-encoded stderr and compile_output
stderr = result.get("stderr", "")
compile_output = result.get("compile_output", "")
if stderr:
decoded_stderr = base64.b64decode(stderr).decode('utf-8')
print("DEBUG: stderr decoded:", decoded_stderr)
if compile_output:
decoded_compile_output = base64.b64decode(compile_output).decode('utf-8')
print("DEBUG: compile_output decoded:", decoded_compile_output)
return {
"statusCode": 200,
"body": json.dumps(result)
}
time.sleep(1)
print("Execution timeout")
return {
"statusCode": 408,
"body": json.dumps({"error": "Execution timeout"})
}