How can I run a ColdFusion scheduled task at an interval <60 seconds?

梦想的初衷 提交于 2019-12-05 02:13:17

The only way to do this is with some manual editing of the file for your scheduled tasks.

So:

  1. Create the scheduled task with an interval of say 5 mins.
  2. Open the file called neo-cron.xml in the lib directory of your CF install. You might want to back it up first.
  3. Search for the name of your scheduled task. It's a big chunk of XML so you might like to format it and search for it in a XML editor.
  4. A little after the name of the task you should see something like this: <var name="interval"><string>300</string></var>. This is the number of seconds between when the task runs. It's in seconds here, so you can manually adjust it to 30 and then save the file and close it.

This will still show as 1 min in the CF admin but it should run every 30 seconds - add logging to prove it if you wish!

Not that if you edit any other scheduled task using CF admin your manual changes will be unaffected, but if you edit the actual task you manually adjusted it will overwrite your changes.

Hope that helps!

You could use curl and schedule a task on the server Win or nix.

Are you really interested in your task running twice a minute, or are you just looking to reduce the latency in detecting changed or new data? If it's the latter, you could look at using CF Event Gateways to detect exactly when to run. If this is the case you'd have far less churn on your server, since you'd run that CFC method immediately upon some event and not just poll endlessly for data.

I know this is an old question now, but I kept ending up back here because I needed a similar solution (only for me it was a 10 second interval), and none of these answers worked. I've finally found a way today that I figured I'd post in case anyone ever ends up in a similar bind.

I found I couldn't use the xml modification method listed as an answer because we're using CF 9.0.1, and the xml would revert almost immediately after I changed it.

I tried looking into cURL, and discovered that while I could use cURL to call the process, I could not get windows scheduler to run in an interval less than a minute, either. Then while searching for a way to get the windows scheduler to run in smaller intervals (since I figured I couldn't fool CF at this point), I stumbled on a suggestion made on experts-exchange (I can't remember the url off hand now).

Based on that, I set up a test using this idea: Add another layer of program that calls the program you want to schedule. Have this layer loop with a sleep, and schedule it for an interval longer than a minute. Ultimately I went for a CF scheduled task that calls my "caller" every 2 minutes, and that "caller" has a loop that sleeps every X seconds.

Below is the code I used for the tests, formatted to answer the questioner's 30 second interval. I made use of a side log-management cfc to output debugging in a way that I could save and revisit.

The test file scheduled to run every 2 min:

<cfsetting requestTimeOut = "120000">

<cfscript>
    /*  Get starting count to keep track of overall running time */
    Start = GetTickCount();

    /*  Set an ID for this process so I know */
    thisProcess = CreateUUID();
    counter = 0;

    /*  Set up a log-file-handler object.
    *   I will use the log lines to keep track of this process 
    *   and whether or not it steps on itself.
    * */
    LogWriter = CreateObject('component','log.LogMgr').init('C:\Logs\');
    LogFile = 'Test\30SecondTestLog';

    LogWriter.WriteToLog('Information',LogFile,'#thisProcess# __ Started');
</cfscript>

<cftry>
    <cfloop condition="Counter LT 4">

        <!--- Output here is if you want to run it manually --->
        <br /><cfdump var="#counter#">

        <!---   Here you could put a cfhttp or similar trigger.
                In my test I just used an update to a test DB table.
                The catch here is that you don't want it to take too
                long to get to the next steps, so you want to avoid
                a long-running process that has to return something.
                --->
        <cfquery datasource="Marge" name="update">
            Update test..testCount
            Set CountField = CountField + 1
        </cfquery>

        <cfscript>
             counter += 1;
             LogWriter.WriteToLog('Information',LogFile,'#thisProcess# __ #counter# - Written');

             /* Important part! Here is where the 30-second delay 
             *  happens. However, we don't want to dally if this is
             *  the last time running it.
             *  */
             if(counter NEQ 4){ sleep(30000); }
        </cfscript>

    </cfloop>

    <!--- Time to output finishing details --->
    <br />Done. <br />

    <cfset End = GetTickCount()>
    <cfdump var="#End - Start#">

    <cfset LogWriter.WriteToLog('Information',LogFile,'#thisProcess# __ Done - Time: #End - Start#')>

<cfcatch type="any">
    <cfset LogWriter.WriteToLog('Error',LogFile,'#thisProcess# __ #counter# - #cfcatch.message#')>
</cfcatch>
</cftry>

The logging cfc used for debugging:

<cfscript>
    variables.LogHome = '';
</cfscript>

<cffunction name="init" raturntype="LogMgr">
    <cfargument name="LogHome" type="string">
    <cfset variables.LogHome = arguments.LogHome>
    <cfreturn this>
</cffunction>

<!--- I wanted to use WriteLog, but that function name is already used in core CF --->
<cffunction name="WriteToLog" returntype="void" hint="writes to a log file.">
    <cfargument name="LogType" type="string" hint="Based off cflog, expects severity level: Information, Warning, Error, or Fatal">
    <cfargument name="LogPath" type="string" hint="Path and file from log root">
    <cfargument name="LogString" type="string" hint="String to output in log">
    <cfscript>

    theFile = variables.LogHome & '\' & arguments.LogPath & '.log';
    theString = arguments.LogType & chr(9) & DateFormat(Now(),'mm/dd/yyyy')& ' ' & TimeFormat(Now(),'HH:mm:ss');
    theString &= '  ' & arguments.LogString;

    </cfscript>
    <cfif FileExists(theFile)>
        <cffile action="append"
                file="#theFile#"
                output="#theString#"
                addnewline="yes">
    <cfelse>
        <cffile action="write"
                file="#theFile#"
                output="#theString#"
                addnewline="yes">
    </cfif>
</cffunction>

</cfcomponent>

The test gave this log output when scheduled for a ten minute window:

Information 02/26/2013 15:29:00 F1E76BAE-C29A-208A-7B14339FD6B9B8D5 __ Started Information 02/26/2013 15:29:00 F1E76BAE-C29A-208A-7B14339FD6B9B8D5 __ 1 - Written Information 02/26/2013 15:29:30 F1E76BAE-C29A-208A-7B14339FD6B9B8D5 __ 2 - Written Information 02/26/2013 15:30:00 F1E76BAE-C29A-208A-7B14339FD6B9B8D5 __ 3 - Written Information 02/26/2013 15:30:30 F1E76BAE-C29A-208A-7B14339FD6B9B8D5 __ 4 - Written Information 02/26/2013 15:30:30 F1E76BAE-C29A-208A-7B14339FD6B9B8D5 __ Done - Time: 90123 Information 02/26/2013 15:31:00 F1F9B64D-C29A-208A-73CEACA04A02F544 __ Started Information 02/26/2013 15:31:00 F1F9B64D-C29A-208A-73CEACA04A02F544 __ 1 - Written Information 02/26/2013 15:31:30 F1F9B64D-C29A-208A-73CEACA04A02F544 __ 2 - Written Information 02/26/2013 15:32:00 F1F9B64D-C29A-208A-73CEACA04A02F544 __ 3 - Written Information 02/26/2013 15:32:30 F1F9B64D-C29A-208A-73CEACA04A02F544 __ 4 - Written Information 02/26/2013 15:32:30 F1F9B64D-C29A-208A-73CEACA04A02F544 __ Done - Time: 90053 Information 02/26/2013 15:33:00 F20C0329-C29A-208A-79C8C0D4C1E1FDFF __ Started Information 02/26/2013 15:33:00 F20C0329-C29A-208A-79C8C0D4C1E1FDFF __ 1 - Written Information 02/26/2013 15:33:30 F20C0329-C29A-208A-79C8C0D4C1E1FDFF __ 2 - Written Information 02/26/2013 15:34:00 F20C0329-C29A-208A-79C8C0D4C1E1FDFF __ 3 - Written Information 02/26/2013 15:34:30 F20C0329-C29A-208A-79C8C0D4C1E1FDFF __ 4 - Written Information 02/26/2013 15:34:30 F20C0329-C29A-208A-79C8C0D4C1E1FDFF __ Done - Time: 90054 Information 02/26/2013 15:35:00 F21E5001-C29A-208A-744291B2817D7702 __ Started Information 02/26/2013 15:35:00 F21E5001-C29A-208A-744291B2817D7702 __ 1 - Written Information 02/26/2013 15:35:30 F21E5001-C29A-208A-744291B2817D7702 __ 2 - Written Information 02/26/2013 15:36:00 F21E5001-C29A-208A-744291B2817D7702 __ 3 - Written Information 02/26/2013 15:36:30 F21E5001-C29A-208A-744291B2817D7702 __ 4 - Written Information 02/26/2013 15:36:30 F21E5001-C29A-208A-744291B2817D7702 __ Done - Time: 90029 Information 02/26/2013 15:37:00 F2309E2F-C29A-208A-7D6A5A2D1CA7D9EF __ Started Information 02/26/2013 15:37:00 F2309E2F-C29A-208A-7D6A5A2D1CA7D9EF __ 1 - Written Information 02/26/2013 15:37:30 F2309E2F-C29A-208A-7D6A5A2D1CA7D9EF __ 2 - Written Information 02/26/2013 15:38:00 F2309E2F-C29A-208A-7D6A5A2D1CA7D9EF __ 3 - Written Information 02/26/2013 15:38:30 F2309E2F-C29A-208A-7D6A5A2D1CA7D9EF __ 4 - Written Information 02/26/2013 15:38:30 F2309E2F-C29A-208A-7D6A5A2D1CA7D9EF __ Done - Time: 90013 Information 02/26/2013 15:39:00 F242ED34-C29A-208A-7952DA25AF0C446D __ Started Information 02/26/2013 15:39:00 F242ED34-C29A-208A-7952DA25AF0C446D __ 1 - Written Information 02/26/2013 15:39:30 F242ED34-C29A-208A-7952DA25AF0C446D __ 2 - Written Information 02/26/2013 15:40:00 F242ED34-C29A-208A-7952DA25AF0C446D __ 3 - Written Information 02/26/2013 15:40:30 F242ED34-C29A-208A-7952DA25AF0C446D __ 4 - Written Information 02/26/2013 15:40:30 F242ED34-C29A-208A-7952DA25AF0C446D __ Done - Time: 90045

I hope this can help anyone else who couldn't get the other answers to work!

Mr. T

Not enough rep to comment on the answer by Ciaran Archer so I'll put it here:

As Kodora pointed out, using ColdFusion 9.0.1 (9.0.1.274733 in my case) the change will not take effect immediately and seems to require a service restart. However, on the restart your change to the neo-cron.xml file is lost!

To work around this, just be sure to stop the ColdFusion service before you make the change to the file. After the service is stopped you can make your adjustment, save the file and start the service again.

It's probably worth noting that on Windows Server 2008 R2 (and probably higher) you might need to open Notepad as Administrator and then open the neo-cron.xml file from within Notepad. Otherwise you may not be allowed to save the changes to the file (due to UAC I believe).

<cfschedule> the tag you're looking for, but be aware that it is a two part tag. It works like this:

<!--- creates/updates the scheduled task you are going to run --->
<cfschedule action="update" task="testing" interval="seconds" operation="HTTPRequest" startdate="7/6/2012" starttime="11:06 AM" URL="yoursite.com">

<!--- runs the task you just updated (the part that makes the task repeat on your specified interval instead. If you do not include this, the task will run only once as it did in @sergii's case--->
<cfschedule action="run" task="testing">

documentation at: http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_r-s_11.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!