@@ -141,6 +141,7 @@ def send_to_scheduler(
141141 - success: whether script running successfully
142142 - return_code: return-code of subprocess
143143 - output: output of subprocess or error message
144+ - started_at: start timestamp
144145 - spend_time: time subprocess took
145146 - request_data: data from request
146147 """
@@ -156,7 +157,7 @@ def send_to_scheduler(
156157 "return_code" : return_code ,
157158 "output" : output ,
158159 "started_at" : datetime .fromtimestamp (started_at ).isoformat (),
159- "spend_time" : spend_time ,
160+ "spend_time" : spend_time or 0 ,
160161 }
161162 result_data .update (
162163 {
@@ -190,9 +191,11 @@ def run_python(data):
190191
191192 logging .info ("New python run initalized... (v%s)" , VERSION )
192193
194+ started_at = time .time ()
195+
193196 script_url = data .get ("script_url" )
194197 if not script_url :
195- send_to_scheduler (False , None , "Script URL is missing" , None , data )
198+ send_to_scheduler (False , None , "Script URL is missing" , started_at , None , data )
196199 return
197200 if (
198201 to_python_bool (USE_ALTERNATIVE_FILE_SERVER_ROOT )
@@ -232,11 +235,11 @@ def run_python(data):
232235 logging .error (
233236 "Failed to get script from %s, response: %s" , script_url , resp
234237 )
235- send_to_scheduler (False , None , "Fail to get script" , None , data )
238+ send_to_scheduler (False , None , "Fail to get script" , started_at , None , data )
236239 return
237240 except Exception as e :
238241 logging .error ("Failed to get script from %s, error: %s" , script_url , e )
239- send_to_scheduler (False , None , "Fail to get script" , None , data )
242+ send_to_scheduler (False , None , "Fail to get script" , started_at , None , data )
240243 return
241244
242245 logging .debug ("Generate temporary random folder directory" )
@@ -343,8 +346,6 @@ def run_python(data):
343346 command .append ("run" ) # override command
344347 logging .debug ("command: %s" , command )
345348
346- start_at = time .time ()
347-
348349 logging .debug ("try to start the python runner image" )
349350 try :
350351 result = subprocess .run (
@@ -375,14 +376,15 @@ def run_python(data):
375376 False ,
376377 - 1 ,
377378 "The script's running time exceeded the limit and the execution was aborted." ,
379+ started_at ,
378380 DEFAULT_SUB_PROCESS_TIMEOUT ,
379381 data ,
380382 )
381383 return
382384 except Exception as e :
383385 logging .exception (e )
384386 logging .error ("Failed to run file %s error: %s" , script_url , e )
385- send_to_scheduler (False , None , None , None , data )
387+ send_to_scheduler (False , None , None , started_at , None , data )
386388 return
387389 else :
388390 logging .debug (
@@ -392,7 +394,7 @@ def run_python(data):
392394 if os .path .isfile (output_file_path ):
393395 if os .path .islink (output_file_path ):
394396 send_to_scheduler (
395- False , - 1 , "Script invalid!" , time .time () - start_at , data
397+ False , - 1 , "Script invalid!" , started_at , time .time () - started_at , data
396398 )
397399 return
398400 with open (output_file_path , "r" ) as f :
@@ -422,7 +424,7 @@ def run_python(data):
422424 except Exception as e :
423425 logging .warning ("Fail to remove container error: %s" , e )
424426
425- spend_time = time .time () - start_at
427+ spend_time = time .time () - started_at
426428 logging .info ("python run finished successful. duration was: %s" , spend_time )
427429 logging .debug (
428430 "send this to the scheduler. return_code: %s, output: %s, spend_time: %s, data: %s" ,
@@ -431,7 +433,7 @@ def run_python(data):
431433 spend_time ,
432434 data ,
433435 )
434- send_to_scheduler (return_code == 0 , return_code , output , start_at , spend_time , data )
436+ send_to_scheduler (return_code == 0 , return_code , output , started_at , spend_time , data )
435437
436438
437439####################
0 commit comments