1313import base64
1414import json
1515import os
16+ import sys
17+ import traceback
1618import zlib
1719from dataclasses import asdict
1820from datetime import datetime
@@ -28,28 +30,52 @@ def serialize(obj: object):
2830 raise TypeError (f"Type { type (obj )} not serializable" )
2931
3032
33+ def write_error_result (error_message : str ):
34+ """Write an error result to result.json when execution fails."""
35+ error_result = {
36+ "success" : False ,
37+ "error" : error_message ,
38+ "runs" : {},
39+ "system" : {},
40+ }
41+ Path ("result.json" ).write_text (json .dumps (error_result , default = serialize ))
42+
43+
3144def main ():
32- # Get payload from environment variable
33- payload_b64 = os .environ .get ("SUBMISSION_PAYLOAD" )
34- if not payload_b64 :
35- raise RuntimeError ("SUBMISSION_PAYLOAD environment variable not set" )
36-
37- # Decompress and parse config
38- payload = zlib .decompress (base64 .b64decode (payload_b64 )).decode ("utf-8" )
39- config = json .loads (payload )
40-
41- # Run the submission
42- result = run_config (config )
43-
44- # Write result to file for artifact upload
45- result_dict = asdict (result )
46- Path ("result.json" ).write_text (json .dumps (result_dict , default = serialize ))
47-
48- # Create profile_data directory if profiling was enabled
49- # (profile artifacts will be written there by run_config)
50- profile_dir = Path ("profile_data" )
51- if profile_dir .exists ():
52- print (f"Profile data available in { profile_dir } " )
45+ try :
46+ # Get payload from environment variable
47+ payload_b64 = os .environ .get ("SUBMISSION_PAYLOAD" )
48+ if not payload_b64 :
49+ write_error_result ("SUBMISSION_PAYLOAD environment variable not set" )
50+ sys .exit (1 )
51+
52+ # Decompress and parse config
53+ try :
54+ payload = zlib .decompress (base64 .b64decode (payload_b64 )).decode ("utf-8" )
55+ config = json .loads (payload )
56+ except Exception as e :
57+ write_error_result (f"Failed to decompress/parse payload: { e } " )
58+ sys .exit (1 )
59+
60+ # Run the submission
61+ result = run_config (config )
62+
63+ # Write result to file for artifact upload
64+ result_dict = asdict (result )
65+ Path ("result.json" ).write_text (json .dumps (result_dict , default = serialize ))
66+
67+ # Create profile_data directory if profiling was enabled
68+ # (profile artifacts will be written there by run_config)
69+ profile_dir = Path ("profile_data" )
70+ if profile_dir .exists ():
71+ print (f"Profile data available in { profile_dir } " )
72+
73+ except Exception as e :
74+ # Catch any unexpected errors and write them to result.json
75+ error_msg = f"Runner error: { e } \n { traceback .format_exc ()} "
76+ print (error_msg , file = sys .stderr )
77+ write_error_result (error_msg )
78+ sys .exit (1 )
5379
5480
5581if __name__ == "__main__" :
0 commit comments