Piscal Queue working more resilient to error

This commit is contained in:
2016-04-18 10:44:36 -04:00
parent 6623aa48ea
commit ca1a235c1d
21 changed files with 418 additions and 176 deletions
+83 -44
View File
@@ -1,27 +1,33 @@
#!/bin/bash
# piscal manager script
usage="$(basename "$0") [-h] -d directory_name [-f input_filename|-s] -- script to manage Piscal
usage="$(basename "$0") [-h] -d directory_name -p photosynthetic_type [-s|-c|-k] -- script to manage Piscal
where:
-h show this help text
-d working directory name
-f input filename
-s job status"
-p photosynthetic type
-s start job
-c cleanup directory
-k kill job"
# Initialize variables:
# http://stackoverflow.com/a/246128/99492
base_directory="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
directory_name=""
photosynthetic_type="C3_photosynthesis_leafweb" #default
task="get_status" # default task
input_directory_name="input"
pid_filename="piscal.pid"
stderr_filename="piscal.err"
output_directory_name="output/fitresult/touser"
cleaned_input_directory_name="output/clninput"
launcher="$base_directory/piscal_launcher.sh"
# http://stackoverflow.com/a/14203146/99492
# http://wiki.bash-hackers.org/howto/getopts_tutorial
# Initialize variables:
base_directory="/home/poprhythm/LeafWeb"
directory_name=""
input_filename=""
task="start" # default task
pid_filename="piscal.pid"
out_filename="piscal.out"
launcher="$base_directory/piscal_launcher.sh"
while getopts "hd:f:s" opt; do
while getopts "hd:f:p:sck" opt; do
#echo "$opt = $OPTARG"
case "$opt" in
h )
@@ -31,12 +37,17 @@ while getopts "hd:f:s" opt; do
d )
directory_name=$OPTARG
;;
f )
input_filename=$OPTARG
task="start"
p )
photosynthetic_type=$OPTARG
;;
s )
task="get_status"
task="launch"
;;
c )
task="cleanup"
;;
k )
task="kill"
;;
\?) printf "illegal option: -%s\n" "$OPTARG" >&2
echo "$usage" >&2
@@ -44,55 +55,83 @@ while getopts "hd:f:s" opt; do
;;
esac
done
## Examine directories
if [ -z "$directory_name" ]; then
echo "directory name required (-d)"
exit 1
fi
working_directory="$base_directory/$directory_name"
if [ ! -d "$working_directory" ]; then
echo "directory $working_directory not found"
echo "working directory $working_directory not found"
exit 1
fi
if [ "$task" = "start" ]; then
if [ -z "$input_filename" ]; then
echo "input filename required (-f)"
exit 1
fi
input_directory="$working_directory/$input_directory_name"
if [ ! -d "$input_directory" ]; then
echo "input directory $input_directory not found"
exit 1
fi
pid_path="$working_directory/$pid_filename"
## Process task
if [ "$task" = "launch" ]; then
piscal_config_file="$working_directory"/piscal.cfg
# write config file for piscal
echo $photosynthetic_type > "$piscal_config_file"
find "$input_directory" -maxdepth 1 -type f\
-printf '%P\n'\
>> "$piscal_config_file"
if [ ! -f "$working_directory/$input_filename" ]; then
echo "input filename $working_directory/$input_filename not found"
exit 1
fi
command="$launcher -d $working_directory -f $input_filename"
nohup ${command} > $working_directory/$out_filename 2>&1 &
command="$launcher -d $working_directory -f piscal.cfg"
# launch it, sending error output to file
nohup ${command} > $working_directory/$stderr_filename 2>&1 &
# write the PID to a temp file to check for completion later
echo $! > $working_directory/$pid_filename
echo started
elif [ "$task" = "get_status" ]; then
pid_path="$working_directory/$pid_filename"
# if the pid doesn't exist, then process never started
# if the pid doesn't exist, then process hasn't started
if [ ! -f "$pid_path" ]; then
echo "no pid file found"
exit 1
echo "not started"
exit
fi
pid=$(head -n 1 $pid_path)
# if the pid exists, check the process status using ps
if ps -p $pid > /dev/null; then
# if it is in ps, then it's still running
echo running
else
# otherwise, it is complete, check the output for success/error
# TODO: examine output for errors, etc
#cat piscal.out
if [ ! -d "$working_directory/output" ]; then
echo "output directory $working_directory/output not found"
# otherwise, it is complete, check for runtime errors
if [ -s "$working_directory/$stderr_filename" ]; then
cat "$working_directory/$stderr_filename"
exit 1
fi
echo success
find "$working_directory/output"/*
output_directory="$working_directory/$output_directory_name"
if [ ! -d "$output_directory" ]; then
echo "output directory $output_directory not found"
exit 1
fi
echo complete
find "$output_directory"/*
fi
fi
elif [ "$task" = "cleanup" ]; then
rm -rf "$working_directory"
echo hi
elif [ "$task" = "kill" ]; then
# if the pid doesn't exist, then process hasn't started
if [ ! -f "$pid_path" ]; then
echo "not started"
exit
fi
pid=$(head -n 1 $pid_path)
# if the pid exists, check the process status using ps
if ps -p $pid > /dev/null; then
# if it is in ps, then it's still running
kill $pid
fi
echo killed
fi