28 Replies Latest reply: Apr 22, 2012 11:42 PM by Adam Cameron. RSS

    Make cfschedule run daily but random hours

    lovewebdev Community Member

      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!

        • 1. Re: Make cfschedule run daily but random hours
          Dan Bracuk Community Member

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

          • 2. Re: Make cfschedule run daily but random hours
            lovewebdev Community Member

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

            • 3. Re: Make cfschedule run daily but random hours
              Adam Cameron. Community Member

              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

              • 4. Re: Make cfschedule run daily but random hours
                Adam Cameron. Community Member

                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

                • 5. Re: Make cfschedule run daily but random hours
                  Dan Bracuk Community Member

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

                   

                  Good point.

                  • 6. Re: Make cfschedule run daily but random hours
                    lovewebdev Community Member

                    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?

                    • 7. Re: Make cfschedule run daily but random hours
                      Adam Cameron. Community Member

                      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

                      • 8. Re: Make cfschedule run daily but random hours
                        lovewebdev Community Member

                        Which new values? I currently have it set running daily. What value would randomize the hour in the day it gets executed?

                        • 9. Re: Make cfschedule run daily but random hours
                          Adam Cameron. Community Member

                          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

                          • 10. Re: Make cfschedule run daily but random hours
                            BKBK CommunityMVP

                            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.

                            • 11. Re: Make cfschedule run daily but random hours
                              lovewebdev Community Member

                              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?

                              • 12. Re: Make cfschedule run daily but random hours
                                lovewebdev Community Member

                                @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.

                                • 13. Re: Make cfschedule run daily but random hours
                                  BKBK CommunityMVP

                                  Interesting. I am having a look.

                                  • 14. Re: Make cfschedule run daily but random hours
                                    BKBK CommunityMVP

                                    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.

                                    • 15. Re: Make cfschedule run daily but random hours
                                      lovewebdev Community Member

                                      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.

                                      • 16. Re: Make cfschedule run daily but random hours
                                        BKBK CommunityMVP

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

                                        • 17. Re: Make cfschedule run daily but random hours
                                          Adam Cameron. Community Member

                                          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

                                          • 18. Re: Make cfschedule run daily but random hours
                                            Adam Cameron. Community Member

                                            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

                                            • 19. Re: Make cfschedule run daily but random hours
                                              lovewebdev Community Member

                                              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.

                                              • 20. Re: Make cfschedule run daily but random hours
                                                BKBK CommunityMVP

                                                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.

                                                • 21. Re: Make cfschedule run daily but random hours
                                                  BKBK CommunityMVP

                                                  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.

                                                  • 22. Re: Make cfschedule run daily but random hours
                                                    Adam Cameron. Community Member

                                                    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

                                                    • 23. Re: Make cfschedule run daily but random hours
                                                      lovewebdev Community Member

                                                      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?

                                                      • 24. Re: Make cfschedule run daily but random hours
                                                        Adam Cameron. Community Member

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

                                                         

                                                        --

                                                        Adam

                                                        • 25. Re: Make cfschedule run daily but random hours
                                                          BKBK CommunityMVP

                                                          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?

                                                          • 26. Re: Make cfschedule run daily but random hours
                                                            BKBK CommunityMVP

                                                            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.

                                                            • 27. Re: Make cfschedule run daily but random hours
                                                              BKBK CommunityMVP

                                                              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>

                                                              • 28. Re: Make cfschedule run daily but random hours
                                                                Adam Cameron. Community Member

                                                                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