2
2
import json
3
3
import logging
4
4
import os
5
+ import sys
5
6
import time
6
7
import uuid
7
8
from contextlib import contextmanager
@@ -88,19 +89,18 @@ def build_job_and_run(
88
89
r = requests .post (url_submit , data = json .dumps (job_request ), headers = headers )
89
90
90
91
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 } " )
93
93
94
94
response = r .json ()
95
95
96
96
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 ))
100
100
raise Exception ("Submission failed - no job id in response" )
101
101
102
102
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 } " )
104
104
105
105
return job_id
106
106
@@ -111,8 +111,6 @@ def submit_orca(session, sim: Simulation):
111
111
user = sim .person .identifier
112
112
uid = uuid .uuid4 ()
113
113
114
- print (f"{ ROOT_DIR } { user } " )
115
-
116
114
job_name = application_name + "-" + str (uid )
117
115
working_dir = ROOT_DIR + user + "/" + job_name
118
116
cluster_dir = CLUSTER_ROOT_DIR + user + "/" + job_name
@@ -163,6 +161,7 @@ def submit_orca(session, sim: Simulation):
163
161
sim .status = SimulationStatus .submitted
164
162
update_simulation (session , sim )
165
163
except Exception :
164
+ logger .exception ("Error submitting orca job" )
166
165
sim .status = SimulationStatus .failed
167
166
update_simulation (session , sim )
168
167
@@ -260,12 +259,13 @@ def run_update():
260
259
if j ["account" ] == SLURM_USER :
261
260
job_map [j ["job_id" ]] = {"state" : j ["job_state" ][0 ]}
262
261
263
- print (f"Number of active jobs { len (active )} " )
262
+ if len (active ) != 0 :
263
+ logger .info (f"Number of active jobs { len (active )} " )
264
264
265
265
for a in active :
266
266
if a .job_id in job_map :
267
267
state = job_map [a .job_id ]["state" ]
268
- print ( state )
268
+ logger . debug ( f"Job { a . job_id } has state { state } " )
269
269
270
270
if state == JOB_RUNNING and a .status != SimulationStatus .running :
271
271
a .status = SimulationStatus .running
@@ -285,6 +285,7 @@ def run_update():
285
285
286
286
else :
287
287
# TODO better state for whatever slurm might return
288
+ logger .error (f"Active job with id { a .job_id } not in job_map" )
288
289
a .status = SimulationStatus .failed
289
290
update_simulation (session , a )
290
291
@@ -295,11 +296,23 @@ def test_read():
295
296
for sim in sims :
296
297
if sim .simulation_type_id == 1 :
297
298
jf , calc = get_orca_jobfile_with_technique (session , sim .id )
298
- print (jf )
299
299
300
300
301
301
def 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
+
303
316
while True :
304
317
try :
305
318
run_update ()
@@ -308,4 +321,4 @@ def main():
308
321
except Exception :
309
322
logger .exception ("Error in update loop" )
310
323
time .sleep (10 )
311
- print ("Loop iteration complete" )
324
+ logger . info ("Loop iteration complete" )
0 commit comments