1

I am running some code as a scheduled task, setup in CF Administrator.

Is there a way to tell in the code that the code ran as a scheduled task, whether it was ran by clicking the run icon in the CF Administrator scheduled task area, or whether it was called directly in a browser?

Adding additional variables will not work?

Peter Boughton
  • 110,170
  • 32
  • 120
  • 176
user1880192
  • 51
  • 10
  • Your question isn't clear to me. Are you talking about scheduled tasks? And determining whether the scheduled task ran? – BKK Dec 20 '12 at 18:00
  • I am asking about scheduled tasks in cf admin. not really determining if the scheduled task is ran , but determining if it is ran from CF ADMIN rather than from the browser – user1880192 Dec 20 '12 at 18:04
  • Well, how do you want to store this information about how it ran? You could add a urlflag on the url of the CF Scheduled Task that says "&cfadmin=1" and then have some logic that looks for this flag and logs either condition using CFLOG. – BKK Dec 20 '12 at 18:15
  • I have attempted to clarify the question - please verify if it is still asking the same thing you want to know... – Peter Boughton Dec 20 '12 at 18:53

1 Answers1

6

From the test link in the CF admin

If you're asking if you can identify the difference between a scheduled task being run manually by clicking the test link in the coldfusion admin or run on schedule, you can enable logging of scheduled tasks. Any time the task is run by the user the log entry will say [name of job] Executing because of user request at {timestamp}. If it ran naturally, the log entry will say [name of job] Executing at {timestamp}

I've looked for a way to tell by code and I can't find anything. It would depend on the accuracy of the scheduler but you could look to see if now() is equal to the time of the schedule. Something like (pseudo code):

<!--- disclaimer: I've heard stories that cfschedule sometimes runs a little late --->
<cfset scheduleTime = "2:00 am">
<cfif cgi.HTTP_USER_AGENT eq "CFSCHEDULE" and timeFormat(now(), "h:mm tt") eq scheduleTime>
   <!--- ran naturally --->
<cfelse>
   <!--- ran by force --->
</cfif>

From a browser

If you want to know if your scheduled task was run by the schedule or if the file was hit by the browser you can look at cgi.HTTP_USER_AGENT. if it is run by the scheduler it will equal CFSCHEDULE otherwise it will equal whatever the client is set to send.

Perhaps, Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11

or if you're lucky enough to have a bot hit it, something like: Mozilla/5.0 (compatible; MJ12bot/v1.4.3; http://www.majestic12.co.uk/bot.php?+)

It is possible to spoof the client or server request to make the user user agent to say CFSCHEDULE but it isn't likely.

on a side note...

The default user agent for cfhttp is "COLDFUSION", in case you were interested.

genericHCU
  • 4,394
  • 2
  • 22
  • 34
  • Can you confirm whether CFSCHEDULE is only used when running scheduled tasks automatically, or is it also used when manually calling the task (via the admin run icon)? If so, is there a difference between manually calling the task vs directly calling the URL? – Peter Boughton Dec 20 '12 at 18:55
  • I tried both manually and waiting for it to run naturally and both are "CFSCHEDULE". Great question, Peter. – genericHCU Dec 20 '12 at 18:57
  • The question wasn't worded that way when I answered the question :( The question just said "the link" which I took to mean could be launched manually from the system, not the admin. – genericHCU Dec 20 '12 at 18:59
  • Yeah, I know - I'm guessing it's what they mean. Either way it's useful info to have. :) Pity CF doesn't seem to distinguish between the two though. – Peter Boughton Dec 20 '12 at 19:02
  • 1
    I did some more snooping and added some hopefully more useful information. – genericHCU Dec 20 '12 at 19:10
  • 1
    The question was actually saying the run icon in cf admin – user1880192 Dec 20 '12 at 19:18
  • The top paragraph of my answer is all I could discover. You could try scouring getPageContext() to see if there is more data. – genericHCU Dec 20 '12 at 19:21
  • well I am interesting I guess manuall when the run icon is clicked can I capture that it whas clicked througth the CF Admin not somebody ran this link througth the browser – user1880192 Dec 20 '12 at 19:24
  • I've updated the answer with another possibility, but use at your own risk. – genericHCU Dec 20 '12 at 23:17
  • To cater for the potential lateness, you could do DateDiff with an appropriate resolution - e.g. for five minutes: `DateDiff(a,b,'n') LT 5`. – Peter Boughton Dec 21 '12 at 00:45
  • 1
    Also, rather than hard-coding the time, you can probably get the actual time with [the code here](http://stackoverflow.com/questions/2320396/how-to-get-list-of-scheduled-tasks-and-last-run-results-in-coldfusion). – Peter Boughton Dec 21 '12 at 00:47
  • Yeah, good point. All the tasks are stored in an XML document that could be theoretically be parsed as well. – genericHCU Dec 21 '12 at 10:07