Search code examples
coldfusioneventhandler

cfschedule: Fire onError function if url cannot be found


We have ColdFusion tasks defined like this:

<cfschedule
    action="update"
    task="Test"
    operation="HTTPRequest"
    url="#path.getFileFolder()#tasks/test.cfm"
    startDate="#now()#"
    startTime="00:00"
    interval="daily"
    resolveURL="no"
    publish="yes"
    file="test.txt"
    path="#path.getLogFolder#"
    eventHandler="tasks.eventHandler"
    onException="invokeHandler">

The onError function in the eventHandler looks like this:

<cffunction name="onError" returntype="void">
    <cfargument name="context" type="struct" required="false" />
    <cfscript>
        var slackHookURL = 'urlToOurSlackErrorChannel';
        var slackMessage = 'ourSlackMessage';
    </cfscript>

    <cftry>
        <cfhttp url="#slackHookURL#" method="post" result="httpResp" timeout="60">
            <cfhttpparam type="header" name="Content-Type" value="application/json" />
            <cfhttpparam type="body" value="#slackMessage#" />
        </cfhttp>
        <cfcatch></cfcatch>
    </cftry>
</cffunction>

We had the problem that after a server switch our config file missed a / in the file folder path. So the url referenced in all of our tasks pointed to something like https://ourPagetasks/test.cfm instead of https://ourPage/tasks/test.cfm. The onError function hasn't been fired. We just "accidentally" stumbled upon all our tasks not having been executed ever since.

In the test.txt log file however we found the message "Connection timeout". Shouldn't the onError function warn us if that happens? Or is there any workaround so I can check the text that's about to be written to the log file? The onTaskEnd function of the eventHandler is only allowed to have the parameter context which tells me nothing about what's going to be logged.

I hope I explained my problem somehow understandable. Thanks in advance!


Solution

  • I managed to implement a workaround. In our scheduledTasks.cfm I added the following lines at the end to check if any of the urls are invalid:

    <!--- Check if the tasks are defined correctly --->
    <cfschedule action="list" mode="server" result="tasks" />
    <cfloop query="tasks">
        <cfhttp method="head" url="#tasks.URL#" />
        <cfif len(cfhttp.errorDetail)>
            <cfscript>
                slackHookURL = 'urlToOurSlackErrorChannel';
                slackMessage = 'ourSlackMessage';
            </cfscript>
    
            <cftry>
                <cfhttp url="#slackHookURL#" method="post" result="httpResp" timeout="60">
                    <cfhttpparam type="header" name="Content-Type" value="application/json" />
                    <cfhttpparam type="body" value="#slackMessage#" />
                </cfhttp>
                <cfcatch></cfcatch>
            </cftry>
        </cfif>
    </cfloop>