Skip to content

Commit d42d4f5

Browse files
committed
Add option to output all output to file within INLClient
1 parent 6a415d2 commit d42d4f5

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

client/JobRunner.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,13 @@ def enqueue_output(out, queue):
300300
t.daemon = True
301301
t.start();
302302

303+
log_job_output_file = None
304+
if 'log_job_output' in self.client_info and self.client_info['log_job_output'] is True:
305+
if 'log_dir' in self.client_info and self.client_info['log_dir']:
306+
log_job_output = os.path.join(self.client_info['log_dir'], 'job_{}_{}'.format(self.job_data['job_id'], step_data['step_num']))
307+
log_job_output_file = open(log_job_output, 'wb')
308+
logger.info('Writing job output to {}'.format(log_job_output))
309+
303310
while proc.poll() is None:
304311
if self.canceled or self.stopped:
305312
logger.info("Killing job\n")
@@ -309,9 +316,14 @@ def enqueue_output(out, queue):
309316
break
310317

311318
output = self.get_output_from_queue(q)
312-
if output and not over_max:
313-
out.extend(output)
314-
chunk_out.extend(output)
319+
if output:
320+
if not over_max:
321+
out.extend(output)
322+
chunk_out.extend(output)
323+
if log_job_output_file is not None:
324+
for chunk in output:
325+
log_job_output_file.write(chunk.encode('utf-8'))
326+
log_job_output_file.flush()
315327

316328
# Don't worry that "out" might contain multibyte characters, we
317329
# just want a rough size check
@@ -343,9 +355,16 @@ def enqueue_output(out, queue):
343355
t.join() # make sure the step has no more output
344356

345357
# we might not have gotten everything
346-
out.extend(self.get_output_from_queue(q, timeout=0))
358+
remaining_output = self.get_output_from_queue(q, timeout=0)
359+
out.extend(remaining_output)
347360
if not step_data['canceled'] or keep_output:
348361
step_data['output'] = ''.join(out)
362+
if log_job_output_file is not None:
363+
if remaining_output:
364+
for chunk in remaining_output:
365+
log_job_output_file.write(remaining_output.encode('utf-8'))
366+
log_job_output_file.flush()
367+
log_job_output_file.close()
349368
step_data['complete'] = True
350369
step_data['time'] = int(time.time() - start_time) #would be float
351370
return step_data

client/inl_client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ def commandline_client(args):
5454
action='store_true',
5555
dest='user_client_suffix',
5656
help='Adds the user to client name as a suffix, i.e, sets the name to <hostname>_<user>_<client number>')
57-
57+
parser.add_argument("--log-job-output",
58+
action='store_true',
59+
default=False,
60+
help='If set, logs all job output to the logging directory with the file prefix "job_"')
5861

5962
parsed = parser.parse_args(args)
6063
home = os.environ.get("CIVET_HOME", os.path.join(os.environ["HOME"], "civet"))
@@ -72,6 +75,7 @@ def commandline_client(args):
7275
"ssl_cert": "",
7376
"log_file": "",
7477
"log_dir": log_dir,
78+
"log_job_output": parsed.log_job_output,
7579
"build_key": "",
7680
"single_shot": False,
7781
"poll": 30,

client/tests/test_JobRunner.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,15 @@ def test_max_step_time(self):
340340
self.assertIn("taking longer than the max", out["output"])
341341
self.assertLess(out["time"], 10)
342342
self.assertEqual(out["canceled"], True)
343+
344+
def test_log_job_output(self):
345+
with JobRunner.temp_file() as script:
346+
script.write(b'echo "foo"\nsleep 5\necho "bar"')
347+
script.close()
348+
with open(os.devnull, "wb") as devnull:
349+
r = self.create_runner()
350+
r.client_info['log_job_output'] = True
351+
proc = r.create_process(script.name, {}, devnull)
352+
r.read_process_output(proc, r.job_data["steps"][0], {'step_num': 0})
353+
with open(os.path.join(r.client_info['log_dir'], 'job_1_0'), 'r') as f:
354+
self.assertEqual(f.readlines(), ['foo\n', 'bar\n'])

0 commit comments

Comments
 (0)