Changeset 4971


Ignore:
Timestamp:
Jan 30, 2015 12:21:28 PM (9 years ago)
Author:
mmc
Message:

Fixed auto-execution via TOOL_PARAMETERS to produce status output in a
file called rappture.status. This makes it easier for the web service
to monitor progress and know when everything is finished. Also, fixed
auto-execution to move the run file to the data/results directory and
clean up the original driver file.

Location:
branches/1.3
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/1.3/gui/apps/execute.tcl

    r4857 r4971  
    6767set toolobj [Rappture::library $toolxml]
    6868set TaskObj [Rappture::Task ::#auto $toolobj $installdir]
     69set LogFid ""
    6970
    70 # tasks in execute mode run quietly and don't try to save results
    71 $TaskObj configure -jobstats "" -resultdir ""
     71# Define some things that we need for logging status...
     72# ----------------------------------------------------------------------
     73proc log_output {message} {
     74    global LogFid
     75
     76    if {$LogFid ne ""} {
     77        #
     78        # Scan through and pick out any =RAPPTURE-PROGRESS=> messages.
     79        #
     80        set percent ""
     81        while {[regexp -indices \
     82                {=RAPPTURE-PROGRESS=> *([-+]?[0-9]+) +([^\n]*)(\n|$)} $message \
     83                 match percent mesg]} {
     84
     85            foreach {i0 i1} $percent break
     86            set percent [string range $message $i0 $i1]
     87
     88            foreach {i0 i1} $mesg break
     89            set mesg [string range $message $i0 $i1]
     90
     91            foreach {i0 i1} $match break
     92            set message [string replace $message $i0 $i1]
     93        }
     94        if {$percent ne ""} {
     95            # report the last percent progress found
     96            log_append progress "$percent% - $mesg"
     97        }
     98    }
     99}
     100
     101# Actually write to the log file
     102proc log_append {level message} {
     103    global LogFid
     104
     105    if {$LogFid ne ""} {
     106        set date [clock format [clock seconds] -format {%Y-%m-%dT%H:%M:%S%z}]
     107        set host [info hostname]
     108        puts $LogFid "$date $host rappture [pid] \[$level\] $message"
     109        flush $LogFid
     110    }
     111}
     112
     113# Actually write to the log file
     114proc log_stats {args} {
     115    set line ""
     116    foreach {key val} $args {
     117        append line "$key=$val "
     118    }
     119    log_append usage $line
     120}
     121
     122# Parse command line options to see
     123# ----------------------------------------------------------------------
     124Rappture::getopts argv params {
     125    value -status ""
     126    value -output ""
     127    value -cleanup no
     128}
     129
     130if {$params(-status) ne ""} {
     131    set LogFid [open $params(-status) w]
     132    $TaskObj configure -logger {log_append status} -jobstats log_stats
     133}
     134
     135if {$params(-output) eq ""} {
     136    # no output? then run quietly and don't try to save results
     137    $TaskObj configure -jobstats "" -resultdir ""
     138}
    72139
    73140# Transfer input values from driver to TaskObj, and then run.
     
    80147        lappend args $path [$driverobj get $path.current]
    81148    }
     149}
     150
     151if {$params(-status) ne ""} {
     152    # recording status? then look through output for progress messages
     153    lappend args -output log_output
    82154}
    83155
     
    95167    $runxml put output.status failed
    96168}
    97 $runxml put output.time [clock format [clock seconds]]
    98169
    99 $runxml put tool.version.rappture.version $::Rappture::version
    100 $runxml put tool.version.rappture.revision $::Rappture::build
    101 
    102 if {[info exists tcl_platform(user)]} {
    103     $runxml put output.user $::tcl_platform(user)
     170# Handle output
     171# ----------------------------------------------------------------------
     172switch -- $params(-output) {
     173    "" {
     174        # no output file -- write to stdout
     175        puts "<?xml version=\"1.0\"?>\n[$runxml xml]"
     176    }
     177    "@default" {
     178        # do the usual Rappture thing -- move to results dir
     179        # but ignore any errors if it fails
     180        catch {$TaskObj save $runxml}
     181    }
     182    default {
     183        # save to the specified file
     184        $TaskObj save $runxml $params(-output)
     185    }
    104186}
    105187
    106 puts "<?xml version=\"1.0\"?>\n[$runxml xml]"
     188if {$params(-cleanup)} {
     189    file delete -force -- $driverxml
     190}
     191
     192log_append status "exit $status"
    107193exit $status
  • branches/1.3/gui/apps/launcher.tcl

    r4962 r4971  
    213213            set mainscript [file join $dir execute.tcl]
    214214            set reqpkgs ""
     215
     216            # When executing from TOOL_PARAMETERS file directives,
     217            # report status, clean up, and save output to data/results.
     218            # This helps the web services interface do its thing.
     219            set alist [list \
     220                -output @default \
     221                -status rappture.status \
     222                -cleanup yes]
    215223        }
    216224        "" - "-input" {
  • branches/1.3/gui/scripts/tool.tcl

    r4531 r4971  
    3131    public method run {args} {
    3232        sync  ;# sync all widget values to XML
    33         eval $_task run $args
     33
     34        foreach {status result} [eval $_task run $args] break
     35        if {$status == 0} {
     36            # move good results to the data/results directory
     37            $_task save $result
     38        }
     39
     40        return [list $status $result]
    3441    }
    3542    public method abort {} {
  • branches/1.3/lang/tcl/scripts/task.tcl

    r4514 r4971  
    3232    public method reset {}
    3333    public method xml {args}
     34    public method save {xmlobj {name ""}}
    3435
    3536    protected method _mkdir {dir}
     
    3940    private variable _xmlobj ""      ;# XML object with inputs/outputs
    4041    private variable _origxml ""     ;# copy of original XML (for reset)
     42    private variable _lastrun ""     ;# name of last run file
    4143    private variable _installdir ""  ;# installation directory for this tool
    4244    private variable _outputcb ""    ;# callback for tool output
     
    215217
    216218        # starting job...
     219        set _lastrun ""
    217220        _log run started
    218221        Rappture::rusage mark
     
    353356        set result [string trim $job(output)]
    354357        if {[regexp {=RAPPTURE-RUN=>([^\n]+)} $result match file]} {
     358            set _lastrun $file
     359
    355360            set status [catch {Rappture::library $file} result]
    356361            if {$status == 0} {
     
    366371            }
    367372
    368             # if there's a results_directory defined in the resources
    369             # file, then move the run.xml file there for storage
    370             set rdir ""
    371             if {$resultdir eq "@default"} {
    372                 if {[info exists _resources(-resultdir)]} {
    373                     set rdir $_resources(-resultdir)
    374                 } else {
    375                     set rdir "."
    376                 }
    377             } elseif {$resultdir ne ""} {
    378                 set rdir $resultdir
    379             }
    380 
    381             if {$status == 0 && $rdir ne ""} {
    382                 catch {
    383                     file delete -force -- $file
    384                     if {![file exists $rdir]} {
    385                         _mkdir $rdir
    386                     }
    387                     set tail [file tail $file]
    388                     set fid [open [file join $rdir $tail] w]
    389                     puts $fid "<?xml version=\"1.0\"?>"
    390                     puts $fid [$result xml]
    391                     close $fid
    392                 }
    393             } else {
    394                 # don't keep the file
    395                 file delete -force -- $file
    396             }
     373            file delete -force -- $file
    397374        } else {
    398375            set status 1
     
    475452
    476453# ----------------------------------------------------------------------
     454# USAGE: save <xmlobj> ?<filename>?
     455#
     456# Used by clients to save the contents of an <xmlobj> representing
     457# a run out to the given file.  If <filename> is not specified, then
     458# it uses the -resultsdir and other settings to do what Rappture
     459# would normally do with the output.
     460# ----------------------------------------------------------------------
     461itcl::body Rappture::Task::save {xmlobj {filename ""}} {
     462    if {$filename eq ""} {
     463        # if there's a results_directory defined in the resources
     464        # file, then move the run.xml file there for storage
     465        set rdir ""
     466        if {$resultdir eq "@default"} {
     467            if {[info exists _resources(-resultdir)]} {
     468                set rdir $_resources(-resultdir)
     469            } else {
     470                set rdir "."
     471            }
     472        } elseif {$resultdir ne ""} {
     473            set rdir $resultdir
     474        }
     475
     476        # use the runfile name generated by the last run
     477        if {$_lastrun ne ""} {
     478            set filename [file join $rdir $_lastrun]
     479        } else {
     480            set filename [file join $rdir run.xml]
     481        }
     482    }
     483
     484    # add any last-minute metadata
     485    $xmlobj put output.time [clock format [clock seconds]]
     486
     487    $xmlobj put tool.version.rappture.version $::Rappture::version
     488    $xmlobj put tool.version.rappture.revision $::Rappture::build
     489
     490    if {[info exists ::tcl_platform(user)]} {
     491        $xmlobj put output.user $::tcl_platform(user)
     492    }
     493
     494    # save the output
     495    set rdir [file dirname $filename]
     496    if {![file exists $rdir]} {
     497        _mkdir $rdir
     498    }
     499
     500    set fid [open $filename w]
     501    puts $fid "<?xml version=\"1.0\"?>"
     502    puts $fid [$xmlobj xml]
     503    close $fid
     504
     505    _log output saved in $filename
     506}
     507
     508# ----------------------------------------------------------------------
    477509# USAGE: _output <data>
    478510#
Note: See TracChangeset for help on using the changeset viewer.