Skip to content

Commit 2a4f2cb

Browse files
committed
Merge pull request #97 from MarcCote/resume_only_pending_commands
Resume only pending commands
2 parents b3ff011 + 5c73901 commit 2a4f2cb

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

scripts/smart_dispatch.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ def main():
8383
if not utils.yes_no_prompt("Do you want to continue?", 'n'):
8484
exit()
8585

86-
command_manager.reset_running_commands()
86+
if not args.onlyPending:
87+
command_manager.reset_running_commands()
88+
8789
nb_commands = command_manager.get_nb_commands_to_run()
8890

8991
# If no pool size is specified the number of commands is taken
@@ -148,6 +150,7 @@ def parse_arguments():
148150
launch_parser.add_argument("commandAndOptions", help="Options for the commands.", nargs=argparse.REMAINDER)
149151

150152
resume_parser = subparsers.add_parser('resume', help="Resume jobs from batch UID.")
153+
resume_parser.add_argument('--onlyPending', action='store_true', help='Resume only pending commands.')
151154
resume_parser.add_argument("batch_uid", help="Batch UID of the jobs to resume.")
152155

153156
args = parser.parse_args()

tests/test_smart_dispatch.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import unittest
33
import tempfile
44
import shutil
5+
from os.path import join as pjoin
56

67
from subprocess import call
78

@@ -15,13 +16,14 @@ def setUp(self):
1516
self.logs_dir = os.path.join(self.testing_dir, 'SMART_DISPATCH_LOGS')
1617

1718
base_command = 'smart_dispatch.py --pool 10 -C 42 -q test -t 5:00 -x {0}'
18-
self.launch_command = base_command.format('launch echo "1 2 3 4" "6 7 8" "9 0"')
19+
self.launch_command = base_command.format('launch echo "[1 2 3 4]" "[6 7 8]" "[9 0]"')
1920
self.resume_command = base_command.format('resume {0}')
2021

2122
self._cwd = os.getcwd()
2223
os.chdir(self.testing_dir)
2324

2425
def tearDown(self):
26+
print "Tear down"
2527
os.chdir(self._cwd)
2628
shutil.rmtree(self.testing_dir)
2729

@@ -39,10 +41,49 @@ def test_main_resume(self):
3941
call(self.launch_command, shell=True)
4042
batch_uid = os.listdir(self.logs_dir)[0]
4143

42-
# Actual test
44+
# Simulate that some commands are in the running state.
45+
path_job_commands = os.path.join(self.logs_dir, batch_uid, "commands")
46+
pending_commands_file = pjoin(path_job_commands, "commands.txt")
47+
running_commands_file = pjoin(path_job_commands, "running_commands.txt")
48+
commands = open(pending_commands_file).read().strip().split("\n")
49+
with open(running_commands_file, 'w') as running_commands:
50+
running_commands.write("\n".join(commands[::2]) + "\n")
51+
with open(pending_commands_file, 'w') as pending_commands:
52+
pending_commands.write("\n".join(commands[1::2]) + "\n")
53+
54+
# Actual test (should move running commands back to pending).
4355
exit_status = call(self.resume_command.format(batch_uid), shell=True)
4456

4557
# Test validation
4658
assert_equal(exit_status, 0)
4759
assert_true(os.path.isdir(self.logs_dir))
4860
assert_equal(len(os.listdir(self.logs_dir)), 1)
61+
assert_equal(len(open(running_commands_file).readlines()), 0)
62+
assert_equal(len(open(pending_commands_file).readlines()), len(commands))
63+
64+
def test_main_resume_only_pending(self):
65+
# SetUp
66+
call(self.launch_command, shell=True)
67+
batch_uid = os.listdir(self.logs_dir)[0]
68+
69+
# Simulate that some commands are in the running state.
70+
path_job_commands = os.path.join(self.logs_dir, batch_uid, "commands")
71+
pending_commands_file = pjoin(path_job_commands, "commands.txt")
72+
running_commands_file = pjoin(path_job_commands, "running_commands.txt")
73+
commands = open(pending_commands_file).read().strip().split("\n")
74+
with open(running_commands_file, 'w') as running_commands:
75+
running_commands.write("\n".join(commands[::2]) + "\n")
76+
with open(pending_commands_file, 'w') as pending_commands:
77+
pending_commands.write("\n".join(commands[1::2]) + "\n")
78+
79+
# Actual test (should NOT move running commands back to pending).
80+
command_line = self.resume_command.format(batch_uid)
81+
command_line += " --onlyPending"
82+
exit_status = call(command_line, shell=True)
83+
84+
# Test validation
85+
assert_equal(exit_status, 0)
86+
assert_true(os.path.isdir(self.logs_dir))
87+
assert_equal(len(os.listdir(self.logs_dir)), 1)
88+
assert_equal(len(open(running_commands_file).readlines()), len(commands[::2]))
89+
assert_equal(len(open(pending_commands_file).readlines()), len(commands[1::2]))

0 commit comments

Comments
 (0)