I'm running a cfschedule at "daily" intervals, but is there anyway to get it to execute at random hours not every 24th hour from when i initially set it to run?
So say I set it daily, how can I get it execute on a random hour in the day as opposed to every 24th day?
The same applies for monthly, how can I get it execute on a random day monthly as opposed to every 30th day?
my code now:
<cfschedule action = "update"
task = "sendit"
operation = "HTTPRequest"
url = "http://xyz.com/xyz/xyz.cfm"
startDate = "#DateFormat(now(),'mmmm, dd, yyyy')#"
startTime = "#TIMEFORMAT(now(),'hh:mm:ss')#"
interval = "daily"
resolveURL = "Yes"
requestTimeOut = "600">
Thanks!
Wouldnt that skip it from executing if the schedule executes when the page is sleeping?
Or will it just subtract the sleeping seconds?
Also is this how you put a page to sleep,
<cfset thread = CreateObject("java", "java.lang.Thread")><cfflush> <cfset thread.sleep(5000)>
Also to get this to work so that its a random time daily, wouldn't I have to set the schedule to run for a certain amount of seconds as opposed to daily...
I'm running a cfschedule at "daily" intervals, but is there anyway to get it to execute at random hours not every 24th hour from when i initially set it to run?
Why do you want to do this?
You could use the task code to update its own schedule each time it runs to be rescheduled for an random hour the following day.
--
Adam
Come on: try to think through what I'm suggesting.
You have this:
<cfschedule action = "update"
task = "sendit"
operation = "HTTPRequest"
url = "http://xyz.com/xyz/xyz.cfm"
startDate = "#DateFormat(now(),'mmmm, dd, yyyy')#"
startTime = "#TIMEFORMAT(now(),'hh:mm:ss')#"
interval = "daily"
resolveURL = "Yes"
requestTimeOut = "600">
Which is not just "running daily", it's "running daily AT A SPECIFIC TIME". So you've coded the task to run @ #TIMEFORMAT(now(),'hh:mm:ss')# each day. What you want to do is to reschedule it (UPDATE it) to run at #some random time of your choosing#.
So you recode xyz.cfm to do this:
1) whatever it does now
2) PLUS a call to reschedule the task for the following day at #some random time#.
Indeed if you take this approach, wherein you specifically schedule each event, you probably want to just have an interval of ONCE, rather than daily.
--
Adam
This goes against what cfschedule was designed for, namely, to schedule tasks at fixed time intervals. I do believe that, even if you use something like this
<cfschedule action="update" interval="#some_randomly_generated_integer#">
it still won't work. I think that ColdFusion will use one of the integers to define the task, then will cache the task for continued reuse later, ignoring any further changes to the value of the interval attribute.
At a random time, do you mean that i'd have to replace the "starttime" attribute with my own variable that outputs a random hour in the day?
Also, Out of curiosity.... can't I just take that second call you're saying to create at a random time and replace it with this one so there's still essentially only one call.
Also, if I were to use 2 cfschedule calls like you said, wouldn't then changing the first call to ONCE as opposed to daily bring the situation back to square one where the cfschedule alone can't run at a random output even if I outputted a random HOUR value?
@adam and @BKBK
Putting it all together, it seems to me a viable solution would be:
1. Running the first cfschedule to execute xyz.cfm every 48 hours.
2. Putting a cfschedule inside of xyz.cfm that has an action of UPDATE, interval of DAILY with a randomly outputted "starttime" hour which will run every 24 hours.
So basically the first schedule runs and then opens the second schedule which has the random output, but since the first schedule will run again every other day it will trigger the second schedule to update itself and produce another random hour as opposed to caching it...
Does that make sense.
Suppose, the task has to fire once daily, at a random time. Take a one-week period (illustrated below).
|x...........|......x.....|...........x|.x..........|x...........|.... .......x|.....x......|
Then the interval between successive task executions could be as short as a few minutes(such as between days 3 and 4), or as long as 2 days(such as between days 5 and 6). Thought I'd share the thought.
I am curious to know whether the following idea works. The tasks are to be run in this order:
setTime.cfm
<cfschedule action = "update"
task = "setTime"
operation = "HTTPRequest"
url = "http://127.0.0.1:8500/cfscheduleTest/randomTime.cfm"
startDate = "4/21/2012"
startTime = "00:00"
interval = "daily">
randomTime.cfm
<cfset randomHour = randRange(0,23,"SHA1PRNG")><!--- Random integer in range [0,23] --->
<cfset application.randomTime = randomHour & ":15">
<!--- Schedule the task--->
<!---<cfhttp method="get" url="http://127.0.0.1:8500/cfscheduleTest/task.cfm">--->
<cfinclude template="task.cfm">
task.cfm
<cfparam name="application.randomTime" default="9:15">
<cfschedule action = "update"
task = "runner"
operation = "HTTPRequest"
url = "http://127.0.0.1:8500/cfscheduleTest/xyz.cfm"
startDate = "4/21/2012"
startTime = "#application.randomTime#"
interval = "daily"
resolveURL = "Yes"
requestTimeOut = "600">
This goes against what cfschedule was designed for, namely, to schedule tasks at fixed time intervals.
Not so. <cfschedule> can also be used simply to run some code "later": it doesn't necessarily need to be at an interval. It specifically caters for running a task only once (interval="once"). It also caters for updating an existing task as required (action="update"), so it's entirely possible to have a run-once task schedule itself again at a different time.
I do believe that,
Fortunately we don't have to base this on "beliefs". I know for a fact it works the way I said.
if you use something like this
<cfschedule action="update" interval="#some_randomly_generated_integer#">
it still won't work.
No, it won't but not for the reason you go on to guess. One does need to re-specify all the required fields (this is the one thing I was unsure about before, but I've just tested it).
--
Adam
It doesn't need to be that complicated. Here's an example of a task which reschedules itself at a random time between 1-5min hence.
<!--- randomInteral.cfm --->
<cfset dRunTime = dateAdd("n", randRange(1,5), now())>
<cfset sRunTime = timeFormat(dRunTime, "HH:MM")>
<cflog file="randomIntervalExperiment" text="Ran task. Next run @ #sRunTime#">
<cfschedule
action = "update"
interval = "once"
operation = "HTTPRequest"
startdate = "#dateFormat(now(), 'mm/dd/yy')#"
starttime = "#sRunTime#"
task ="randomInterval"
url = "http://localhost:8301/shared/CF/CFML/tags/other/schedule/randomInterva l.cfm"
>
For your purposes, you'd need to vary it to increment the startdate by one, as well as setting the start time to be a random hour (rather than just 1-5min). But that's the basic gist of what you want to do.
I just did 1-5min in my example so I could actually test the veracity of my suggestions (without having to wait until tomorrow ;-)), rather than just guess...
--
Adam
Adam Cameron. wrote:
This goes against what cfschedule was designed for, namely, to schedule tasks at fixed time intervals.
Not so. <cfschedule> can also be used simply to run some code "later": it doesn't necessarily need to be at an interval. It specifically caters for running a task only once (interval="once"). It also caters for updating an existing task as required (action="update"), so it's entirely possible to have a run-once task schedule itself again at a different time.
All of this is true. It in fact confirms my point. "Later", "once" and "update" tasks all occur at a fixed time interval, even in cases where tasks are configured to be rerun. The way I see it, recurrence is then a sequence of fixed intervals. However, I should point out that I am using the word 'fixed' as a contrast to the 'random' in the question. I took it for granted that was clear.
lovewebdev wrote:
A little off topic, is there any reason why a cfschedule won't delete. I used:
<cfschedule action = "delete"
task = "sendreminder">
But the task just keeps on executing. I'm contacting the web host now.
Are you sure the code is in a CFM page under the same application? Just in case the scheduler thread was held up, I would keep running the delete code till ColdFusion tells me the task sendreminder cannot be found.
A little off topic, is there any reason why a cfschedule won't delete. I used:
<cfschedule action = "delete"
task = "sendreminder">
But the task just keeps on executing. I'm contacting the web host now.
That syntax works for me, so if it's not working for you, there's something else afoot. However you are deleting a task called "sendreminder" there, and in your earlier code, it was called "sendit". Are you sure you're trying to delete the correct task?
--
Adam
Yes the task name is correct. I'm finding out from the hosting company why its not deleting. Aargh. Maybe it hung somehow. Dont know.
Also, I'm trying to sort of lock access to the CFM task page so that it doesnt get executed randomly or by google so in the url for the cfschedule tag, I put something like:
http://xyz.com/xyz/xyz.cfm?thecode=e342feeer32
Then in the cfm task page I only execute if the querystring is found.
Is that an ok way to do this?
Adam Cameron. wrote:
For your purposes, you'd need to vary it to increment the startdate by one, as well as setting the start time to be a random hour (rather than just 1-5min). But that's the basic gist of what you want to do.
I just did 1-5min in my example so I could actually test the veracity of my suggestions (without having to wait until tomorrow ;-)), rather than just guess...
Still, I think you're making some assumptions that may or may not turn out to be true. So you "increment the startdate by one, as well as setting the start time to be a random hour (rather than just 1-5min)", and the job runs once, firing itself. And off it goes again. So when will it take the time off to run Lovewebdev's task, xyz.cfm?
lovewebdev wrote:
Yes the task name is correct. I'm finding out from the hosting company why its not deleting. Aargh. Maybe it hung somehow. Dont know.
In my experience, the best place to put the delete code is the CFM page that defines the task. Temporarily comment out the cfschedule tag that defines the task, and run the delete code in its place. As I said, ColdFusion is known to sometimes require a few tries before granting you your wish.
Adam Cameron. wrote:
<!--- randomInteral.cfm --->
<cfset dRunTime = dateAdd("n", randRange(1,5), now())>
<cfset sRunTime = timeFormat(dRunTime, "HH:MM")>
<cflog file="randomIntervalExperiment" text="Ran task. Next run @ #sRunTime#">
<cfschedule
action = "update"
interval = "once"
operation = "HTTPRequest"
startdate = "#dateFormat(now(), 'mm/dd/yy')#"
starttime = "#sRunTime#"
task ="randomInterval"
url = "http://localhost:8301/shared/CF/CFML/tags/other/schedule/randomInterva l.cfm"
>
For your purposes, you'd need to vary it to increment the startdate by one, as well as setting the start time to be a random hour (rather than just 1-5min).
I have revised my opinion about this solution. With some adaptation, it could be an efficient solution to the problem.
Why not adapt the page to also run the task code! The page flow is then as follows:
- Include code of task you wish to schedule, xyz.cfm, or do cfhttp-get to xyz.cfm;
- Randomly vary startdate and startTime;
- <cfschedule>
North America
Europe, Middle East and Africa
Asia Pacific