Index: branches/pap/ippTasks/pstamp.pro
===================================================================
--- branches/pap/ippTasks/pstamp.pro	(revision 27708)
+++ branches/pap/ippTasks/pstamp.pro	(revision 28003)
@@ -13,4 +13,6 @@
 $pstampFin_DB = 0
 $pstampRev_DB = 0
+$pstampDep_DB = 0
+$pstampCleanup_DB = 0
 
 # set PS_DBSERVER if postage stamp database host is not the same as the value for DBSERVER in site.config
@@ -28,8 +30,9 @@
     book init pstampJob
     book init pstampFinish
+    book init pstampDependent
+    book init pstampCleanup
 end
 
 pstamp.reset
-
 
 macro pstamp.on
@@ -57,5 +60,5 @@
         active true
     end
-    task pstamp.job.revert
+    task pstamp.dependent.load
         active true
     end
@@ -89,4 +92,10 @@
         active false
     end
+    task pstamp.dependent.load
+        active false
+    end
+    task pstamp.dependent.run
+        active false
+    end
 end
 
@@ -98,4 +107,20 @@
 macro pstamp.revert.off
     task pstamp.job.revert
+        active false
+    end
+end
+macro pstamp.cleanup.on
+    task pstamp.cleanup.load
+        active true
+    end
+    task pstamp.cleanup.run
+        active true
+    end
+end
+macro pstamp.cleanup.off
+    task pstamp.cleanup.run
+        active false
+    end
+    task pstamp.cleanup.run
         active false
     end
@@ -150,5 +175,5 @@
 
     task.exec
-        stdout $LOGSUBDIR/pstamp.request.load.log
+        stdout NULL
         stderr $LOGSUBDIR/pstamp.request.load.log
         $run = pstamptool -pendingreq
@@ -195,5 +220,5 @@
 
     task.exec
-        stdout $LOGSUBDIR/pstamp.request.run.log
+        stdout NULL
         stderr $LOGSUBDIR/pstamp.request.run.log
         book npages pstampRequest -var N
@@ -208,8 +233,10 @@
         book getword pstampRequest $pageName uri -var URI
         book getword pstampRequest $pageName ds_outProduct -var PRODUCT
+        book getword pstampRequest $pageName outdir -var OUTDIR
+        book getword pstampRequest $pageName label -var LABEL
 
         host anyhost
 
-        $run = pstamp_parser_run.pl --req_id $REQ_ID --uri $URI --product $PRODUCT --redirect-output
+        $run = pstamp_parser_run.pl --req_id $REQ_ID --uri $URI --product $PRODUCT --outdir $OUTDIR --label $LABEL --redirect-output
 
         add_standard_args run
@@ -252,5 +279,5 @@
 
     task.exec
-        stdout $LOGSUBDIR/pstamp.finish.load.log
+        stdout NULL
         stderr $LOGSUBDIR/pstamp.finish.load.log
         $run = pstamptool  -completedreq
@@ -297,5 +324,5 @@
 
     task.exec
-        stdout $LOGSUBDIR/request.finish.run.log
+        stdout NULL
         stderr $LOGSUBDIR/request.finish.run.log
         book npages pstampFinish -var N
@@ -312,6 +339,7 @@
         book getword pstampFinish $pageName name -var REQ_NAME
         book getword pstampFinish $pageName outProduct -var PRODUCT
-
-        $run = request_finish.pl --req_id $REQ_ID --req_type $REQ_TYPE --req_file $URI --req_name $REQ_NAME --product $PRODUCT --redirect-output
+        book getword pstampFinish $pageName outdir -var OUTDIR
+
+        $run = request_finish.pl --req_id $REQ_ID --req_type $REQ_TYPE --req_file $URI --req_name $REQ_NAME --product $PRODUCT --outdir $OUTDIR --redirect-output
 
         add_standard_args run
@@ -354,5 +382,5 @@
 
     task.exec
-        stdout $LOGSUBDIR/pstamp.job.load.log
+        stdout NULL
         stderr $LOGSUBDIR/pstamp.job.load.log
         $run = pstamptool -pendingjob
@@ -402,5 +430,5 @@
 
     task.exec
-        stdout $LOGSUBDIR/pstamp.job.run.log
+        stdout NULL
         stderr $LOGSUBDIR/pstamp.job.run.log
         book npages pstampJob -var N
@@ -480,5 +508,5 @@
 
     task.exec
-        stdout $LOGSUBDIR/pstamp.job.revert.log
+        stdout NULL
         stderr $LOGSUBDIR/pstamp.job.revert.log
         $run = pstamptool -revertjob -all
@@ -512,2 +540,257 @@
 end
 
+task pstamp.dependent.load
+    host        local
+
+    periods     -poll $LOADPOLL
+    # XXX: create a macro for this time
+    periods     -exec 10
+#    periods     -exec $LOADEXEC
+    periods     -timeout 300
+    npending    1
+
+    task.exec
+        stdout NULL
+        stderr $LOGSUBDIR/pstamp.dependent.load.log
+        $run = pstamptool -pendingdependent
+        if ($DB:n == 0)
+            option DEFAULT
+        else
+            option $DB:$pstampDep_DB
+            $run = $run -dbname $DB:$pstampDep_DB $PS_DBSERVER
+            $pstampDep_DB ++
+            if ($pstampDep_DB >= $DB:n) set pstampDep_DB = 0
+        end
+        add_poll_args run
+        add_poll_labels run 
+        command $run
+    end
+
+    task.exit $EXIT_SUCCESS
+        ipptool2book stdout pstampDependent -key dep_id -uniq -setword dbname $options:0 -setword pantaskState INIT
+
+        book npages pstampDependent -var N
+        if ($VERBOSE > 2)
+            book listbook pstampDependent
+        end
+
+        # delete existing entries in the appropriate pantaskStates
+        process_cleanup pstampDependent
+    end
+
+    task.exit   crash
+        showcommand crash
+    end
+
+    task.exit   timeout
+        showcommand timeout
+    end
+
+    task.exit   default
+        showcommand failure
+    end
+
+end
+
+task pstamp.dependent.run
+    periods     -poll $RUNPOLL
+    periods     -exec $RUNEXEC
+    periods     -timeout 300
+
+    task.exec
+        book npages pstampDependent -var N
+        if ($N == 0) 
+            periods -exec $RUNEXEC
+            break
+        end
+        periods -exec 0.05
+        
+        book getpage pstampDependent 0 -var pageName -key pantaskState INIT
+        if ("$pageName" == "NULL") break
+
+        book setword pstampDependent $pageName pantaskState RUN
+        book getword pstampDependent $pageName dep_id     -var DEP_ID
+        book getword pstampDependent $pageName state      -var STATE
+        book getword pstampDependent $pageName stage      -var STAGE
+        book getword pstampDependent $pageName stage_id   -var STAGE_ID
+        book getword pstampDependent $pageName component  -var COMPONENT
+        book getword pstampDependent $pageName imagedb    -var IMAGEDB
+        book getword pstampDependent $pageName rlabel     -var RLABEL
+        book getword pstampDependent $pageName outdir     -var OUTDIR
+        book getword pstampDependent $pageName need_magic -var NEED_MAGIC
+        book getword pstampDependent $pageName dbname     -var DBNAME
+
+        if ($VERBOSE > 1) 
+            book listpage pstampDependent $pageName
+        end
+
+        host anyhost
+
+        if ("$NEED_MAGIC" == "T")
+            $NEED_MAGIC="--need_magic"
+        else
+            $NEED_MAGIC=""
+        end
+
+        $MYLOGFILE = $OUTDIR/checkdep.$DEP_ID.log
+        stdout $MYLOGFILE
+        stderr $MYLOGFILE
+
+        $run = pstamp_checkdependent.pl --dep_id $DEP_ID --stage_id $STAGE_ID --stage $STAGE --component $COMPONENT --imagedb $IMAGEDB --rlabel $RLABEL $NEED_MAGIC
+
+        add_standard_args run
+
+        options $pageName
+
+        if ($VERBOSE > 1) 
+            echo command $run
+        end
+        command $run
+    end
+
+
+    task.exit $EXIT_SUCCESS
+        if ($VERBOSE > 1)
+            echo pstamp.dependent.run task.exit $DEP_ID status: $JOB_STATUS
+        end
+        process_exit pstampDependent $options:0 $JOB_STATUS
+    end
+    task.exit default
+        if ($VERBOSE > 1)
+            echo pstamp.job.run task.exit $DEP_ID status: $JOB_STATUS
+        end
+        showcommand failure
+        process_exit pstampDependent $options:0 $JOB_STATUS
+    end
+
+    task.exit crash
+        echo pstamp.job.run task.crash $DEP_ID status: $JOB_STATUS
+        process_exit pstampDependent $options:0 $JOB_STATUS
+        showcommand crash
+        book setword pstampDependent $options:0 pantaskState CRASH
+    end
+
+    task.exit timeout
+        echo pstamp.job.run task.timeout $DEP_ID status: $JOB_STATUS
+        process_exit pstampDependent $options:0 $JOB_STATUS
+        showcommand timeout
+        book setword pstampDependent $options:0 pantaskState TIMEOUT
+    end
+end
+
+task pstamp.cleanup.load
+    host        local
+
+    periods     -poll $LOADPOLL
+    periods     -exec $LOADEXEC
+    periods     -timeout 300
+    npending    1
+
+    task.exec
+        stdout NULL
+        stderr $LOGSUBDIR/pstamp.cleanup.load.log
+        $run = pstamptool -pendingcleanup
+        if ($DB:n == 0)
+            option DEFAULT
+        else 
+            option $DB:$pstampCleanup_DB
+            $run = $run $PS_DBSERVER -dbname $DB:$pstampCleanup_DB
+            $pstampCleanup_DB ++
+            if ($pstampCleanup_DB >= $DB:n) set pstampCleanup_DB = 0
+        end
+        add_poll_args run
+        add_poll_labels run
+        command $run
+    end
+
+    task.exit $EXIT_SUCCESS
+        ipptool2book stdout pstampCleanup -key req_id -uniq -setword dbname $options:0 -setword pantaskState INIT
+        if ($VERBOSE > 2)
+            echo starting request
+            book listbook pstampCleanup
+        end
+
+        process_cleanup pstampCleanup
+    end
+
+    task.exit   default
+        showcommand failure
+    end
+
+    task.exit   crash
+        showcommand crash
+    end
+
+    task.exit   timeout
+        showcommand timeout
+    end
+end
+
+task pstamp.cleanup.run
+    periods     -poll $RUNPOLL
+    periods     -exec $RUNEXEC
+    periods     -timeout 300
+    # since everything is on one file system keep npending low to avoid
+    # overloading nfs
+
+    npending    10
+
+    task.exec
+        book npages pstampCleanup -var N
+        if ($N == 0) 
+            periods -exec $RUNEXEC
+            break
+        end
+        periods -exec 0.05
+        
+        book getpage pstampCleanup 0 -var pageName -key pantaskState INIT
+        if ("$pageName" == "NULL") break
+
+        book setword pstampCleanup $pageName pantaskState RUN
+        book getword pstampCleanup $pageName req_id -var REQ_ID
+        book getword pstampCleanup $pageName dbname -var DBNAME
+        book getword pstampCleanup $pageName name -var NAME
+        book getword pstampCleanup $pageName outdir -var OUTDIR
+        book getword pstampCleanup $pageName uri -var URI
+        book getword pstampCleanup $pageName reqType -var REQTYPE
+        book getword pstampCleanup $pageName outProduct -var PRODUCT
+
+        # XXX: have the script set this up this
+        $MYLOGFILE=/data/ippdb02.0/pstamp/work/logs/cleanup.$REQ_ID
+        stdout $MYLOGFILE
+        stderr $MYLOGFILE
+
+        host anyhost
+
+        $run = pstamp_cleanup.pl --req_id $REQ_ID --uri $URI --product $PRODUCT --name $NAME --outdir $OUTDIR --reqType $REQTYPE
+
+        add_standard_args run
+        options $pageName
+
+        if ($VERBOSE > 1) 
+            echo command $run
+        end
+        command $run
+    end
+
+
+    task.exit $EXIT_SUCCESS
+        process_exit pstampCleanup $options:0 $JOB_STATUS
+    end
+
+    task.exit default
+        showcommand failure
+        process_exit pstampCleanup $options:0 $JOB_STATUS
+    end
+
+    task.exit crash
+        showcommand crash
+        book setword pstampCleanup $options:0 pantaskState CRASH
+    end
+
+    task.exit timeout
+        showcommand timeout
+        book setword pstampCleanup $options:0 pantaskState TIMEOUT
+    end
+end
+
