Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
Run the job at the start of the day/month. However, amend the file being run to sleep for a random period of time.
Copy link to clipboard
Copied
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...
Copy link to clipboard
Copied
Run the job at the start of the day/month. However, amend the file being run to sleep for a random period of time.
That would tie up a thread whilst the thing is sleeping.
--
Adam
Copy link to clipboard
Copied
Regarding, "That would tie up a thread whilst the thing is sleeping."
Good point.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
I'm setting a random reminder to be sent throughout the day, but it shouldn't be sent at the exact time each day...
Im not an expert, are we saying the other method isn't good?
@adam In terms of changing the schedule itself to do this what attribute should I be looking into it?
Copy link to clipboard
Copied
Just do a <cfschedule action="update"> with the new values (I don't think one can specify just a subset to upate, it'll need to be the whole lot, but do some experiementation...
--
Adam
Copy link to clipboard
Copied
Which new values? I currently have it set running daily. What value would randomize the hour in the day it gets executed?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
@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.
Copy link to clipboard
Copied
Interesting. I am having a look.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
I see what you mean.
I think i should be able to set up the random hour variable so that it's always between x amount of hours as opposed to the entire 24 hour period to avoid that problem.
Ill have to play with it.
Copy link to clipboard
Copied
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">
Copy link to clipboard
Copied
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/randomInterval.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
Copy link to clipboard
Copied
Thank you both.
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
I'd just not expose the URLs that are for scheduled tasks to the outside world at all. Problem goes away.
--
Adam
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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/randomInterval.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>
Copy link to clipboard
Copied
Why not adapt the page to also run the task code!
Sorry, I thought that was sufficiently obvious as to go without saying.
But... yes... that was the idea.
--
Adam