Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate canceling script #173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions mcpartools/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ def run(self):
# generate submit script
self.generate_submit_script()

# generate kill script
self.generate_kill_script()

# copy input files
self.copy_input()

Expand Down Expand Up @@ -201,6 +204,11 @@ def generate_submit_script(self):
jobs_no=self.options.jobs_no,
workspace_dir=self.workspace_dir)

def generate_kill_script(self):
script_path = os.path.join(self.main_dir, self.scheduler.kill_script)
logger.debug("Preparation to generate " + script_path)
self.scheduler.write_kill_script(script_path)

def copy_input(self):
indir_name = 'input'
indir_path = os.path.join(self.main_dir, indir_name)
Expand Down
14 changes: 14 additions & 0 deletions mcpartools/scheduler/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(self, scheduler_options):

submit_script = 'submit.sh'
main_run_script = 'main_run.sh'
kill_script = 'cancel.sh'

def submit_script_body(self, jobs_no, main_dir, workspace_dir):
from pkg_resources import resource_string
Expand All @@ -51,6 +52,12 @@ def main_run_script_body(self, jobs_no, workspace_dir):
jobs_no=jobs_no)
return self.main_run_script

def submit_kill_body(self, ):
from pkg_resources import resource_string
tpl = resource_string(__name__, self.kill_script_template)
self.kill_script = tpl.decode('ascii')
return self.kill_script

def write_submit_script(self, main_dir, script_basename, jobs_no, workspace_dir):
script_path = os.path.join(main_dir, script_basename)
fd = open(script_path, 'w')
Expand All @@ -72,3 +79,10 @@ def write_main_run_script(self, jobs_no, output_dir):
os.chmod(out_file_path, 0o750)
logger.debug("Saved main run script: " + out_file_path)
logger.debug("Output dir " + output_dir)

def write_kill_script(self, script_path):
fd = open(script_path, 'w')
fd.write(self.submit_kill_body())
fd.close()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with open(script_path, 'w') as f:
    f.write(self.submit_kill_body())

?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it work for python 2.6 ?

os.chmod(script_path, 0o750)
logger.debug("Saved kill script: " + script_path)
30 changes: 30 additions & 0 deletions mcpartools/scheduler/data/kill_slurm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

# submit.log is for storing stdout and stderr of submit.sh
LOGFILE="$(cd $(dirname $0) && pwd)/submit.log"

RE="Job ID: ([0-9]*)"

# no log file. Probably submit.sh not run
if [ ! -f $LOGFILE ]; then
echo "File not found: $LOGFILE"
echo "Make sure you run submit script"
exit 1
fi

cat ${LOGFILE} | while read line
do
if [[ ${line} =~ $RE ]];
then
JOB_ID=${BASH_REMATCH[1]};
if [ -n "$JOB_ID" ]
then
scancel ${JOB_ID}
if [ $? -eq 0 ]; then
echo "Job with id: $JOB_ID canceled successfully"
else
echo "Unable to cancel job: $JOB_ID"
fi
fi
fi
done
30 changes: 30 additions & 0 deletions mcpartools/scheduler/data/kill_torque.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

# submit.log is for storing stdout and stderr of submit.sh
LOGFILE="$(cd $(dirname $0) && pwd)/submit.log"

RE="Job ID: ([0-9]*\[\])"

# no log file. Probably submit.sh not run
if [ ! -f $LOGFILE ]; then
echo "File not found: $LOGFILE"
echo "Make sure you run submit script"
exit 1
fi

cat ${LOGFILE} | while read line
do
if [[ ${line} =~ $RE ]];
then
JOB_ID=${BASH_REMATCH[1]};
if [ -n "$JOB_ID" ]
then
qdel ${JOB_ID}
if [ $? -eq 0 ]; then
echo "Job with id: $JOB_ID canceled successfully"
else
echo "Unable to cancel job: $JOB_ID"
fi
fi
fi
done
2 changes: 2 additions & 0 deletions mcpartools/scheduler/slurm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ def __init__(self, options_content):
submit_script_template = os.path.join('data', 'submit_slurm.sh')

main_run_script_template = os.path.join('data', 'run_slurm.sh')

kill_script_template = os.path.join('data', 'kill_slurm.sh')
2 changes: 2 additions & 0 deletions mcpartools/scheduler/torque.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ def __init__(self, options_content):
submit_script_template = os.path.join('data', 'submit_torque.sh')

main_run_script_template = os.path.join('data', 'run_torque.sh')

kill_script_template = os.path.join('data', 'kill_torque.sh')