Monday, March 26, 2012

Performing an Operation Once a Day

I want to write a script that runs just once in a day.
Like i want to add a poll of the day or send all emails after one day or something like that then how can i do?
I thought of threading but if the web application closes and then on its start how would it know if the actions have been taken? do I have to store them in some files like last poll update time etc.
Is there a better way of doing this? Moreover can i make a class that is visible to all the sessions (just like application variables) and any script can call that class or its member functions.There are several ways to go about this. You could have the web app fire the tasks (yes, you'd have to store the status in a file or database somewhere), but because ASP.NET may restart itself, the application may shut down, etc., you won't be able to control when your daily processes run. If you are simply adding content to a database, you might choose to schedule a database job to run at a specific time. You could use the Windows scheduler to fire off an exe or to run some VBScript via WSH at a predetermined time. Or, you may choose to create a Windows service to coordinate execution of each task.
To answer your second question: yes, you can create a class that is visible to all sessions. A class built using the singleton design pattern will accomplish this, with the side-effect of also being accesible to your non-HttpContext daily tasks.

Thanks for the reply.
Yes, choosing a database schedule is suitable for database jobs. I dont have direct access to my server ( i have shared hosting), so i dont think i can write a windows scheduler. Moreover i would like to write a generic script that will work on any client machine.
I didnt quite understand the second point. What is a class built on a singleton design? The side effect that it is accessible to non httpcontext tasks mean it can be accessed without the http request also?
I did the same thing with an application I wrote, this was delivered on a shared hosting plan.
I added a little script to the homepage which checked if todaysactivity had been completed, if not, it did it and then changed theflag.
Of course, someone had to visit the homepage at least once a day - butthat wasn't a problem, since the customer visited it every hour anyway.
We had two versions - one which stored data in a database, another stored the flag in an XML file.

Ya this is a good idea. I had thought of making a page that the web team will visit each day, which will execute the scripts. This way i can perform the operations.
Is it possible to make a class that sends email. This must be visible to the entire application. How can i do that?
Yes, things will get a bit trickier in a shared hosting environment. Your only option may be to have a page check a status flag and kickoff the script as has been suggested and, as long as thight scheduling is not required, should work just fine.
A singleton class can only ever have one instance, meaning that data can be easily shared between objects, etc. The advantage to using this approach versus the ASP.NET Application object is that other, non-web pieces of your application can gain access to the object. If you'd like to learn more about singleton, referenceDesign Patterns or theMS P&P website.


Thanks. The link was of great help.

0 comments:

Post a Comment