Clarity

 View Only
  • 1.  How do I check for file existence using

    Posted Jul 07, 2014 10:38 PM

    Hi All,

     

    I'm having trouble getting the <util:available> tag returning me a 'true' condition when a file is being found (which is actually the ability to execute a set of commands within the utl tag, from what I can gather).

     

    What I'm trying to do is poll a fileshare directory for the existence of a file, and when the file existence is detected, exit the polling loop (and carry on with the rest of the GEL).

     

    The problem I'm trying to solve is a file doesn't exist error when trying to open a file using the <file:readFile>. To make this issue most interesting, only our production environment exhibits the issue of not finding this file on the fileshare using the <file:readFile> tag (and if I simply 'retry' the process after 2-8 minutes, the script finds the file just fine, without any polling logic needed).

     

    Two snippets below, namespaces and polling loop. The loop does iterate, but the commands within the util:available doesn't ever get executed. The loop does iterate, and sleeps ok with the said milliseconds, but the file is never 'seen' by the util:available. Funny thing is, the file is actually there......

     

    What am I doing wrong? I've tried with and without the "file://" prefic for the uri.

     

    The fileshare directory is of the form /fs0/clarity1/files/filestore/clarity/

     

    (I'm on v13.1, CA on demand).

     

    Regards,

    Sam.

    ======================

     

    <gel:script

        xmlns:core="jelly:core"

        xmlns:file="jelly:com.niku.union.gel.FileTagLibrary"

        xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"

        xmlns:soap="jelly:com.niku.union.gel.SOAPTagLibrary"

        xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"

        xmlns:sql="jelly:sql"

        xmlns:util="jelly:util"

        xmlns:xog="http://www.niku.com/xog"

    >

     

    <!-- snip -->

    <core:set value="false" var="errorFlag"/>

    <!-- snip -->

     

     

    <!-- check for file existence a number of times, before timing out if required-->

    <!-- CA couldn't resolve this timing issue for us, which only happens in PROD, not in dev or test yet. -->

    <core:if test="${errorFlag==false}">

        <core:set value="0" var="v_file_exists"/>

        <core:set value="0" var="v_counter"/>

        <core:set value="24" var="v_counter_limit"/>

        <core:set value="500" var="v_sleep_ms"/>

        <core:while test="${v_counter le v_counter_limit and v_file_exists eq 0}">

            <core:catch var="v_exception">

                <util:available uri="file://${v_fileStore}${file}">

                    <gel:log category="File" level="INFO">File available returned true on iteration ${v_counter}.</gel:log>

                    <core:set value="1" var="v_file_exists"/>

                </util:available>

            </core:catch>

            <core:choose>

                <core:when test="${v_exception!=null}">

                    <gel:log category="File" level="WARN">Unable to test for file existence:${file} from directory:${v_fileStore} Exception:${v_exception}.</gel:log>

                    <core:set value="true" var="errorFlag"/>

                </core:when>

                <core:when test="${v_counter lt v_counter_limit}">

                    <gel:log category="File" level="INFO">File polling iteration ${v_counter}, sleeping...</gel:log>

                    <util:sleep millis="${v_sleep_ms}"/>

                </core:when>

                <core:when test="${v_counter ge v_counter_limit}">

                    <core:set value="Polled too many times / Timed out polling for file existence:${file} from directory:${v_fileStore} Iteration:(${v_counter}) out of (${v_counter_limit}), waiting (${v_sleep_ms/1000}) seconds between each." var="v_exception"/>

                    <gel:log category="File" level="WARN">${v_exception}</gel:log>

                    <core:set value="true" var="errorFlag"/>

                </core:when>

                <core:otherwise>

                    <gel:log category="File" level="WARN">Shouldnt ever be here ${v_counter}.</gel:log>

                </core:otherwise>

            </core:choose>

            <core:set value="${v_counter+1}" var="v_counter"/>

        </core:while>

    </core:if>

     

    <!-- open the file from the windows directory, and load to a gel variable. -->

    <core:if test="${errorFlag==false}">

        <core:catch var="v_exception">

            <file:readFile

            delimiter="~"

            embedded="false"

            escapeText="true"

            fileName="${v_fileStore}${file}"

            var="v_file_in_mem"/>

        </core:catch>

        <core:choose>

            <core:when test="${v_exception!=null}">

                <gel:log category="File" level="ERROR">Unable to read file:${file} from directory:${v_fileStore} Exception:${v_exception}.</gel:log>

                <core:set value="true" var="errorFlag"/>

            </core:when>

            <core:otherwise>

                <core:set value="${v_file_in_mem.size()}" var="v_file_in_mem_size"/>

                <core:set value="0" var="v_count"/>

                <core:if test="${DEBUG>0}">

                    <gel:log category="File" level="INFO">File has ${v_file_in_mem_size} rows.</gel:log>

                </core:if>

            </core:otherwise>

        </core:choose>

    </core:if>



  • 2.  Re: How do I check for file existence using

     
    Posted Jul 09, 2014 05:11 PM

    Hi All,

     

    Any ideas here for Sam?

     

    Thanks!

    Chris



  • 3.  Re: How do I check for file existence using

    Posted Jul 11, 2014 11:40 AM

    This works on my machine.  For file you need file:/// to get to the top of the disk.

     

    <gel:script

        xmlns:core="jelly:core"

        xmlns:log="jelly:log"

        xmlns:util="jelly:util"

        xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"

        xmlns:xsd="http://www.w3.org/2001/XMLSchema"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

     

        <gel:out>Start this Script</gel:out>

      

        <core:set var="fileName" value="C:\Temp\ProjectsService.cs"/>

      

        <util:available uri="file:///${fileName}">

            <gel:out>This URL is available</gel:out>

        </util:available>

      

        <core:new className="java.io.File" escapeText="false" var="fileTest" >

            <core:arg type="java.lang.String" value="${fileName}" />

        </core:new>

      

        <core:if test="${fileTest.exists()}">

                <gel:out>This file exits ${fileName}</gel:out>

        </core:if>

      

        <gel:out>End this Script</gel:out>

     

    </gel:script>

     

    V/r,

    Gene



  • 4.  Re: How do I check for file existence using

    Posted Jul 11, 2014 11:58 AM

    Beware of empty/zero-sized-files.



  • 5.  Re: How do I check for file existence using

    Posted Jul 11, 2014 12:16 PM

    Very good point:

     

    <core:if test="${fileTest.exists()}">

        <gel:out>This file exits ${fileName}</gel:out>

        <core:if test="${fileTest.length() == 0}">

            <gel:out>The file is empty</gel:out>

        </core:if>

        <core:if test="${fileTest.length() > 0}">

            <gel:out>The file is not empty</gel:out>

        </core:if>

    </core:if>

     

    V/r,

    Gene



  • 6.  Re: How do I check for file existence using

    Posted Jul 13, 2014 07:52 PM

    gcubed, may I ask which version you got this to work on?

     

    It may be that util:available is not available on my v13.1... (as it doesn't seem to be there in the ca support docs for that version, does appear to be there in v13.3)



  • 7.  Re: How do I check for file existence using

    Posted Jul 14, 2014 10:30 AM

    I don't have a 13.1 system but it worked on a 13.2 and 13.3 system.  My 13.3 system is OnDemand.  I ran this script.

     

    <?xml version="1.0" encoding="utf-8"?>

    <gel:script

        xmlns:core="jelly:core"

        xmlns:log="jelly:log"

        xmlns:util="jelly:util"

        xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"

        xmlns:xsd="http://www.w3.org/2001/XMLSchema"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

     

        <gel:log>Start this Script</gel:log>

       

        <core:set var="fileName" value="/fs0/clarity1/share/UrlOutput.txt"/>

       

        <util:available uri="file://${fileName}">

            <gel:log>This URL is available</gel:log>

        </util:available>

       

        <core:new className="java.io.File" escapeText="false" var="fileTest" >

            <core:arg type="java.lang.String" value="${fileName}" />

        </core:new>

       

        <core:if test="${fileTest.exists()}">

                <gel:log>This file exits ${fileName}</gel:log>

                <core:if test="${fileTest.length() == 0}">

                    <gel:log>The file is empty</gel:log>

                </core:if>

                <core:if test="${fileTest.length() > 0}">

                    <gel:log>The file is not empty</gel:log>

                </core:if>

        </core:if>

       

        <gel:log>End this Script</gel:log>

     

    </gel:script>

     

    And here is the log for it:

     

     

    The is an option to restrict gel tags, I not sure what it restricts but you could ask the OnDemand group, via Support if that is turn on in your instance.

     

    V/r,

    Gene



  • 8.  Re: How do I check for file existence using

    Posted Jul 14, 2014 04:21 PM

    Try <util:available file="${v_fileStore}${file}">



  • 9.  Re: How do I check for file existence using
    Best Answer

    Posted Jul 17, 2014 02:48 AM

    In answer to my own post (weird!), what I ended up doing was to use the exception of the <file:readFile> to drive the polling logic. So I've replaced my above GEL snippet with the below.

     

    I was also suggested by CA Support that util:available may not be available on v13.1 - and I took their word for it.

     

    Gel below.

     

    Thanks to All for looking into it!

     

    Sam.

     

    =====

     

    <!-- check for file existence a number of times, before timing out if required-->

    <core:if test="${errorFlag==false}">

        <core:set value="0" var="v_file_exists"/>

        <core:set value="0" var="v_counter"/>

        <core:set value="72" var="v_counter_limit"/>

        <core:set value="10000" var="v_sleep_ms"/> <!-- v_counter_limit multiplied by v_sleep_ms equals total timeout -->

        <core:while test="${v_counter le v_counter_limit and v_file_exists eq 0}">

            <core:catch var="v_exception">

                <file:readFile

                delimiter="~"

                embedded="false"

                escapeText="true"

                fileName="${v_fileStore}${file}"

                var="v_file_in_mem"/>

            </core:catch>

            <core:choose>

                <core:when test="${v_exception == null and v_counter lt v_counter_limit}">

                    <gel:log category="File" level="INFO">File found on iteration ${v_counter}.</gel:log>

                    <core:set value="1" var="v_file_exists"/>

                    <core:set value="${v_file_in_mem.size()}" var="v_file_in_mem_size"/>

                    <core:if test="${DEBUG>0}">

                        <gel:log category="File" level="INFO">File has ${v_file_in_mem_size} rows.</gel:log>

                    </core:if>

                </core:when>

                <core:when test="${v_exception != null and v_counter lt v_counter_limit}">

                    <gel:log category="File" level="INFO">File not found on polling iteration ${v_counter}, sleeping for another (${v_sleep_ms/1000}) seconds... (Exception:${v_exception}).</gel:log>

                    <util:sleep millis="${v_sleep_ms}"/>

                </core:when>

                <core:when test="${v_counter ge v_counter_limit}">

                    <core:set value="Polled too many times / Timed out polling for file existence:${file} from directory:${v_fileStore} Iteration:(${v_counter}) out of (${v_counter_limit}), waiting (${v_sleep_ms/1000}) seconds between each." var="v_exception"/>

                    <gel:log category="File" level="WARN">${v_exception}</gel:log>

                    <core:set value="true" var="errorFlag"/>

                </core:when>

                <core:otherwise>

                    <gel:log category="File" level="WARN">Shouldn't ever be here ${v_counter}.</gel:log>

                </core:otherwise>

            </core:choose>

            <core:set value="${v_counter+1}" var="v_counter"/>

        </core:while>

    </core:if>