How to get list of scheduled tasks and last run results in ColdFusion?
Asked Answered
C

3

19

We're trying to build a dashboard for our cron jobs ---- CF, Java, SQLServer, etc. so that we can see when things were run last, what the result was, and when they're scheduled to run next.

Is there a way with the CFAdmin API or some undocumented <cfschedule> trick to get a list of:

  1. What tasks are scheduled?
  2. What the last run time was?
  3. Did it succeed?
  4. When is it scheduled to run again?

We're currently on CF8, but will be upgrading to CF9 within a few weeks.

Contrapose answered 23/2, 2010 at 17:44 Comment(2)
Great question. I would love to get that same information.Fromma
any chance your dashboard will end up on riaforge?? that sounds greatLinlithgow
T
29

I did a little research into this for you. I found a somewhat older reference that is still valid, at least in CF8 and presumably in CF9 as well.

<cfobject type="JAVA" action="Create" name="factory" class="coldfusion.server.ServiceFactory">
<cfset allTasks = factory.CronService.listAll()/>
<cfloop index="i" from="1" to="#ArrayLen(allTasks)#">
    <cfdump var="#allTasks[i]#" />
</cfloop>

This answers your questions #1 and #4. As for #3, there can be no answer to that. ColdFusion's scheduled task engine is just loading up the specified URL at the prescribed time. There is no success or fail -- it simply performs an HTTP request.

Hope this helps.

Thomas answered 23/2, 2010 at 18:48 Comment(4)
Nice, +1. I would opt for <cfset factory = CreateObject("java", …)>, though, I find that easier to read.Aedes
Awesome. Exactly what I was trying to accomplish. I think I'll create a table in the db that can hold running/failure states for these jobs. Set it as 'running' when it starts, and when it makes it to completion, set it as 'success'Contrapose
Railo 3+ and CF10+ offer a list action: <cfschedule action="list" returnvariable="qTasks">Amimia
@Amimia it is actually <cfschedule action="list" mode="server|application" result="qTasks"> with mode being optional and defaulting to serverDakar
S
2

It is possible to "Publish" the results of the job. The response from the HTTP request can be written to the file server, and that will have the values of the last run job.

<cfschedule action = "update"
    task = "TaskName" 
    operation = "HTTPRequest"
    url = "/index.cfm?action=task"
    startDate = "#STARTDATE#"
    startTime = "12:00:00 AM"
    interval = "Daily"
    resolveURL = "NO"
    requestTimeOut = "600"
    publish = "yes"
    path = "#PATH#"
    file = "log_file.log">

Then you can verify the log against the database if you wanted. Since it is the response from the page, you can get and store errors and warnings here as well.

Subzero answered 21/5, 2012 at 21:44 Comment(0)
M
1

@eric kolb is right - that is the way to do it programmatically. If you want more control over how the list reacts, try the following code (essentially the same, but in cfscript):

<cfscript>
scheduledTasksArray=ArrayNew(1);
taskService=createobject('java','coldfusion.server.ServiceFactory').getCronService();
scheduledTasksArray=taskservice.listall();

Also, to answer #2 and #3 (which is pretty much just one two-part question if you do it right): When the task is run, send yourself an email right at the top saying "HEY! I'M RUNNING!!!!" and then another saying "HEY! I'M DONE!!!" at the bottom of the code for the task - you could add in a timestamp as well to tell when it started and stopped (logging this in a database works too). Also, to know when it will run next, just take a look at the last time AND the "interval" field gotten back from the results of the ServiceFactory call. (If you need further explanation on what I mean by this, feel free to ask.

Hope this helps if you haven't figured out what you needed to already

Massproduce answered 17/7, 2012 at 20:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.