3

I am writing a program recording service calls and treatment done. We have a number of users who open and close calls and I want to show at all times the total number of calls opened today and the total number closed today and the difference between them. I thought of doing it with an application variable. I have to reset these variables to 0 every day. Where would I do that? I thought in the Global.asax but in which event could that be done? The application is running all the time so I suppose Application_Start wouldn't be appropriate. So where? Thank you.

Dov Miller
  • 1,958
  • 5
  • 34
  • 46
  • 3
    Lazy approach: a) save last call's time b)on each call check if day is changed and refresh counters if necessary. – default locale Mar 06 '13 at 12:11
  • Where do you record service calls? If you store them in a database then you just need a query... – Adriano Repetti Mar 06 '13 at 12:11
  • Surely a requirement like this should be stored in a database? As long as you were persisting the calls / treatments with a date flag, you could just query for the information you want. This isn't really application variable suitable information. – Evan Knowles Mar 06 '13 at 12:11
  • 1
    Using an application variable may produce inconsistent results as these are reset when the app pool is restarted which can happen (unpredicatably) when resources are low and such forth. My recommendation would be to store each call with its date/time and closed calls with date time in a DB and then use GROUP BY clauses on the dates and the COUNT function in the SELECT statement to get the numbers you need. – Luke Baughan Mar 06 '13 at 12:13
  • @bUKaneer Thank you for the information about application variables producing inconsistent results. – Dov Miller Mar 06 '13 at 12:54

4 Answers4

2

I'd have a date variable with the last time the counter was reset, and check the date is "today" on every access to the counter.

Unless you have critical performance problems, I guess that'd be the way to go.

Sample easy-lazy code to call whenever you are updating the counter:

lock(myCounter)
{ 
  if(DateTime.Now.Date != lastDateCounterWasReset)
  {
     lastDateCounterWasReset = DateTime.Now.Date;
     myCounter = 0;
  }
  myCounter++;
}

Now we'd need to know more about how you'd like to be storing those variables (myCounter and lastDateCounterWasReset), but could be basically anywhere (database, filesystem, etc.)

Jcl
  • 27,696
  • 5
  • 61
  • 92
  • -1, It is a web application. We have to store it somewhere and run automatically. – Rajesh Subramanian Mar 06 '13 at 12:12
  • 1
    How does being a web application matter? You store the variable(s) somewhere (be it a database, filesystem, or wherever) and check them upon requests. The problem here was resetting the counter, not storing or accesing the counter. – Jcl Mar 06 '13 at 12:13
  • Okay, lets assume he stores in db, when the application is going to check for the date? i mean on what event? – Rajesh Subramanian Mar 06 '13 at 12:14
  • 1
    Whenever he accessed the counter – Jcl Mar 06 '13 at 12:15
  • 1
    Thanks @defaultlocale , I also think it was pretty unreasonable this time, however everyone is entitled to their oppinion, I guess :-) – Jcl Mar 06 '13 at 19:18
2

You could configure the Periodic Restart Settings for Application Pool Recycling in IIS:

The element contains configuration settings that allow you to control when an application pool is recycled. You can specify that Internet Information Services (IIS) 7 recycle the application pool after a time interval (in minutes) or at a specific time each day. You can also configure IIS to base the recycle on the amount of virtual memory or physical memory that the worker process in the application pool is using or configure IIS to recycle the application pool after the worker process has processed a specific number of requests.

But this has a side-effect of putting the application offline during the time the pool is restarting, so if you have any user connected at that time it will lose its session. This can be minimized by restarting the application at a time you have no users connected, like at dawn.

The following config snippet set the application pool to daily recycle at 3:00 A.M.:

<add name="Example">
   <recycling logEventOnRecycle="Schedule">
     <periodicRestart>
       <schedule>
          <clear />
          <add value="03:00:00" />
        </schedule>
     </periodicRestart>
   </recycling>
 <processModel identityType="NetworkService" shutdownTimeLimit="00:00:30" startupTimeLimit="00:00:30" />
</add>
Marcus Vinicius
  • 1,891
  • 1
  • 19
  • 35
  • surely putting the app pool off line isn't the answer to this? I don't know site availability requirements, but I'd always thought the periodic was 'to compensate for bad programming' as Chris Lively eloquently says here: http://stackoverflow.com/questions/2043031/do-we-really-need-to-restart-iis-7-application-pool-do-the-same-practice-apply – NDJ Mar 06 '13 at 12:51
  • The discussion in that particular case is about cleaning up memory junk, if is really necessary restart the Web Server for that. Chris Lively associates these memory junks with bad programming, saying that is better review the application before begin recycling the pool. – Marcus Vinicius Mar 06 '13 at 13:19
  • That's kind of my point - the functionality exists to deal with cleaning up junk as you say, not resetting variables. Just seems a bit extreme IMO – NDJ Mar 06 '13 at 13:23
1

I would store the calls to a database and do a select which groups by the current day to get the total calls, etc. for display.

That way it will automatically reset for you when a new day starts, and you don't need to worry about IIS Resets destroying your in memory data.

If you don't want the performance hit of querying too often, there are a number of caching options available.

NDJ
  • 5,189
  • 1
  • 18
  • 27
  • I did want to avoid querying to often, so could you detail the caching options? Thank you. – Dov Miller Mar 06 '13 at 12:34
  • Data caching in Asp.Net - tutorial here: http://weblogs.asp.net/dotnetstories/archive/2011/02/15/data-caching-in-asp-net-applications.aspx and an SO link here http://stackoverflow.com/questions/7462298/data-caching-in-asp-net – NDJ Mar 06 '13 at 12:41
  • I would add to your solution beside saving calls, I would make a separate table with a field for the date and two counter fields and when a call is opened or closed add to the appropriate counter. – Dov Miller Mar 13 '13 at 09:04
0

I suppose you could use the Application_BeginRequest method. Use a boolean to see if it's already run that day.

Another option is a scheduler with a hidden URL to reset.

Peter
  • 171
  • 4