cfschedule:如果找不到 url,则触发 onError 函数

cfschedule: Fire onError function if url cannot be found

我们有这样定义的 ColdFusion 任务:

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

eventHandler 中的 onError 函数如下所示:

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

我们遇到的问题是,在服务器切换后,我们的 config 文件在文件夹路径中丢失了 /。所以我们所有任务中引用的 url 指向类似 https://ourPagetasks/test.cfm 而不是 https://ourPage/tasks/test.cfm 的东西。 onError 函数尚未触发。我们只是“不小心”发现了我们所有的任务都没有被执行过。

但是在 test.txt 日志文件中我们发现了消息“连接超时”。如果发生这种情况,onError 函数不应该警告我们吗?或者是否有任何解决方法,以便我可以检查将要写入日志文件的文本? eventHandleronTaskEnd 函数只允许有参数 context,它不会告诉我将要记录的内容。

我希望我能以某种方式解释我的问题。提前致谢!

我设法实施了解决方法。在我们的 scheduledTasks.cfm 中,我在末尾添加了以下行以检查是否有任何 urls 无效:

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