user29964
user29964

Reputation: 15990

How do you set scheduled task properly in a webapplication

I have the following problem.
A customer of ours has an application that is used by multiple users. Now they want to notify the users that are inactive for more than 30 days.

I'm using Spring.Quartz to solve this problem. But now this stuff is running within a windows service (which communicates with the website's database).
I was wondering if it isn't possible to use the Quartz library within the web application.
I know this works as long as the application is active, but what if the application recycles? Or is inactive for some time (ex 2 days).

Edit: Regular inactivity is possible. But the notifications should still work.

Are there other methods to do this? Any help is welcome.

Cheers

Upvotes: 0

Views: 341

Answers (3)

Denis Mazourick
Denis Mazourick

Reputation: 1455

The Windows Service approach is best in this case. You can also create a Windows Schedule, which will call your page (e.g. http://[your-site]/[yourapp]/notifyusers.aspx), which will do what is necessary. Or, if you expect the application to be visited pretty often (so you're sure that it is not just recycled), place to Application_Start of global.asax the QueueWorkingItem to start the thread, which will have something like below:

private void MyPingThread(object state)
{
    ThreadExitState lstate = state as ThreadExitState;
    EventWaitHandle handle = new EventWaitHandle(false, EventResetMode.ManualReset);
    while (true)
    {
        if (lstate != null)
        {
           if (!lstate.Active)
               return;
        }
        handle.WaitOne(1000 * 60 * 60 * 4); // Run the task each 4 hours
        //Do your work here
    }
}

Upvotes: 1

šljaker
šljaker

Reputation: 7374

I usually create a new 'Class Library' project and add it to my solution.
Then I create a new task in Task Scheduler.

I never mix scheduled jobs with website. If scheduled job is unresponsive, it may slow down or even crash your website.

Upvotes: 0

Jerod Venema
Jerod Venema

Reputation: 44642

Just kick of a thread when the application starts (you can use the Global.asax). Wrap it in a while(true) and a try/catch, make sure you have some nice long sleeps in place even if errors occur (i.e. outside the try/catch block), and you're good to go. Probably not appropriate if you need down-to-the-second notification, but it should work for what you're doing.

Upvotes: 0

Related Questions