TABLE OF CONTENTS


::pwtk::afterDone

SYNOPSIS

proc ::pwtk::afterDone {args} {

USAGE

   ::pwtk::afterDone  ?OPTIONS?  OUTPUT_FILE_LIST  ?SCRIPT?

PURPOSE

Wait for the calculations associated with the list of output files specified in OUTPUT_FILE_LIST to complete. If the SCRIPT argument is supplied, it will be executed after waiting is completed (hence, the name "afterDone").

This command is blocking, it returns only after waiting and the execution of the optional SCRIPT are completed.

If maximum waiting time is exceeded (default = 2 weeks), PWTK aborts.

OPTIONS

ARGUMENTS

SOURCE

    set nargmin 1
    set nargmax 2
    set usage "?-check TIME_INTERVAL?  ?-max TIME?  OUTPUT_FILE_LIST  ?SCRIPTS?"
    set options {
        {check.arg  30s    "time interval for checking"}
        {max.arg    2week  "maximum waiting time"}
    }
    ::pwtk::parseOpt_    
    lassign $args outputFileList script

    # N.B. do not check if the output files exist because they may not yet
    # (e.g. this is (almost) always the case for job schedulers)
    
    if { [llength $outputFileList] == 1 } {
        set job job
    } else {
        set job jobs
    }
    print "Waiting for the [join $outputFileList {, }] $job to be completed ..."

    set check_interval_ms   [time2ms $opt(check)]
    set max_waiting_time_ms [time2ms $opt(max)]
    set elapsed_ms 0
    
    while 1 {
        #
        after $check_interval_ms
        #
        ifnotexist waiting {
            print -nonewline "waiting .."
            set waiting 1
        }
        puts -nonewline "."
        flush stdout

        set done 1
        foreach out $outputFileList {
            if { ! [file exists $out ] || ! [job_done $out] } {
                set done 0
                break
            }
            # check if an error occured during the $out run and abort if so
            checkForError $out $out 1
        }
        if { $done } {
            puts ""
            # execute the 'script'
            set code [catch {uplevel 1 $script} result]
            return -code $code $result
            
        }

        set elapsed_ms [expr $elapsed_ms + $check_interval_ms]

        # prevent the loop going on indefinitely
        
        if { $elapsed_ms > $max_waiting_time_ms } {
            ::pwtk::abort "Maximum waiting time of $max_waiting_time_ms ms reached,
but the jobs associated with [join $outputFileList {, }] are not completed."
        }
    }
}