Forum Moderators: open

Message Too Old, No Replies

Auto running ASP Scripts

Can it be done

         

webboy1

10:57 am on Oct 22, 2004 (gmt 0)

10+ Year Member



I am looking to write a script for one of our websites. This script will involve sending out birthday cards to users. Basically the code will take todays date, search the DB for users whose birthdays match and send them an email.

I roughly know how i will code this. However, i would like the could to run automatically, but im not sure how to do this. I don't want to have to manually start the code each day (especially not at weekends).

Is there someway that i can make scripts automatically run say around 2am every morning?

I know i can use use 'if > then' statements on a page if the page has been loaded on the browser, but is there anyway to do it that it doesn't matter if the page is ever loaded - it still send the cards?

All help appreciated,

Cheers,
Webboy

txbakers

11:50 am on Oct 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can write a batch file to call IEXPLORE.EXE and that page, which will fire everynight. Other than that I don't think a web page can run automatically.

webboy1

12:01 pm on Oct 22, 2004 (gmt 0)

10+ Year Member



Cool, i will try that. Cheers.

mattglet

2:24 pm on Oct 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I know of 3 methods that are easy:
-Create a batch file
-Create a service
-Create a scheduled task

Personally, I use would use a scheduled task if the site is on a hosted server.

wackal

5:18 pm on Oct 22, 2004 (gmt 0)

10+ Year Member



put all your code in a .vbs file and use windows scripting host (WSH) to schedule the execution of the vbs file

dotme

5:26 pm on Oct 22, 2004 (gmt 0)

10+ Year Member



IMO, wackal offers the best solution. WSH may not even be needed. I have several vbs scripts that run overnight sending out birthday greetings, and I just run them through the Windows Task Scheduler.

Converting asp to vbs is pretty easy. vbs doesn't support response.write (obviously) and anywhere you do a "Server.CreateObject" replace with just "CreateObject"

That should get things rolling for you.

raywood

3:24 pm on Oct 23, 2004 (gmt 0)

10+ Year Member



I use asp.net. I use a delegate that invokes a timer with a callback function in the application on_start event. The delegate spawns a separate thread so the operation does not interfere with or bog down the website. The timer checks the system time every hour. When the hour is right, boom, there goes the program off to do its work in its own thread.

mattglet

10:12 pm on Oct 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The only problem with running your own thread is that you can't really debug if something goes wrong, can you?

duckhunter

1:49 am on Oct 24, 2004 (gmt 0)

10+ Year Member



With good error handling you can send yourself an email when error conditions are hit. Be sure to include the class/function name you were in and the error number and description in the body of your email.

raywood

3:48 am on Oct 24, 2004 (gmt 0)

10+ Year Member



That's what I do. I use a try/catch block and send the error message to myself in an email. Once I get everything working right I comment out the email code and recompile. I don't delete the email code, because I might need again when I make changes.

mattglet

11:41 pm on Oct 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



raywood-

Rather than commenting out your email code before recompile, why not just leave it in, in case of another mishap?

raywood

1:33 pm on Oct 25, 2004 (gmt 0)

10+ Year Member



Well, mattglet, the main reason I comment it out is because I usually have lots of emails in the try/catch block. I use the technique to download datafeeds from merchants for my associate marketing sites, and databases from other sites. They all have different formats, and debugging sometimes needs lots of detail info to show where I'm having trouble.

If I download a csv file, for example, I'll put it on the hard drive. Then I have to take the header out. Then I have to parse out the fields and put them into my database. So I have an email to tell me the file was downloaded. Another one to tell me that the header was successfully removed, and another one to tell me the database update went ok.

So once I finish debugging I don't want the web servers sending four or five emails from a whole bunch of sites every morning to tell all the details. I just want the final one telling me that the database was updated.

This may be a crude approach, but it works for me. If anybody has a better idea, please post it.

raywood

1:41 pm on Oct 25, 2004 (gmt 0)

10+ Year Member



BTW, there is one consideration I forgot to mention. I put the timer delegate in the application_onstart event. My sites run on commercial hosts. I have no way of knowing if the web server has been stopped and restarted since the last update. And I have no way of knowing if the application has been started. So I always hit the home page on each site early every morning. Someday I'll automate this process, but for now, I do it manually with my web browser.

webboy1

2:03 pm on Oct 25, 2004 (gmt 0)

10+ Year Member



I had actually thought of doing it manually for now i.e. like you, just hitting the homepage (or another page) everyday to make the script run.

I will keep working on it,

Cheers,
Brian

raywood

2:36 pm on Oct 25, 2004 (gmt 0)

10+ Year Member



To add a little more detail, I actually have a timer in application_onstart that fires only once, 5 seconds after the application starts. That timer actually calls the delegate that launches a separate thread and begins checking the time every hour.

The downloads and updates are usually done while I'm asleep. (Either in bed or here at my keyboard). So if I start the app manually, I'll be behind on updates until tomorrow. So I'm thinking about letting my computer request the home page a few minutes before the download each morning.