Process Array Example using Generators

# This module shows DeBasher's generator method as a third way to
# create multiple tasks for the same process, besides the loop
# written by hand and the array option handler seen in the previous
# two examples. Instead of a define_opts function that decides how
# many tasks to create and saves each option list itself,
# array_writer and array_reader implement generate_opts_size
# (returning the number of tasks) and generate_opts (building the
# option list for one task, given its index). DeBasher calls
# generate_opts_size once and then generate_opts once per index, so
# the iteration itself belongs to the engine rather than to the
# module.

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="process id"
    explain_opt "-id" "<int>" "$description"

    # -outf option
    local description="output file"
    explain_opt "-outf" "<file>" "$description"
}

array_writer_identify_cmdline_opts()
{
    opt_is_cmdline "-c"
}

array_writer_generate_opts_size()
{
    # Initialize variables
    local cmdline=$1
    local process_spec=$2
    local process_name=$3
    local process_outdir=$4

    echo 4
}

array_writer_generate_opts()
{
    # Initialize variables
    local cmdline=$1
    local process_spec=$2
    local process_name=$3
    local process_outdir=$4
    local task_idx=$5
    local optlist=""

    # -c option
    define_cmdline_opt "$cmdline" "-c" optlist || return 1

    # -id option
    define_opt "-id" $task_idx optlist || return 1

    # -outf option
    define_opt "-outf" "${process_outdir}/${task_idx}" optlist || return 1

    save_opt_list optlist
}

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_generate_opts_size()
{
    # Initialize variables
    local cmdline=$1
    local process_spec=$2
    local process_name=$3
    local process_outdir=$4

    echo 4
}

array_reader_generate_opts()
{
    # Initialize variables
    local cmdline=$1
    local process_spec=$2
    local process_name=$3
    local process_outdir=$4
    local task_idx=$5
    local optlist=""

    # -infile option
    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_opt_list optlist
}

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_generator_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"
}