【问题标题】:cfschedule: Fire onError function if url cannot be foundcfschedule:如果找不到 url,则触发 onError 函数
【发布时间】:2021-10-06 14:44:31
【问题描述】:

我们有这样定义的 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.cfmonError 函数尚未触发。我们只是“不小心”偶然发现了我们所有的任务从那以后都没有执行。

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

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

【问题讨论】:

  • 您的 CFC 是否在实现 CFIDE.scheduler.ITaskEventHandlereventHandler 属性中定义?文档提到CFC 必须实现 CFIDE.scheduler.ITaskEventHandler。
  • @Miguel-F 是的,它正在实现CFIDE.scheduler.ITaskEventHandler。我尝试向 onTaskEnd 函数添加一个附加参数,但随后它抛出了一个错误,即找不到 eventHandler

标签: coldfusion eventhandler cfschedule


【解决方案1】:

我设法实施了一种解决方法。在我们的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>

【讨论】:

    猜你喜欢
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 2014-01-23
    • 2020-09-08
    • 2020-08-14
    • 2022-06-22
    • 1970-01-01
    相关资源
    最近更新 更多