Skip to content

Commit abc32d2

Browse files
committed
Merge pull request #104 from MarcCote/append_execution_output
Make sure commands output/error are appended to the log.
2 parents e53b42d + 598dffe commit abc32d2

File tree

4 files changed

+59
-18
lines changed

4 files changed

+59
-18
lines changed

scripts/smart_worker.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import argparse
66
import subprocess
77
import logging
8+
import time as t
89

910
from smartdispatch import utils
1011
from smartdispatch.command_manager import CommandManager
@@ -44,11 +45,17 @@ def main():
4445
stdout_filename = os.path.join(args.logs_dir, uid + ".out")
4546
stderr_filename = os.path.join(args.logs_dir, uid + ".err")
4647

47-
with open(stdout_filename, 'w') as stdout_file:
48-
with open(stderr_filename, 'w') as stderr_file:
49-
stdout_file.write("# " + command + '\n')
50-
stderr_file.write("# " + command + '\n')
48+
with open(stdout_filename, 'a') as stdout_file:
49+
with open(stderr_filename, 'a') as stderr_file:
50+
log_datetime = t.strftime("## SMART_DISPATCH - Started on: %Y-%m-%d %H:%M:%S ##\n")
51+
if stdout_file.tell() > 0: # Not the first line in the log file.
52+
log_datetime = t.strftime("\n## SMART_DISPATCH - Resumed on: %Y-%m-%d %H:%M:%S ##\n")
53+
54+
log_command = "## SMART_DISPATCH - Command: " + command + '\n'
55+
56+
stdout_file.write(log_datetime + log_command)
5157
stdout_file.flush()
58+
stderr_file.write(log_datetime + log_command)
5259
stderr_file.flush()
5360

5461
error_code = subprocess.call(command, stdout=stdout_file, stderr=stderr_file, shell=True)

smartdispatch/tests/test_smartdispatch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,4 @@ def test_log_command_line():
223223
assert_true(t.strftime("## %Y-%m-%d %H:%M:") in lines[6]) # Don't check second.
224224
assert_equal(lines[7], re.sub(r'(\[)([^\[\]]*\\ [^\[\]]*)(\])', r'"\1\2\3"', command_3))
225225

226-
shutil.rmtree(temp_dir)
226+
shutil.rmtree(temp_dir)

tests/test_smart_dispatch.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ def test_main_resume(self):
6060
assert_equal(len(open(running_commands_file).readlines()), 0)
6161
assert_equal(len(open(pending_commands_file).readlines()), len(commands))
6262

63-
# Test when batch_uid is a path instead of a jobname.
64-
# Setup
63+
# Test when batch_uid is a path instead of a jobname.
64+
# Setup
6565
call(self.launch_command, shell=True)
6666
batch_uid = os.path.join(self.logs_dir, os.listdir(self.logs_dir)[0])
6767

68-
# Simulate that some commands are in the running state.
68+
# Simulate that some commands are in the running state.
6969
path_job_commands = os.path.join(self.logs_dir, batch_uid, "commands")
7070
pending_commands_file = pjoin(path_job_commands, "commands.txt")
7171
running_commands_file = pjoin(path_job_commands, "running_commands.txt")
@@ -85,7 +85,6 @@ def test_main_resume(self):
8585
assert_equal(len(open(running_commands_file).readlines()), 0)
8686
assert_equal(len(open(pending_commands_file).readlines()), len(commands))
8787

88-
8988
def test_main_resume_only_pending(self):
9089
# SetUp
9190
call(self.launch_command, shell=True)

tests/test_smart_worker.py

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ def tearDown(self):
3131
def test_main(self):
3232
command = ["smart_worker.py", self.command_manager._commands_filename, self.logs_dir]
3333
assert_equal(call(command), 0)
34+
# Simulate a resume, i.e. re-run the command, the output/error should be concatenated.
35+
self.command_manager.set_commands_to_run(self.commands)
36+
assert_equal(call(command), 0)
3437

3538
# Check output logs
3639
filenames = os.listdir(self.logs_dir)
@@ -41,13 +44,29 @@ def test_main(self):
4144
uid = os.path.splitext(os.path.basename(log_filename))[0]
4245
executed_command = self.commands[self.commands_uid.index(uid)]
4346

44-
# First line is the executed command in comment
45-
header = logfile.readline().strip()
46-
assert_equal(header, "# " + executed_command)
47+
# Since the command was run twice.
48+
for i in range(2):
49+
# First line is the datetime of the executed command in comment.
50+
line = logfile.readline().strip()
51+
52+
if i == 0:
53+
assert_true("Started" in line)
54+
else:
55+
assert_true("Resumed" in line)
56+
57+
assert_true(line.startswith("## SMART_DISPATCH"))
58+
assert_true(time.strftime("%Y-%m-%d %H:%M:") in line) # Don't check seconds.
59+
60+
# Second line is the executed command in comment.
61+
line = logfile.readline().strip()
62+
assert_true(executed_command in line)
4763

48-
# Next should be the command's output
49-
output = logfile.readline().strip()
50-
assert_equal(output, executed_command[-1]) # We know those are 'echo' of a digit
64+
# Next should be the command's output
65+
line = logfile.readline().strip()
66+
assert_equal(line, executed_command[-1]) # We know those are 'echo' of a digit
67+
68+
# Empty line
69+
assert_equal(logfile.readline().strip(), "")
5170

5271
# Log should be empty now
5372
assert_equal("", logfile.read())
@@ -60,9 +79,25 @@ def test_main(self):
6079
uid = os.path.splitext(os.path.basename(log_filename))[0]
6180
executed_command = self.commands[self.commands_uid.index(uid)]
6281

63-
# First line is the executed command in comment
64-
header = logfile.readline().strip()
65-
assert_equal(header, "# " + executed_command)
82+
# Since the command was run twice.
83+
for i in range(2):
84+
# First line is the datetime of the executed command in comment.
85+
line = logfile.readline().strip()
86+
87+
if i == 0:
88+
assert_true("Started" in line)
89+
else:
90+
assert_true("Resumed" in line)
91+
92+
assert_true(line.startswith("## SMART_DISPATCH"))
93+
assert_true(time.strftime("%Y-%m-%d %H:%M:") in line) # Don't check seconds.
94+
95+
# Second line is the executed command in comment.
96+
line = logfile.readline().strip()
97+
assert_true(executed_command in line)
98+
99+
# Empty line
100+
assert_equal(logfile.readline().strip(), "")
66101

67102
# Log should be empty now
68103
assert_equal("", logfile.read())

0 commit comments

Comments
 (0)