Skip navigation
Currently Being Moderated

Make cfschedule run daily but random hours

Apr 21, 2012 6:31 PM

Tags: #cfschedule

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!

 
Replies
  • Currently Being Moderated
    Apr 21, 2012 7:23 PM   in reply to lovewebdev

    Run the job at the start of the day/month.  However, amend the file being run to sleep for a random period of time.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 22, 2012 3:06 AM   in reply to Dan Bracuk

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 22, 2012 3:09 AM   in reply to lovewebdev

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 22, 2012 4:17 AM   in reply to Adam Cameron.

    Regarding, "That would tie up a thread whilst the thing is sleeping."

     

    Good point.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 22, 2012 4:43 AM   in reply to lovewebdev

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 22, 2012 6:02 AM   in reply to lovewebdev

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 22, 2012 6:37 AM   in reply to Adam Cameron.

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 22, 2012 7:46 AM   in reply to lovewebdev

    Interesting. I am having a look.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 22, 2012 7:58 AM   in reply to lovewebdev

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 22, 2012 9:31 AM   in reply to lovewebdev

    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">

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 22, 2012 9:49 AM   in reply to BKBK

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 22, 2012 9:53 AM   in reply to lovewebdev

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 22, 2012 1:11 PM   in reply to Adam Cameron.

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 22, 2012 1:17 PM   in reply to lovewebdev

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 22, 2012 1:20 PM   in reply to lovewebdev

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 22, 2012 1:29 PM   in reply to lovewebdev

    I'd just not expose the URLs that are for scheduled tasks to the outside world at all.  Problem goes away.

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 22, 2012 1:34 PM   in reply to Adam Cameron.

    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?

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 22, 2012 1:42 PM   in reply to lovewebdev

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 22, 2012 11:39 PM   in reply to Adam Cameron.

    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>

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 22, 2012 11:42 PM   in reply to BKBK

    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

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points