# pstamp.pro : Postage Stamp Server tasks

check.globals

$LOGSUBDIR = $LOGDIR/pstamp
mkdir $LOGSUBDIR

# these variables wil cycle through the known database names

$pstampDS_DB = 0
$pstampReq_DB = 0
$pstampJob_DB = 0
$pstampFin_DB = 0
$pstampRev_DB = 0

# set PS_DBSERVER if postage stamp database host is not the same as the value for DBSERVER in site.config
# warning: no quotes around the two words. That causes the variable to get passed to pstamptool as one word
# and a tricky to debug problem ensues
# example:
# $PS_DBSERVER = -dbserver hostname

if ($?PS_DBSERVER == 0)
    $PS_DBSERVER = ""
end

macro pstamp.reset
    book init pstampRequest
    book init pstampJob
    book init pstampFinish
end

pstamp.reset


macro pstamp.on
    task pstamp.request.find
        active true
    end
    task pstamp.request.load
        active true
    end
    task pstamp.request.run
        active true
    end

    task request.finish.load
        active true
    end
    task request.finish.run
        active true
    end

    task pstamp.job.load
        active true
    end
    task pstamp.job.run
        active true
    end
    task pstamp.job.revert
        active true
    end
end


macro pstamp.off
    task pstamp.request.find
        active false
    end
    task pstamp.request.load
        active false
    end
    task pstamp.request.run
        active false
    end
    task request.finish.load
        active false
    end
    task request.finish.run
        active false
    end

    task pstamp.job.load
        active false
    end
    task pstamp.job.run
        active false
    end
    task pstamp.job.revert
        active false
    end
end

macro pstamp.revert.on
    task pstamp.job.revert
        active true
    end
end
macro pstamp.revert.off
    task pstamp.job.revert
        active false
    end
end

task pstamp.request.find
    host        local

    periods     -poll $LOADPOLL
    periods     -exec 10
    periods     -timeout 120
    npending    1

    task.exec
        stdout NULL
        stderr $LOGSUBDIR/pstamp.request.find.log
        if ($DB:n == 0)
            option DEFAULT
            command pstamp_queue_requests.pl --limit 5
        else 
            option $DB:$pstampDS_DB
            command pstamp_queue_requests.pl --limit 5 --dbname $DB:$pstampDS_DB --verbose
            $pstampDS_DB ++
            if ($pstampDS_DB >= $DB:n) set pstampDS_DB = 0
        end
    end

    task.exit $EXIT_SUCCESS
        # echo nothing to do
    end

    task.exit   default
        showcommand failure
    end

    task.exit   crash
        showcommand crash
    end

    task.exit   timeout
        showcommand timeout
    end
end

task pstamp.request.load
    host        local

    periods     -poll $LOADPOLL
    periods     -exec $LOADEXEC
    periods     -timeout 300
    npending    1

    task.exec
        stdout $LOGSUBDIR/pstamp.request.load.log
        stderr $LOGSUBDIR/pstamp.request.load.log
        $run = pstamptool -pendingreq
        if ($DB:n == 0)
            option DEFAULT
        else 
            option $DB:$pstampReq_DB
            $run = $run $PS_DBSERVER -dbname $DB:$pstampReq_DB
            $pstampReq_DB ++
            if ($pstampReq_DB >= $DB:n) set pstampReq_DB = 0
        end
        add_poll_args run
        add_poll_labels run
        command $run
    end

    task.exit $EXIT_SUCCESS
        ipptool2book stdout pstampRequest -key req_id -uniq -setword dbname $options:0 -setword pantaskState INIT
        if ($VERBOSE > 2)
            echo starting request
            book listbook pstampRequest
        end

        process_cleanup pstampRequest
    end

    task.exit   default
        showcommand failure
    end

    task.exit   crash
        showcommand crash
    end

    task.exit   timeout
        showcommand timeout
    end
end

task pstamp.request.run
    periods     -poll $RUNPOLL
    periods     -exec $RUNEXEC
    periods     -timeout 300

    task.exec
        stdout $LOGSUBDIR/pstamp.request.run.log
        stderr $LOGSUBDIR/pstamp.request.run.log
        book npages pstampRequest -var N
        if ($N == 0) break
        
        book getpage pstampRequest 0 -var pageName -key pantaskState INIT
        if ("$pageName" == "NULL") break

        book setword pstampRequest $pageName pantaskState RUN
        book getword pstampRequest $pageName req_id -var REQ_ID
        book getword pstampRequest $pageName dbname -var DBNAME
        book getword pstampRequest $pageName uri -var URI
        book getword pstampRequest $pageName ds_outProduct -var PRODUCT

        host anyhost

        $run = pstamp_parser_run.pl --req_id $REQ_ID --uri $URI --product $PRODUCT --redirect-output

        add_standard_args run
        options $pageName

        if ($VERBOSE > 1) 
            echo command $run
        end
        command $run
    end


    task.exit $EXIT_SUCCESS
        process_exit pstampRequest $options:0 $JOB_STATUS
    end

    task.exit default
        showcommand failure
        process_exit pstampRequest $options:0 $JOB_STATUS
    end

    task.exit crash
        showcommand crash
        book setword pstampRequest $options:0 pantaskState CRASH
    end

    task.exit timeout
        showcommand timeout
        book setword pstampRequest $options:0 pantaskState TIMEOUT
    end
end

task request.finish.load
    host        local

    periods     -poll $LOADPOLL
    periods     -exec $LOADEXEC
    periods     -timeout 300
    npending    1

    task.exec
        stdout $LOGSUBDIR/pstamp.finish.load.log
        stderr $LOGSUBDIR/pstamp.finish.load.log
        $run = pstamptool  -completedreq
        if ($DB:n == 0)
            option DEFAULT
        else 
            option $DB:$pstampFin_DB
            $run = $run -dbname $DB:$pstampFin_DB $PS_DBSERVER
            $pstampFin_DB ++
            if ($pstampFin_DB >= $DB:n) set pstampFin_DB = 0
        end
        add_poll_args run
        add_poll_labels run
        command $run
    end

    task.exit $EXIT_SUCCESS
        ipptool2book stdout pstampFinish -key req_id -uniq -setword dbname $options:0 -setword pantaskState INIT
        if ($VERBOSE > 2)
            book listbook pstampFinish
        end

        process_cleanup pstampFinish
    end

    task.exit   default
        showcommand failure
    end

    task.exit   crash
        showcommand crash
    end

    task.exit   timeout
        showcommand timeout
    end
end

task request.finish.run
    periods     -poll $RUNPOLL
    periods     -exec $RUNEXEC
    periods     -timeout 2400
    host        anyhost

    task.exec
        stdout $LOGSUBDIR/request.finish.run.log
        stderr $LOGSUBDIR/request.finish.run.log
        book npages pstampFinish -var N
        if ($N == 0) break
        
        book getpage pstampFinish 0 -var pageName -key pantaskState INIT
        if ("$pageName" == "NULL") break

        book setword pstampFinish $pageName pantaskState RUN
        book getword pstampFinish $pageName req_id -var REQ_ID
        book getword pstampFinish $pageName uri -var URI
        book getword pstampFinish $pageName dbname -var DBNAME
        book getword pstampFinish $pageName reqType -var REQ_TYPE
        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

        add_standard_args run
        options $pageName

        if ($VERBOSE > 1) 
            echo command $run
        end
        command $run
    end

    task.exit $EXIT_SUCCESS
        process_exit pstampFinish $options:0 $JOB_STATUS
    end

    task.exit default
        # echo request.finish.run task.exit status: $JOB_STATUS
        process_exit pstampFinish $options:0 $JOB_STATUS
        showcommand failure
    end

    task.exit crash
        showcommand crash
        book setword pstampFinish $options:0 pantaskState CRASH.run
    end

    task.exit timeout
        showcommand timeout
        book setword pstampFinish $options:0 pantaskState TIMEOUT.run
    end
end

task pstamp.job.load
    host        local

    periods     -poll $LOADPOLL
    periods     -exec $LOADEXEC
    periods     -timeout 30
    npending    1

    task.exec
        stdout $LOGSUBDIR/pstamp.job.load.log
        stderr $LOGSUBDIR/pstamp.job.load.log
        $run = pstamptool -pendingjob
        if ($DB:n == 0)
            option DEFAULT
        else
            option $DB:$pstampJob_DB
            $run = $run -dbname $DB:$pstampJob_DB $PS_DBSERVER
            $pstampJob_DB ++
            if ($pstampJob_DB >= $DB:n) set pstampJob_DB = 0
        end
        add_poll_args run
        add_poll_labels run 
        command $run
    end

    task.exit $EXIT_SUCCESS
        ipptool2book stdout pstampJob -key job_id -uniq -setword dbname $options:0 -setword pantaskState INIT

        book npages pstampJob -var N
        if ($VERBOSE > 2)
            book listbook pstampJob
        end

        # delete existing entries in the appropriate pantaskStates
        process_cleanup pstampJob
    end

    task.exit   crash
        showcommand crash
    end

    task.exit   timeout
        showcommand timeout
    end

    task.exit   default
        showcommand failure
    end

end

task pstamp.job.run
    periods     -poll $RUNPOLL
    periods     -exec $RUNEXEC
    periods     -timeout 1200

    task.exec
        stdout $LOGSUBDIR/pstamp.job.run.log
        stderr $LOGSUBDIR/pstamp.job.run.log
        book npages pstampJob -var N
        if ($N == 0) 
            periods -exec $RUNEXEC
            break
        end
        periods -exec 0.05
        
        book getpage pstampJob 0 -var pageName -key pantaskState INIT
        if ("$pageName" == "NULL") break

        #echo pageName: $pageName

        book setword pstampJob $pageName pantaskState RUN
        book getword pstampJob $pageName job_id -var JOB_ID
        book getword pstampJob $pageName jobType -var JOB_TYPE
        book getword pstampJob $pageName rownum -var ROWNUM
        book getword pstampJob $pageName dbname -var DBNAME
        book getword pstampJob $pageName outputBase -var OUTPUT_BASE
        book getword pstampJob $pageName options -var OPTIONS

        if ($VERBOSE > 1) 
            book listpage pstampJob $pageName
        end

        host anyhost

        $run = pstamp_job_run.pl --job_id $JOB_ID --job_type $JOB_TYPE --rownum $ROWNUM --output_base $OUTPUT_BASE --options $OPTIONS --redirect-output 
        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.job.run task.exit $JOB_ID status: $JOB_STATUS
        end
        process_exit pstampJob $options:0 $JOB_STATUS
    end
    task.exit default
        if ($VERBOSE > 1)
            echo pstamp.job.run task.exit $JOB_ID status: $JOB_STATUS
        end
        showcommand failure
        process_exit pstampJob $options:0 $JOB_STATUS
    end

    task.exit crash
        echo pstamp.job.run task.crash $JOB_ID status: $JOB_STATUS
        process_exit pstampJob $options:0 $JOB_STATUS
        showcommand crash
        book setword pstampJob $options:0 pantaskState CRASH
    end

    task.exit timeout
        echo pstamp.job.run task.timeout $JOB_ID status: $JOB_STATUS
        process_exit pstampJob $options:0 $JOB_STATUS
        showcommand timeout
        book setword pstampJob $options:0 pantaskState TIMEOUT
    end
end

task pstamp.job.revert
    host        local

    periods     -poll $LOADPOLL
    periods     -exec 300
    periods     -timeout 300
    npending    1

    task.exec
        stdout $LOGSUBDIR/pstamp.job.revert.log
        stderr $LOGSUBDIR/pstamp.job.revert.log
        $run = pstamptool -revertjob -all
        if ($DB:n == 0)
            option DEFAULT
        else 
            $run = $run $PS_DBSERVER -dbname $DB:$pstampRev_DB
            $pstampRev_DB ++
            if ($pstampRev_DB >= $DB:n) set pstampRev_DB = 0
        end
        add_poll_args run
        add_poll_labels run
        command $run
    end

    task.exit $EXIT_SUCCESS
        # nothing to do
    end

    task.exit   default
        showcommand failure
    end

    task.exit   crash
        showcommand crash
    end

    task.exit   timeout
        showcommand timeout
    end
end

