3

I have written a web application in asp.net. It has some user roles. There are 3 roles which are Manager, Accountant and Employee. The employees write their expenses in a form and send it to Manager. When manager approves it, it'll be sent to Accountant to pay it. I need to have an idea that when manager doesn't approve the employee's expense in 48 hours, it should send an automatic e-mail to Manager's mail.

I thought that I can write another small console application to handle that by checking every hour. But it would waste resources and decrease performance.

I need a good idea to handle that. How should I do?

Oscar Foley
  • 6,817
  • 8
  • 57
  • 90
Anıl Canlı
  • 432
  • 9
  • 20

3 Answers3

4

There are several options, but if I were you I would go with first or second options.

Console App & scheduler

I would create that console application that every time is run perform the check for you. Then I will have it run using Windows Scheduler in a daily basis (at 00:05) or a hourly basis if you prefer so. This way Windows Scheduler daemon will launch it every hour and the rest of the time your app is not running.

Check this Microsoft link to see how a scheduled task is created in windows.

Restful Web Service & scheduler

As suggested in @marapet answer, having a restful web service that allow you to perform this action instead of a console application would give you the advantage of having all code in your web application.

Similar as previous one, you should only invoke the restful uri to have your action done. As possible disadvantage, you have to get sure that that uri is not accessible to end users. In usual architecture (Web Server --> Application Server --> DB) this restful service should be in the Application Servers, far away from end user access.

Windows Service

Another option is creating a Windows Service that runs all the time and check the time itself so every hour perform the job (maybe using Quartz or similar). But this does not meet your performance requirements.

The performance hit will be small anyway as your service should check every minute to see if an hour has pass and is time to do its job.. a task pretty easy.

The advantage is that a windows service is easier to control and monitor than a Scheduled tasks

DB job

Yet another option... If your app uses SQL Server you can have a t-sql job that runs daily or hourly. I wouldn't recommend this option unless you really have performance problems.

The problem with this is that you would be splitting the logic and responsibilities of your code. A future developer or admin would find hard to maintain your app.

Oscar Foley
  • 6,817
  • 8
  • 57
  • 90
3

If you'd like to keep the logic within the web application for simplicity (depending on the total size of your solution, this may or may not be desired):

  • For a given URL, have the web app check for due approvals and sends emails out if needed. Be sure to keep track of emails sent in order to prevent sending the same email multiple times.
  • Call this URL in a regular interval. You may use a scheduled task or a third party url monitoring service to do this.

You may call the URL with a simple VBScript (or wget, or curl, or powershell, or whatever is fastest for you), which in turn you can automate by using the task scheduler (see also).

An example script in vbscript for calling an URL:

Function LoadUrl(url)
    Dim objRequest
    Set objRequest = CreateObject("MSXML2.ServerXMLHTTP.6.0")
    objRequest.open "POST", url , false
    objRequest.Send
    LoadUrl = objRequest.responseText
    Set objRequest = Nothing
End Function
Community
  • 1
  • 1
marapet
  • 54,856
  • 12
  • 170
  • 184
1

Checking every hour won't affect performance. Even checking every minute is probably fine, depending on your database. The simplest option is a console program fired as a Scheduled Task. You can also try a Windows Service but they're a bit trickier.

Also give some thought how you'll count the 48 hours. If an employee puts in expenses just before the weekend then 48 hours will probably elapse every time and you'll end up with a manager having lots of emails in their Inbox on Monday morning. That could cause some friction :)

Ed Gibbs
  • 25,924
  • 4
  • 46
  • 69