
#$VERBOSE = 3

# these variables wil cycle through the known database names

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

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
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
end

task pstamp.request.find
    host        local

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

    task.exec
        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   timeout
        showcommand timeout
    end
end

task pstamp.request.load
    host        local

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

    task.exec
        if ($DB:n == 0)
            option DEFAULT
            command pstamptool  -pendingreq
        else 
            option $DB:$pstampReq_DB
            command pstamptool  -pendingreq -dbname $DB:$pstampReq_DB
            $pstampReq_DB ++
            if ($pstampReq_DB >= $DB:n) set pstampReq_DB = 0
        end
    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   timeout
        showcommand timeout
    end
end

task pstamp.request.run
    periods     -poll $RUNPOLL
    periods     -exec $RUNEXEC
    periods     -timeout 30
    npending    1

    task.exec
        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

        $run = pstamp_parser_run.pl --req_id $REQ_ID

        add_standard_args run
        options $pageName

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


    task.exit default
#        echo pstamp.request.run task.exit status: $JOB_STATUS
        process_exit pstampRequest $options:0 $JOB_STATUS
    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
        if ($DB:n == 0)
            option DEFAULT
            command pstamptool  -completedreq
        else 
            option $DB:$pstampFin_DB
            command pstamptool  -completedreq -dbname $DB:$pstampFin_DB
            $pstampFin_DB ++
            if ($pstampFin_DB >= $DB:n) set pstampFin_DB = 0
        end
    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   timeout
        showcommand timeout
    end
end

task request.finish.run
    periods     -poll $RUNPOLL
    periods     -exec $RUNEXEC
    periods     -timeout 30
    npending    1

    task.exec
        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 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_name $REQ_NAME --product $PRODUCT

        add_standard_args run
        options $pageName

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

    task.exit default
        echo request.finish.run task.exit status: $JOB_STATUS
        process_exit pstampFinish $options:0 $JOB_STATUS
    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
        if ($DB:n == 0)
            option DEFAULT
            command pstamptool -pendingjob -limit 10
        else
            option $DB:$pstampJob_DB
            command pstamptool -pendingjob -limit 10 -dbname $DB:$pstampJob_DB
            $pstampJob_DB ++
            if ($pstampJob_DB >= $DB:n) set pstampJob_DB = 0
        end
    end

    task.exit $EXIT_SUCCESS
        # echo ipptool2book stdout pstampJob -key job_id -setword dbname $options:0 -setword pantaskState INIT
        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   default
        showcommand failure
    end

    task.exit   timeout
        showcommand timeout
    end
end

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

    task.exec
        book npages pstampJob -var N
        if ($N == 0) break
        
        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 dbname -var DBNAME

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


        $run = pstamp_job_run.pl --job_id $JOB_ID
        add_standard_args run

        options $pageName

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


    task.exit default
        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 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
