Search code examples
timecoldfusioncoldfusion-2016

Creating an internal clock within Coldfusion .cfc file to execute scheduled tasks


I am needing to do scheduled tasks based off an ever going internal clock. I know this is possible with Javascript, but this solution will need to be hosted within a Application.cfc file. I tried to just use a "Now" function within a OnRequestStart function, however the problem is if I need to let's say send an email at midnight to someone, but no one makes a request at midnight, then the email won't send. I tried to do the same function as well in the OnApplicationStart function, but that will only run once ideally. I was thinking about using the CreateTime method within the OnApplicationStart function, but how would I increment the time in real time, if that makes sense. I hope this is more clear on what is actually needed. Here is some code I tried out, however I am not really sure how to test if this works honestly.

<cffunction NAME="internalclock">
   <cfset today = #day(Now())#>
   <cfset  h = #hour(Now())#>
   <cfset  m = #minute(Now())#>
   <cfset  s = #second(Now())#>
   <cfset  m = #checktime(m)#>
   <cfset  s = #checktime(s)#>
   <cfsetting requestTimeOut = ".5">
</cffunction>
<cffunction  NAME="checktime">
   <cfif i LESS THAN 10 > i="0"+i
   <cfreturn i>
   </cfif>
</cffunction>

Edit: I'm having some issues accessing a variable within a page2.cfc file. The way the data is flowing is that I initialize the variable in an Application.cfc file, rewrite the same variable in page2.cfc, and access the modified variable in page3.cfm. Page3.cfm is just a test to ensure that the variable is in fact defined and updating as expected. I should mention that page2.cfc has a scheduled task on it and runs around every minute. Here is some example code:

Application.cfc
<CFFUNCTION NAME="OnApplicationStart">
  <cfset Application.currenttime = 0>
</CFFUNCTION>

page2.cfc
<CFFUNCTION NAME ="Counter">
  <cfset Application.currenttime = #Now()#>
</CFFUNCTION>

page3.cfm
<cfoutput>
  #Application.currenttime#
</cfoutput>

The error I am getting is that either the current time variable is displaying as 0, or if I change the output to Counter.currenttime it says it's not defined. I am sure I am doing something dumb, but I would appreciate the help.

Thank you all for your help.


Solution

  • This can actually be done via Scheduled Tasks within Coldfusion Administrator. I didn't know that was a thing before actually making this post. My apologies.

    Thank you,