@@ -185,7 +185,9 @@ def write_slurm_script(run_id, job_type, input_file_local, output_file_local, si
185185 script .write ('\n ' )
186186
187187 script .write ('echo Running Job $SLURM_JOB_ID \n \n ' )
188-
188+ notify_job_start_cmd = f'curl -X POST http://{ CONTROLLER_HOSTNAME } :5000/job-start -d \" job_type={ job_type } \" -d \" run_id={ run_id } \" \n '
189+ script .write (notify_job_start_cmd )
190+
189191 # Execute the singularity command
190192 script .write (f'{ singularity_run_cmd } \n ' )
191193
@@ -746,6 +748,44 @@ def run_sacct():
746748 except subprocess .CalledProcessError as e :
747749 return log_and_return_error (str (e ), 500 )
748750
751+ @app .route ('/job-start' , methods = ['POST' ])
752+ def job_start ():
753+ job_type = request .form .get ('job_type' )
754+ run_id = request .form .get ('run_id' )
755+
756+ # Validate inputs
757+ if not job_type :
758+ return log_and_return_error ("No job type provided" , 400 )
759+
760+ if not run_id :
761+ return log_and_return_error ("No job id provided" , 400 )
762+
763+
764+ postprocessing_dir = os .path .join ('postprocess' , job_type , run_id )
765+ callback_script = os .path .join (postprocessing_dir , 'callback' )
766+
767+ if not os .path .exists (callback_script ):
768+ error_msg = f"Callback script ${ callback_script } does not exist"
769+ logger .error (error_msg )
770+ log_and_return_error (error_msg , 500 )
771+
772+ # Construct the command to run sleep and sacct
773+ with open (callback_script , 'r' ) as f :
774+ callback = f .read ().replace ('__job_status__' , 'STARTING' )
775+
776+ try :
777+ # Run the command in the background using subprocess.Popen
778+ logger .info (f"Running: { callback } " )
779+ subprocess .Popen (callback , shell = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
780+
781+ # Return an immediate response while the command runs in the background
782+ return jsonify ({"success" : True , "message" : f"Starting callback was submitted" }), 200
783+
784+ except Exception as e :
785+ # Return an error message if something goes wrong
786+ return log_and_return_error (str (e ), 500 )
787+
788+
749789@app .route ('/postprocess' , methods = ['POST' ])
750790def postprocess ():
751791 slurm_job_id = request .form .get ('slurm_job_id' )
0 commit comments