1
votes

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.

1
What are you ultimately using this internal clock for? If you're wanting to do something at a specific time or on a specific schedule, you can just schedule a task in the CFAdmin. - Shawn
And if this is still to see how many times an API is being called and then report it, this may be an awful lot of work. Does your application and your API use a database? If so, it'll be much easier and much more persistent to store and increment a value there from your API and reference it from your application or a scheduled reporting task. - Shawn
And RE: your code above >> i is not defined in your second function, checktime(). It looks like you're trying to pass in an integer of either minutes or seconds of Now() and then converting it to a string with a 0 in front. Are you trying to pad those values out to 2 characters and then make that a string? You also don't need the # symbols, unless you are using the variable name inside a quoted string. And if you scope the variables you use in the functions, they won't leak out. And last, if you are familiar with Javascript, you might find cfscript syntax much easier to use. - Shawn
You don't have to have a database just for this. I was just saying if you already had a database, it's much easier to use that database to track the variables you need. - Shawn
Just a tip about cffunctions. Always var all function local variables. Otherwise you can unintentionally create some bizarre bugs due to threading issues. Especially if the parent component's stored in a shared scope. - SOS

1 Answers

2
votes

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,