22import json
33import logging
44import os
5+ import sys
56import time
67import uuid
78from contextlib import contextmanager
@@ -88,19 +89,18 @@ def build_job_and_run(
8889 r = requests .post (url_submit , data = json .dumps (job_request ), headers = headers )
8990
9091 if r .status_code != 200 :
91- print (r .status_code )
92- raise Exception ("Job submission response not successful" )
92+ raise Exception (f"Job submission response not successful { r .status_code } " )
9393
9494 response = r .json ()
9595
9696 if "job_id" not in response :
97- print (f"Error submitting { job_name } " )
98- print (pformat (job_request ))
99- print (pformat (response ))
97+ logger . error (f"Error submitting { job_name } " )
98+ logger . error (pformat (job_request ))
99+ logger . error (pformat (response ))
100100 raise Exception ("Submission failed - no job id in response" )
101101
102102 job_id = response ["job_id" ]
103- print (f"Simulation job { job_name } has ID { job_id } " )
103+ logger . info (f"Simulation job { job_name } has ID { job_id } " )
104104
105105 return job_id
106106
@@ -111,8 +111,6 @@ def submit_orca(session, sim: Simulation):
111111 user = sim .person .identifier
112112 uid = uuid .uuid4 ()
113113
114- print (f"{ ROOT_DIR } { user } " )
115-
116114 job_name = application_name + "-" + str (uid )
117115 working_dir = ROOT_DIR + user + "/" + job_name
118116 cluster_dir = CLUSTER_ROOT_DIR + user + "/" + job_name
@@ -163,6 +161,7 @@ def submit_orca(session, sim: Simulation):
163161 sim .status = SimulationStatus .submitted
164162 update_simulation (session , sim )
165163 except Exception :
164+ logger .exception ("Error submitting orca job" )
166165 sim .status = SimulationStatus .failed
167166 update_simulation (session , sim )
168167
@@ -260,12 +259,13 @@ def run_update():
260259 if j ["account" ] == SLURM_USER :
261260 job_map [j ["job_id" ]] = {"state" : j ["job_state" ][0 ]}
262261
263- print (f"Number of active jobs { len (active )} " )
262+ if len (active ) != 0 :
263+ logger .info (f"Number of active jobs { len (active )} " )
264264
265265 for a in active :
266266 if a .job_id in job_map :
267267 state = job_map [a .job_id ]["state" ]
268- print ( state )
268+ logger . debug ( f"Job { a . job_id } has state { state } " )
269269
270270 if state == JOB_RUNNING and a .status != SimulationStatus .running :
271271 a .status = SimulationStatus .running
@@ -285,6 +285,7 @@ def run_update():
285285
286286 else :
287287 # TODO better state for whatever slurm might return
288+ logger .error (f"Active job with id { a .job_id } not in job_map" )
288289 a .status = SimulationStatus .failed
289290 update_simulation (session , a )
290291
@@ -295,11 +296,23 @@ def test_read():
295296 for sim in sims :
296297 if sim .simulation_type_id == 1 :
297298 jf , calc = get_orca_jobfile_with_technique (session , sim .id )
298- print (jf )
299299
300300
301301def main ():
302- print ("Running main loop" )
302+ rootlogger = logging .getLogger ()
303+ formatter = logging .Formatter (
304+ "%(asctime)s - %(levelname)s - %(name)s - %(message)s" ,
305+ datefmt = "%Y-%m-%d %H:%M:%S" ,
306+ )
307+
308+ sh = logging .StreamHandler (sys .stdout )
309+ sh .setFormatter (formatter )
310+ rootlogger .addHandler (sh )
311+ rootlogger .setLevel (logging .DEBUG )
312+ rootlogger .debug ("Logging Configured" )
313+
314+ logger .info ("Running main loop" )
315+
303316 while True :
304317 try :
305318 run_update ()
@@ -308,4 +321,4 @@ def main():
308321 except Exception :
309322 logger .exception ("Error in update loop" )
310323 time .sleep (10 )
311- print ("Loop iteration complete" )
324+ logger . info ("Loop iteration complete" )
0 commit comments