To read the article online, visit http://www.4GuysFromRolla.com/webtech/012101-1.shtml

Automatically Running a Function Every So Often

By Anthony Biondo


Have you ever had certain tasks that you wanted to automate throughout your Web site? Perhaps there was a specific function in an ASP script that you would like executed every hour, or once a day? If it is essential that this task occur every hour, on the hour, or exactly once a day at a particular time, then read Automating Tasks with WSH. This article focuses on how to have certain tasks executed roughly every hour, or roughly once a day. The benefit of this article over Automating Tasks with WSH is that with the previous article you need to be able to setup the Windows Scripting Host (WSH), which means you need access to the Web server. With this article, you just need to add some code to each of your ASP pages!

You may be wondering what types of tasks can be executed sporadically, not on a rigid schedule. I know I have written a lot of applications where I've said, "I wish that I could automatically delete the old records in this forum," or, "I wish old greeting cards can be archived and deleted." Tasks such as these are ones that do not require a precise schedule. For example, deleting those old greeting cards need not happen exactly once an hour. If it happens at noon, and then at one thirty, and then at 2:45, it's not a big deal. A good example of when code like this would be useful can be seen in this article: A Real-World Example of Caching Data in the Application Object. In that article, database-generated HTML code that takes a long time to generate is executed once every 15 minutes or so. It employs similar techniques as described in this article to determine when to bust the cache and requery the database.

The approach I took was to make it possible to execute code on a timed basis, specifically once an hour or once a day. This is all managed with an application variable that records when the last time a particular function was executed through an ASP page. If, when an ASP page is loaded, a certain amount of time has passed since the last time the function was executed, the application variable is updated with the current time and the function is called.

Another application variable, execution_feq, specifies how often a particular function should execute. The code below checks to see if the variable execution_feq is set to a D or an H. If a D is specified we will execute code on a daily basis; conversely, specifying a H will execute code on a daily basis.

In the code below we check to see if the application variable is null using the IsNull function. If the application variable is null, then it is the first time we are executing the script. We set the application variable equal to today’s date.

If the application variable is not null we do a check to see if the application variable is equivalent to today’s date. If the dates are not equal, we execute the code. If the dates are the same we do nothing (since that means that the function has been called at an earlier time today).

' CONFIGURATION OPTIONS
' Description: Set the frequency by setting the
'              execution_feq equal to D or H
  execution_feq = "D"    ' D = Execute Code Once Daily
                         ' H = Execute Code Once Hourly

on error resume next

' ////////////////////////
' EXECUTE CODE DAILY
' Description:  This set of code will execute on a Daily basis.
' ////////////////////////
if UCase(execution_feq) = "D" then
' IF THE APPLCIATION VARIABLE IS EMPTY INITIALIZE IT AND EXECUTED THE CODE

  if isNull(application("te_todaysDate")) then
    ' execute function/code
    fn_ToExecute()
		
    application.lock
    application("te_todaysDate")=FormatDateTime(now(),vbshortdate)
    application.unlock
  end if


' IF THE APPLCIATION VARIABLE IS NOT EQUAL TO TODAYS DATE EXECUTE CODE
  if application("te_todaysDate") <>  FormatDateTime(now(),vbshortdate) then
    ' execute function/code
    fn_ToExecute()

    application.lock
    application("te_todaysDate")=FormatDateTime(now(),vbshortdate)
    application.unlock
  end if
end if

So, if you have an ASP page that contains a function that you only want to have executed every so often, you can cut and paste the above code into your script and change the name of fn_ToExecute() to the name of the function you want run intermittenly.

Now that we have looked at how to have code execute daily, let's look at how to have a function that is called hourly. The following code has the same idea behind it; it simply uses some other techniques to get the hour. We first check to see if the application variable is null. If the application variable is null we populate the application variable with the current hour. We get the hour by getting the digits before the first colon using the Hour function. If the application variable is not null we compare the hour currently with the hour saved in our application variable. If the hour is different, we execute the code otherwise we do nothing.

' ////////////////////////
' EXECUTE CODE HOURLY
' Description:  This set of code will execute on a Hourly basis.
' ////////////////////////
if UCase(execution_feq) = "H" then
' GRAB THE HOUR
  my_time = Hour(Now())

' IF THE APPLCIATION VARIABLE IS EMPTY INITIALIZE IT AND EXECUTED THE CODE
  if isNull(application("te_todaysDate")) then
    ' execute function/code
    fn_ToExecute()
		
    application.lock
    application("te_todaysDate")=my_time
    application.unlock
  end if


' IF THE APPLCIATION VARIABLE IS NOT EQUAL TO TODAYS DATE EXECUTE CODE
  if application("te_todaysDate") <> my_time then
    ' execute function/code
    fn_ToExecute()

    application.lock
    application("te_todaysDate")=my_time
    application.unlock
  end if
end if

Conclusion
This script is great for he times when you want to automate certain tasks. I keep this code in an include file and usually include this code on a homepage, or a page that I know will be visited frequently. This way the code has a higher percentage of being executed each day. Since the ASP page itself needs to be visited to have the above code executed, if you do not have any visitors visiting your Web site, the scheduled function will never be called!

Happy Programming!

  • By Anthony Biondo


  • Article Information
    Article Title: Automatically Running a Function Every So Often
    Article Author: Anthony Biondo
    Published Date: Sunday, January 21, 2001
    Article URL: http://www.4GuysFromRolla.com/webtech/012101-1.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers