#!/bin/csh -f
#*****************************************************************************
#                                                                            *
#            Copyright (C) 1993, Alias Research, Inc.                        *
#                                                                            *
#    These coded instructions,  statements and  computer programs contain    *
#    unpublished information proprietary to Alias Research, Inc.  and are    *
#    protected by the Canadian and US Federal copyright law. They may not    *
#    be disclosed to third parties  or copied  or duplicated, in whole or    *
#    in part,  without the prior written consent of Alias Research,  Inc.    *
#                                                                            *
# Unpublished-rights reserved under the Copyright Laws of the United States. *
#                                                                            *
#****************************************************************************/
#
#       $Source: /tmp_mnt/repo/root/v/alias3.2.2/src/Gifts/bin/RCS/drender_check.csh.sgi,v $
#       $Author: psantan $ $Date: 93/06/17 11:18:30 $
#
#
#
# Shell app that wakes up occasionally, and checks
# for rendering jobs from the input queue directory
#
# This code assumes the script that calls it has defined and exported the
# environment variable NT_MASTER_DIR. In practice, the shell that calls it
# was generated by drender_install. Drender_install makes sure this happens.
#
onintr -
nohup

set NT_QUEUE_DIR=${NT_MASTER_DIR}/queue

umask 000

# echo "Queue Dir=${NT_QUEUE_DIR}"
source ${NT_MASTER_DIR}/config/`hostname`

set NT_TEMP_1=/usr/tmp/$$.t1
set NT_TEMP_2=/usr/tmp/$$.t2
set NT_ERRORS=/usr/tmp/$$.t3
#
# Check for existance of ANY queued files
#
/bin/ls ${NT_QUEUE_DIR} > ${NT_TEMP_1}

if ( -s ${NT_TEMP_1} ) then

    #
    # for every lock file in the queue directory...
    #
    /bin/ls ${NT_QUEUE_DIR} > ${NT_TEMP_2}
    foreach file ( `grep '.lock' ${NT_TEMP_2}` )

        set NT_LOCKFILE=${NT_QUEUE_DIR}/$file
        
        #
        # Try to remove the lock!
        #
        ${NT_BIN_DIR}/NTunlink  ${NT_LOCKFILE};set NTrmstat=${status}

        #
        # If we suceeded, we own the file ('cause only one person
        # can delete a file! (even over NFS!))
        #
        if ( "GAMMA${NTrmstat}" == "GAMMA0" ) then

            #
            # Now we need to check that we can actually process this
            # file! (Bummer we couldn't have done this before...)
            #
            set NT_FILE=`basename ${NT_LOCKFILE} .lock`

            #
            # Read the properties out of the properties file
            #
            source ${NT_QUEUE_DIR}/${NT_FILE}.prop

            foreach NT_REND_PROP ( ${RENDER_PROPERTIES} )

                set NT_FOUND=FALSE
                foreach NT_MACH_PROP ( ${MACHINE_PROPERTIES} )

                    if ( ${NT_REND_PROP} == ${NT_MACH_PROP} ) then

                        set NT_FOUND=TRUE
                        break
                    endif
                end

                #
                # Check to see if we met this rendering criteria
                #
                if ( ${NT_FOUND} == "FALSE" ) then

                    break;
                endif
            end

            if ( ${NT_FOUND} == "FALSE" ) then
                #
                # We couldn't handle this job, so give up control of it
                # by creating a new lock file
                #
                echo "`date '+%D %T' `: Job ${NT_FILE} is unfullfillable by this node"

                #
                # Re-create the lock file to free it for somebody else
                #
                echo "" > ${NT_LOCKFILE}
            else
                #
                # We can handle the job, so render it, then terminate.
                #
                echo "`date '+%D %T' `: Job ${NT_FILE} will be executed"

                #
                # Create an entry in the running directory. This is only used
                # as an indicator that the job is running on this machine.
                #
                echo "`date`" > ${NT_MASTER_DIR}/running/${NT_FILE}.job_on_`hostname`

                (sh ${NT_QUEUE_DIR}/${NT_FILE}.job > /dev/null) >& ${NT_ERRORS}

                set NT_STAT=${status}

                #
                # The job is finished, so remove the running indicator.
                #
                /bin/rm -f ${NT_MASTER_DIR}/running/${NT_FILE}.job_on_`hostname`

                #
                # Echo any errors to STDOUT. STDOUT has been redirected to the log file
                #
                cat ${NT_ERRORS}
                /bin/rm ${NT_ERRORS}

                if ( "${NT_STAT}" == "0" ) then

                    #
                    # Here the job processed correctly
                    #
                    mv ${NT_QUEUE_DIR}/${NT_FILE}.job ${NT_MASTER_DIR}/completed
                    mv ${NT_QUEUE_DIR}/${NT_FILE}.prop ${NT_MASTER_DIR}/completed
                    if ( -e ${NT_QUEUE_DIR}/${NT_FILE}.post ) then
                        mv ${NT_QUEUE_DIR}/${NT_FILE}.post ${NT_MASTER_DIR}/completed
                    endif
                    echo "" >  ${NT_MASTER_DIR}/completed/${NT_FILE}.lock
                    echo "`date '+%D %T' `: Job ${NT_FILE} completed sucessfully"
                else

                    #
                    # Here the failed
                    #
                    if ( "${NT_RETRY}" == "1" ) then
                        #
                        # If the job failed, then we create a new lock file to
                        # resubmit it if retry is true.
                        #
                        echo "" > ${NT_QUEUE_DIR}/${NT_FILE}.lock
                        echo "`date '+%D %T' `: Job ${NT_FILE} failed, exit code=${NT_STAT}, retrying"
                    else
                        #
                        # Retry was false, so just fail it.
                        #
                        mv ${NT_QUEUE_DIR}/${NT_FILE}.job ${NT_MASTER_DIR}/failed
                        mv ${NT_QUEUE_DIR}/${NT_FILE}.prop ${NT_MASTER_DIR}/failed
                        if ( -e ${NT_QUEUE_DIR}/${NT_FILE}.post ) then
                            mv ${NT_QUEUE_DIR}/${NT_FILE}.post ${NT_MASTER_DIR}/failed
                        endif
                        echo "" > ${NT_MASTER_DIR}/failed/${NT_FILE}.lock
                        echo "`date '+%D %T' `: Job ${NT_FILE} failed, exit code=${NT_STAT}, failing"
                    endif
                endif
                break;       
            endif
        endif
    end
endif

if (-e ${NT_TEMP_1} ) then
    /bin/rm -f ${NT_TEMP_1}
endif

if (-e ${NT_TEMP_2} ) then
    /bin/rm -f ${NT_TEMP_2}
endif


