Pergunta

apologies if this has been asked before but i couldn't find anything that could answer my question.

We have a Coldfusion script that runs from our website and queries an external dsn in our office.

Problem is that the server is located in rural england and sometimes the server is unavailable (thanks to our unreliable UK ISP!!)

Is there a command in coldfusion that could query or ping an external dsn so I can wrap the whole script in a cfif statement and get an email if it fails to connect?

Foi útil?

Solução

This is an extension to a comment on duncan's answer, but can't put code blocks in comments, so...

<cfset TargetHost = "0.0.0.0" />
<cfset PingAttempts = 4 />

<cfif find('Windows',Server.Os.Name)>
    <cfset Args = "-n #PingAttempts# #TargetHost#" />
<cfelse>
    <cfset Args = "-c #PingAttempts# #TargetHost#" />
</cfif>

<cfexecute name="ping" arguments=#Args# variable="PingResult" timeout=10 />

<cfif PingResult CONTAINS "100% packet loss"
    OR PingResult CONTAINS "100% loss"
    >
    [send alert]
</cfif>

<cfdump var=#PingResult# />

(In general, I'd still go with checking via an actual query - pinging only confirms the machine is online, not that the database is responding.)

Outras dicas

JFGI:

<cfexecute name="C:\winnt\system32\ping.exe" arguments="#request.myIP#"  variable="myPing" timeout="8"></cfexecute> 

Or on Linux/Unix:

<cfexecute name="ping" arguments="#request.myIP# -c 3" timeout="10" variable="myPing" />

<cfif myPing contains "Request timed out">
  <cfmail ...>
<cfelse>
  [do something else]
</cfif>

Or http://www.bennadel.com/blog/751-Learning-ColdFusion-8-Ping-User-Defined-Function-Inspired-By-Ray-Camden-.htm

To find out if the datasource works just try to use it:

<cftry>
    <cfquery datasource="external" timeout=10 >
        SELECT 1
    </cfquery>
    <cfcatch>
        <cfif [timeout error]>
            <cfmail ...>No response in 10 seconds</cfmail>
        <cfelse>
            [do something else]
        </cfif>
    </cfcatch>
</cftry>


Since that doesn't appear to work, another option might be:

<cfthread action="run" name="QueryThread">
    <cfquery datasource="external">SELECT 1</cfquery>
</cfthread>
<cfthread action="run" name="CheckThread">
    <cfthread action="sleep" duration="10000" />
    <cfif cfthread.QueryThread.Status NEQ 'COMPLETED'>
        [handle error]
        <cfthread action="terminate" name="QueryThread" />
    </cfif>
</cfthread>

Same concept, but using multiple threads should mean that even if the query is too deep for CF to break, you can still trigger the email sending after ten seconds.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top