# This module reproduces the exact same array_writer and array_reader
# scenario as the previous example, but each define_opts method
# follows DeBasher's array option handler convention instead of a
# loop written by hand: an array variable named "array", built by
# user code, is iterated over using an index variable named
# "task_idx". This is exactly the shape produced by the frontend's option
# handler code generation and recognized back by its import logic,
# so loading this module resolves both processes to array mode in
# the visual editor instead of falling back to a manual handler.
array_writer_document()
{
document_process "Executes an array of 4 tasks. Each task creates a file containing the task index."
}
array_writer_explain_opts()
{
# -c option
local description="Sleep time in seconds"
explain_opt "-c" "<int>" "$description"
# -id option
local description="id of writer"
explain_opt "-id" "<int>" "$description"
# -outf option
local description="output file of writer"
explain_opt "-outf" "<file>" "$description"
}
array_writer_identify_cmdline_opts()
{
opt_is_cmdline "-c"
}
array_writer_define_opts()
{
# Initialize variables
local cmdline=$1
local process_spec=$2
local process_name=$3
local process_outdir=$4
# Array of task ids: the simplest case, where each element is
# just its own index
array=(0 1 2 3)
local task_idx
for task_idx in "${!array[@]}"; do
local optlist=""
# -c option
define_cmdline_opt "$cmdline" "-c" optlist || return 1
# -id option
define_opt "-id" "${array[$task_idx]}" optlist || return 1
# -outf option
define_opt "-outf" "${process_outdir}/${task_idx}" optlist || return 1
# Save option list
save_opt_list optlist
done
}
array_writer()
{
# Initialize variables
local sleep_time=$(read_opt_value_from_func_args "-c" "$@")
local id=$(read_opt_value_from_func_args "-id" "$@")
local outf=$(read_opt_value_from_func_args "-outf" "$@")
# Sleep some time
sleep ${sleep_time}
# Create file
echo $id > "${outf}"
}
array_writer_reset_outfiles()
{
# Initialize variables
local outf=$(read_opt_value_from_func_args "-outf" "$@")
# Remove output file
if [ -f "${outf}" ]; then
rm "${outf}"
fi
}
array_reader_document()
{
document_process "Reads each array_writer task's output file and copies its content into a directory of the task's own, below the output directory of the process."
}
array_reader_explain_opts()
{
# -infile option
local description="input file"
explain_opt "-infile" "<file>" "$description"
# -outdir option
local description="directory of the task, below the output directory of the process"
explain_opt "-outdir" "<dir>" "$description"
}
array_reader_identify_cmdline_opts()
{
:
}
array_reader_define_opts()
{
# Initialize variables
local cmdline=$1
local process_spec=$2
local process_name=$3
local process_outdir=$4
array=(0 1 2 3)
local task_idx
for task_idx in "${!array[@]}"; do
local optlist=""
# -infile option: the task of array_writer with the same index
define_opt_from_proc_task_out "-infile" "array_writer" "${task_idx}" "-outf" optlist || return 1
# -outdir option: a directory of the task's own, which the engine
# creates and empties before the task runs
define_opt_from_process_outdir "-outdir" optlist --subdir "${task_idx}" || return 1
# Save option list
save_opt_list optlist
done
}
array_reader()
{
# Initialize variables
local infile=$(read_opt_value_from_func_args "-infile" "$@")
local outd=$(read_opt_value_from_func_args "-outdir" "$@")
# Copy content of infile to auxiliary file
cat "${infile}" > "${outd}"/aux
# Copy content of infile to final file
cat "${outd}"/aux > "${outd}"/out
}
array_reader_post()
{
logmsg "Cleaning directory..."
# Initialize variables
local outd=$(read_opt_value_from_func_args "-outdir" "$@")
# Remove auxiliary file
rm "${outd}"/aux
logmsg "Cleaning finished"
}
debasher_array_example_program()
{
add_debasher_process "array_writer" "cpus=1 mem=32 time=00:01:00,00:02:00 throttle=2"
add_debasher_process "array_reader" "cpus=1 mem=32 time=00:01:00 throttle=4"
}